Browse Source

Update logging

master
Joshua Rubingh 4 months ago
parent
commit
acdc0e80e7
  1. 1
      .gitignore
  2. 50
      agent.py
  3. 2
      log/.gitignore

1
.gitignore

@ -139,4 +139,5 @@ dmypy.json
# Cython debug symbols
cython_debug/
# Development IDE
.vscode/launch.json

50
agent.py

@ -1,6 +1,51 @@
import logging
import logging.handlers
from logging.config import dictConfig, fileConfig
from pathlib import Path
import time
import glob
import codecs
import zipfile
class TimedCompressedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
"""
Extended version of TimedRotatingFileHandler that compress logs on rollover.
"""
def doRollover(self):
"""
do a rollover; in this case, a date/time stamp is appended to the filename
when the rollover happens. However, you want the file to be named for the
start of the interval, not the current time. If there is a backup count,
then we have to get a list of matching filenames, sort them and remove
the one with the oldest suffix.
"""
self.stream.close()
# get the time that this sequence started at and make it a TimeTuple
t = self.rolloverAt - self.interval
timeTuple = time.localtime(t)
dfn = self.baseFilename + "." + time.strftime(self.suffix, timeTuple)
if os.path.exists(dfn):
os.remove(dfn)
os.rename(self.baseFilename, dfn)
if self.backupCount > 0:
# find the oldest log file and delete it
s = glob.glob(self.baseFilename + ".20*")
if len(s) > self.backupCount:
s.sort()
os.remove(s[0])
#print "%s -> %s" % (self.baseFilename, dfn)
if self.encoding:
self.stream = codecs.open(self.baseFilename, 'w', self.encoding)
else:
self.stream = open(self.baseFilename, 'w')
self.rolloverAt = self.rolloverAt + self.interval
if os.path.exists(dfn + ".zip"):
os.remove(dfn + ".zip")
file = zipfile.ZipFile(dfn + ".zip", "w")
file.write(dfn, os.path.basename(dfn), zipfile.ZIP_DEFLATED)
file.close()
os.remove(dfn)
if Path('logging.conf').exists():
# Custom logging config
@ -19,9 +64,10 @@ else:
'level': logging.WARNING},
'file' : {
'class': 'logging.handlers.RotatingFileHandler',
'class': 'agent.TimedCompressedRotatingFileHandler',
'formatter': 'f',
'filename': 'agent.log',
'filename': 'log/agent.log',
'backupCount' : 3,
'level': logging.INFO}
},

2
log/.gitignore

@ -0,0 +1,2 @@
*.*
!.gitignore
Loading…
Cancel
Save