mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-04 14:31:13 +02:00
Support for log rotation and compression. Fixes #1586
This commit is contained in:
1 parent
3b87a19979
commit
4ac999461b
2 files changed
+44
-4
No files matched your search
@@ -21,6 +21,11 @@
|
||||
|
||||
import logging
|
||||
import sys
|
||||
import os
|
||||
import shutil
|
||||
import gzip
|
||||
|
||||
from logging.handlers import RotatingFileHandler
|
||||
|
||||
|
||||
class ColouredFormatter(logging.Formatter):
|
||||
@@ -108,9 +113,37 @@ class LogFilter:
|
||||
return 1
|
||||
|
||||
|
||||
def init_logger(level, logfile=None, quiet=False):
|
||||
class CompressedRotatingFileHandler(RotatingFileHandler):
|
||||
"""
|
||||
Custom rotating file handler with compression support.
|
||||
"""
|
||||
|
||||
def doRollover(self):
|
||||
if self.stream:
|
||||
self.stream.close()
|
||||
if self.backupCount > 0:
|
||||
for i in range(self.backupCount - 1, 0, -1):
|
||||
sfn = "%s.%d.gz" % (self.baseFilename, i)
|
||||
dfn = "%s.%d.gz" % (self.baseFilename, i + 1)
|
||||
if os.path.exists(sfn):
|
||||
if os.path.exists(dfn):
|
||||
os.remove(dfn)
|
||||
os.rename(sfn, dfn)
|
||||
dfn = self.baseFilename + ".1.gz"
|
||||
if os.path.exists(dfn):
|
||||
os.remove(dfn)
|
||||
with open(self.baseFilename, 'rb') as f_in, gzip.open(dfn, 'wb') as f_out:
|
||||
shutil.copyfileobj(f_in, f_out)
|
||||
self.mode = 'w'
|
||||
self.stream = self._open()
|
||||
|
||||
|
||||
def init_logger(level, logfile=None, max_bytes=1000000000, backup_count=10, compression=True, quiet=False):
|
||||
if logfile and len(logfile) > 0:
|
||||
stream_handler = logging.FileHandler(logfile)
|
||||
if compression:
|
||||
stream_handler = CompressedRotatingFileHandler(logfile, maxBytes=max_bytes, backupCount=backup_count)
|
||||
else:
|
||||
stream_handler = RotatingFileHandler(logfile, maxBytes=max_bytes, backupCount=backup_count)
|
||||
stream_handler.formatter = ColouredFormatter("{asctime} {levelname} {filename}:{lineno} {message}", "%Y-%m-%d %H:%M:%S", "{")
|
||||
elif sys.platform.startswith("win"):
|
||||
stream_handler = WinStreamHandler(sys.stdout)
|
||||
|
||||
Reference in new issue
Block a user