Add Evennia logfile class based on Twisted LogFile for automatic rotating channel logs. Append tail end of logs when rotating, customizable with settings.

This commit is contained in:
Tehom 2017-05-20 21:33:57 -04:00
parent 112e457cc3
commit f150d8bae5
2 changed files with 29 additions and 3 deletions

View file

@ -125,6 +125,10 @@ LOCKWARNING_LOG_FILE = os.path.join(LOG_DIR, 'lockwarnings.log')
# file sizes down. Turn off to get ever growing log files and never # file sizes down. Turn off to get ever growing log files and never
# loose log info. # loose log info.
CYCLE_LOGFILES = True CYCLE_LOGFILES = True
# Number of lines to append to rotating channel logs when they rotate
NUM_LOG_TAIL_LINES = 20
# Max size of channel log files before they rotate
LOG_ROTATE_SIZE = 1000000
# Local time zone for this installation. All choices can be found here: # Local time zone for this installation. All choices can be found here:
# http://www.postgresql.org/docs/8.0/interactive/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE # http://www.postgresql.org/docs/8.0/interactive/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE
TIME_ZONE = 'UTC' TIME_ZONE = 'UTC'

View file

@ -19,8 +19,9 @@ import os
import time import time
from datetime import datetime from datetime import datetime
from traceback import format_exc from traceback import format_exc
from twisted.python import log from twisted.python import log, logfile
from twisted.internet.threads import deferToThread from twisted.internet.threads import deferToThread
from django.conf import settings
_LOGDIR = None _LOGDIR = None
@ -153,6 +154,27 @@ log_depmsg = log_dep
# Arbitrary file logger # Arbitrary file logger
class EvenniaLogFile(logfile.LogFile):
num_lines_to_append = settings.NUM_LOG_TAIL_LINES
def rotate(self):
append_tail = self.num_lines_to_append > 0
print "append_tail is %s" % append_tail
if not append_tail:
logfile.LogFile.rotate(self)
return
lines = tail_log_file(self.path, 0, self.num_lines_to_append)
print "lines is %s" % lines
logfile.LogFile.rotate(self)
for line in lines:
self.write(line)
def seek(self, *args, **kwargs):
return self._file.seek(*args, **kwargs)
def readlines(self, *args, **kwargs):
return self._file.readlines(*args, **kwargs)
_LOG_FILE_HANDLES = {} # holds open log handles _LOG_FILE_HANDLES = {} # holds open log handles
@ -164,7 +186,6 @@ def _open_log_file(filename):
""" """
global _LOG_FILE_HANDLES, _LOGDIR global _LOG_FILE_HANDLES, _LOGDIR
if not _LOGDIR: if not _LOGDIR:
from django.conf import settings
_LOGDIR = settings.LOG_DIR _LOGDIR = settings.LOG_DIR
filename = os.path.join(_LOGDIR, filename) filename = os.path.join(_LOGDIR, filename)
@ -173,7 +194,8 @@ def _open_log_file(filename):
return _LOG_FILE_HANDLES[filename] return _LOG_FILE_HANDLES[filename]
else: else:
try: try:
filehandle = open(filename, "a+") # append mode + reading filehandle = EvenniaLogFile.fromFullPath(filename, rotateLength=settings.LOG_ROTATE_SIZE)
# filehandle = open(filename, "a+") # append mode + reading
_LOG_FILE_HANDLES[filename] = filehandle _LOG_FILE_HANDLES[filename] = filehandle
return filehandle return filehandle
except IOError: except IOError: