dpa*_*ino 4 python logging json
我需要在 json 中准备一个日志配置文件,该文件按时间、大小和压缩 de 文件旋转,为我的应用程序中的所有模块旋转(我现在卡住了)。我想使用一个 json 配置文件来完成它,这是我当前的文件,但是这个配置只按时间轮换:
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"simple": {
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
}
},
"handlers": {
"my_rotate": {
"level": "DEBUG",
"class": "logging.handlers.TimedRotatingFileHandler",
"formatter": "simple",
"when": "D",
"interval": 1,
"backupCount": 5,
"filename": "/var/log/test.log",
"encoding": "utf8"
}
},
"loggers": {
"my_module": {
"level": "DEBUG",
"handlers": ["my_rotate"]
}
},
"root": {
"level": "DEBUG",
"handlers": ["my_rotate"],
"propagate": false
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在处理程序部分(logging.handlers.RotateFileHandler)中添加其他处理程序以按大小旋转结果日志文件重复所有消息。
我的主程序使用以下方法获取记录器属性:
config_json_file = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "logging.json"))
logging_data = json.load(config_json_file)
logging.config.dictConfig(logging_data)
Run Code Online (Sandbox Code Playgroud)
我的模块像这样调用记录器:
class StoreData(object):
def __init__(self):
self.logger = logging.getLogger(__name__)
Run Code Online (Sandbox Code Playgroud)
我怎么能在这个文件中混合我的三个要求(按大小和时间旋转,并压缩旋转的文件)?
非常感谢!
更新 1
正如@Bakuriu 所建议的,我创建了一个新类来覆盖所需的方法,以便能够压缩旋转文件:
class MyRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
""" My rotating file hander to compress rotated file """
def __init__(self, **kwargs):
logging.handlers.TimedRotatingFileHandler.__init__(self, **kwargs)
def rotate(self, source, dest):
""" Compress rotated log file """
os.rename(source, dest)
f_in = open(dest, 'rb')
f_out = gzip.open("%s.gz" % dest, 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()
os.remove(dest)
Run Code Online (Sandbox Code Playgroud)
所以这个“新”类被称为 fron 日志配置文件,我在其中添加了一个新的处理程序:
"handlers": {
"my_rotate": {
"level": "DEBUG",
"class": "myMainPackage.MyRotatingFileHandler",
"formatter": "simple",
"when": "D",
"interval": 1,
"backupCount": 5,
"filename": "/var/log/test.log",
"encoding": "utf8"
}
Run Code Online (Sandbox Code Playgroud)
在下一次更新中,我将添加一个方法来覆盖所需的方法,并且能够将时间和文件大小轮换混合在一起。
更新 2
好吧,我已经覆盖了“shouldRollover”方法,因此我混合了旋转的时间和文件大小。
class MyRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
""" My rotating file hander to compress rotated file """
def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None,
delay=0, when='h', interval=1, utc=False):
if maxBytes > 0:
mode = 'a'
logging.handlers.TimedRotatingFileHandler.__init__(
self, filename, when, interval, backupCount, encoding, delay, utc)
self.maxBytes = maxBytes
self.backupCount = backupCount
def shouldRollover(self, record):
""" Determine if rollover should occur. """
# Check rollover by size
if self.stream is None: # delay was set...
self.stream = self._open()
if self.maxBytes > 0: # are we rolling over?
msg = "%s\n" % self.format(record)
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
if self.stream.tell() + len(msg) >= self.maxBytes:
return 1
# Check rollover by time
t = int(time.time())
if t >= self.rolloverAt:
return 1
return 0
def rotate(self, source, dest):
""" Compress rotated log file """
os.rename(source, dest)
f_in = open(dest, 'rb')
f_out = gzip.open("%s.gz" % dest, 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()
os.remove(dest)
Run Code Online (Sandbox Code Playgroud)
并在 json 中修改我的日志记录配置文件以按天轮换时间但以秒为单位检查......这意味着86400s(一天中的秒数)和5Mb的大小限制:
"handlers": {
"my_rotate_timed": {
"level": "DEBUG",
"class": "myMainPackage.MyRotatingFileHandler",
"formatter": "simple",
"when": "S",
"interval": 86400,
"backupCount": 5,
"maxBytes": 5242880,
"filename": "/var/log/test.log",
"encoding": "utf8"
}
Run Code Online (Sandbox Code Playgroud)
这种方式重用了 TimedRotationFileHandler 的其他方法,如果旋转被maxBytes调用,那么它使用从init TimedRotationFileHandler 方法继承的以秒为单位的后缀,格式为:“%Y-%m-%d_%H-%M-%S” . 这就是我使用 "{when="S", interval=86400}" 而不是 "{when="D", interval=1}" 的原因
这是可以预料的。如果你添加一个处理程序,这个处理程序要么不会产生消息(由于过滤),所以什么都不会改变,或者它会写这些消息(从而复制它们)。
TheTimedRotatingFileHandler和RotatingFileHandleronly 支持分别按时间和大小旋转。不能同时进行。
AFAIK没有内置的方式来实现你想要的,所以只使用配置文件你将无法实现你想要的,你必须编写一些代码来组合功能。
考虑阅读如何在Logging Cookbook 中创建新的旋转处理程序。如果将此处理程序类保存在文件中mypackage.myrotatinghandler,则可以指定:
class: 'mypackage.myrotatinghandler.MyHandler'
Run Code Online (Sandbox Code Playgroud)
在配置文件中。
请注意,要在保存文件时添加压缩rotator,只需使用保存压缩文件的函数设置旋转处理程序的属性即可。取自上面的链接:
def namer(name):
return name + ".gz"
def rotator(source, dest):
with open(source, "rb") as sf:
data = sf.read()
compressed = zlib.compress(data, 9)
with open(dest, "wb") as df:
df.write(compressed)
os.remove(source)
rh = logging.handlers.RotatingFileHandler(...)
rh.rotator = rotator
rh.namer = namer
Run Code Online (Sandbox Code Playgroud)
该rh处理器将表现为一个正常的RotatingFileHandler,但也压缩日志。
但是,设置执行轮换的条件需要您重写处理程序的部分内容。您可以阅读logging.handlers模块的源代码以了解内置处理程序是如何实现的。
| 归档时间: |
|
| 查看次数: |
2763 次 |
| 最近记录: |