|
@@ -34,7 +34,13 @@ class InterceptHandler(logging.Handler):
|
|
# 现在 xxx.log xxx1.log xxx2.log 如果backup_count 是2位数时 则 01 02 03 三位数 001 002 .. 文件由近及远
|
|
# 现在 xxx.log xxx1.log xxx2.log 如果backup_count 是2位数时 则 01 02 03 三位数 001 002 .. 文件由近及远
|
|
class RotatingFileHandler(BaseRotatingHandler):
|
|
class RotatingFileHandler(BaseRotatingHandler):
|
|
def __init__(
|
|
def __init__(
|
|
- self, filename, mode="a", max_bytes=0, backup_count=0, encoding=None, delay=0
|
|
|
|
|
|
+ self,
|
|
|
|
+ filename,
|
|
|
|
+ mode="a",
|
|
|
|
+ max_bytes=0,
|
|
|
|
+ backup_count=0,
|
|
|
|
+ encoding=None,
|
|
|
|
+ delay=0
|
|
):
|
|
):
|
|
BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
|
|
BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
|
|
self.max_bytes = max_bytes
|
|
self.max_bytes = max_bytes
|
|
@@ -45,12 +51,13 @@ class RotatingFileHandler(BaseRotatingHandler):
|
|
if self.stream:
|
|
if self.stream:
|
|
self.stream.close()
|
|
self.stream.close()
|
|
self.stream = None
|
|
self.stream = None
|
|
|
|
+
|
|
if self.backup_count > 0:
|
|
if self.backup_count > 0:
|
|
for i in range(self.backup_count - 1, 0, -1):
|
|
for i in range(self.backup_count - 1, 0, -1):
|
|
- sfn = ("%0" + self.placeholder + "d.") % i # '%2d.'%i -> 02
|
|
|
|
- sfn = sfn.join(self.baseFilename.split("."))
|
|
|
|
# sfn = "%d_%s" % (i, self.baseFilename)
|
|
# sfn = "%d_%s" % (i, self.baseFilename)
|
|
# dfn = "%d_%s" % (i + 1, self.baseFilename)
|
|
# dfn = "%d_%s" % (i + 1, self.baseFilename)
|
|
|
|
+ sfn = ("%0" + self.placeholder + "d.") % i # '%2d.'%i -> 02
|
|
|
|
+ sfn = sfn.join(self.baseFilename.split("."))
|
|
dfn = ("%0" + self.placeholder + "d.") % (i + 1)
|
|
dfn = ("%0" + self.placeholder + "d.") % (i + 1)
|
|
dfn = dfn.join(self.baseFilename.split("."))
|
|
dfn = dfn.join(self.baseFilename.split("."))
|
|
if os.path.exists(sfn):
|
|
if os.path.exists(sfn):
|
|
@@ -58,27 +65,29 @@ class RotatingFileHandler(BaseRotatingHandler):
|
|
if os.path.exists(dfn):
|
|
if os.path.exists(dfn):
|
|
os.remove(dfn)
|
|
os.remove(dfn)
|
|
os.rename(sfn, dfn)
|
|
os.rename(sfn, dfn)
|
|
- dfn = (("%0" + self.placeholder + "d.") % 1).join(
|
|
|
|
- self.baseFilename.split(".")
|
|
|
|
- )
|
|
|
|
|
|
+
|
|
|
|
+ dfn = (("%0" + self.placeholder + "d.") % 1).join(self.baseFilename.split("."))
|
|
if os.path.exists(dfn):
|
|
if os.path.exists(dfn):
|
|
os.remove(dfn)
|
|
os.remove(dfn)
|
|
|
|
+
|
|
# Issue 18940: A file may not have been created if delay is True.
|
|
# Issue 18940: A file may not have been created if delay is True.
|
|
if os.path.exists(self.baseFilename):
|
|
if os.path.exists(self.baseFilename):
|
|
os.rename(self.baseFilename, dfn)
|
|
os.rename(self.baseFilename, dfn)
|
|
|
|
+
|
|
if not self.delay:
|
|
if not self.delay:
|
|
self.stream = self._open()
|
|
self.stream = self._open()
|
|
|
|
|
|
def shouldRollover(self, record):
|
|
def shouldRollover(self, record):
|
|
-
|
|
|
|
if self.stream is None: # delay was set...
|
|
if self.stream is None: # delay was set...
|
|
self.stream = self._open()
|
|
self.stream = self._open()
|
|
|
|
+
|
|
if self.max_bytes > 0: # are we rolling over?
|
|
if self.max_bytes > 0: # are we rolling over?
|
|
# print('record >>>> ', record)
|
|
# print('record >>>> ', record)
|
|
msg = "%s\n" % self.format(record)
|
|
msg = "%s\n" % self.format(record)
|
|
self.stream.seek(0, 2) # due to non-posix-compliant Windows feature
|
|
self.stream.seek(0, 2) # due to non-posix-compliant Windows feature
|
|
if self.stream.tell() + len(msg) >= self.max_bytes:
|
|
if self.stream.tell() + len(msg) >= self.max_bytes:
|
|
return 1
|
|
return 1
|
|
|
|
+
|
|
return 0
|
|
return 0
|
|
|
|
|
|
|
|
|
|
@@ -151,7 +160,7 @@ def get_logger(
|
|
# 定义一个RotatingFileHandler,最多备份5个日志文件,每个日志文件最大10M
|
|
# 定义一个RotatingFileHandler,最多备份5个日志文件,每个日志文件最大10M
|
|
if is_write_to_file:
|
|
if is_write_to_file:
|
|
if path and not os.path.exists(os.path.dirname(path)):
|
|
if path and not os.path.exists(os.path.dirname(path)):
|
|
- os.makedirs(os.path.dirname(path))
|
|
|
|
|
|
+ os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
|
|
|
|
rf_handler = RotatingFileHandler(
|
|
rf_handler = RotatingFileHandler(
|
|
path,
|
|
path,
|