Browse Source

修复日志文件无创建权限导致的异常

dongzhaorui 3 months ago
parent
commit
1b49bd2062
1 changed files with 17 additions and 8 deletions
  1. 17 8
      FworkSpider/feapder/utils/log.py

+ 17 - 8
FworkSpider/feapder/utils/log.py

@@ -34,7 +34,13 @@ class InterceptHandler(logging.Handler):
 # 现在 xxx.log xxx1.log xxx2.log  如果backup_count 是2位数时  则 01  02  03 三位数 001 002 .. 文件由近及远
 class RotatingFileHandler(BaseRotatingHandler):
     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)
         self.max_bytes = max_bytes
@@ -45,12 +51,13 @@ class RotatingFileHandler(BaseRotatingHandler):
         if self.stream:
             self.stream.close()
             self.stream = None
+
         if self.backup_count > 0:
             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)
                 # 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 = dfn.join(self.baseFilename.split("."))
                 if os.path.exists(sfn):
@@ -58,27 +65,29 @@ class RotatingFileHandler(BaseRotatingHandler):
                     if os.path.exists(dfn):
                         os.remove(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):
                 os.remove(dfn)
+
             # Issue 18940: A file may not have been created if delay is True.
             if os.path.exists(self.baseFilename):
                 os.rename(self.baseFilename, dfn)
+
         if not self.delay:
             self.stream = self._open()
 
     def shouldRollover(self, record):
-
         if self.stream is None:  # delay was set...
             self.stream = self._open()
+
         if self.max_bytes > 0:  # are we rolling over?
             # print('record >>>> ', record)
             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.max_bytes:
                 return 1
+
         return 0
 
 
@@ -151,7 +160,7 @@ def get_logger(
     # 定义一个RotatingFileHandler,最多备份5个日志文件,每个日志文件最大10M
     if is_write_to_file:
         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(
             path,