From c3fbd1164e341d0fc862d3a8ecffe77a1eea6219 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 7 Sep 2020 14:17:49 +0100 Subject: [PATCH] Logging: Implement additional logging to rotated set of files. * All logging duplicated into %TEMP%/{appname}/{logger_name}.log * These are handled by the RotatingFileHandler, currently set to 1MiB per file and 10 backup files. * Do *NOT* setLevel() on the handlers, as we want to control the level up at the logger instead. This would have caused the CL and GUI selection of loglevel to NOT have any effect (hidden by default having been DEBUG). * EDMarketConnector.py now INFO logs its startup and exit. --- EDMCLogging.py | 26 ++++++++++++++++++++++++-- EDMarketConnector.py | 3 +++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/EDMCLogging.py b/EDMCLogging.py index 12dcf374..9efaa31d 100644 --- a/EDMCLogging.py +++ b/EDMCLogging.py @@ -11,10 +11,13 @@ strings. from sys import _getframe as getframe import inspect import logging +import logging.handlers import pathlib +import tempfile +from os import mkdir from typing import Tuple -from config import config +from config import config, appname # TODO: Tests: # @@ -69,8 +72,9 @@ class Logger: self.logger_filter = EDMCContextFilter() self.logger.addFilter(self.logger_filter) + ## Our basic channel handling stdout self.logger_channel = logging.StreamHandler() - self.logger_channel.setLevel(loglevel) + # Do *NOT* set here, want logger's level to work: self.logger_channel.setLevel(loglevel) self.logger_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s.%(qualname)s:%(lineno)d: %(message)s') # noqa: E501 self.logger_formatter.default_time_format = '%Y-%m-%d %H:%M:%S' @@ -79,6 +83,24 @@ class Logger: self.logger_channel.setFormatter(self.logger_formatter) self.logger.addHandler(self.logger_channel) + ## Rotating Handler in sub-directory + ## We want the files in %TEMP%\{appname}\ as {logger_name}.log and rotated versions + ## This is {logger_name} so that EDMC.py logs to a different file. + logfile_rotating = pathlib.Path(tempfile.gettempdir()) + logfile_rotating = logfile_rotating / f'{appname}' + try: + mkdir(logfile_rotating) + except FileExistsError: + pass + logfile_rotating = logfile_rotating / f'{logger_name}.log' + + _MAXBYTES = 1024 * 1024 # 1MiB + _BACKUPS = 10 + self.logger_channel_rotating = logging.handlers.RotatingFileHandler(logfile_rotating, mode='a', maxBytes=_MAXBYTES, backupCount=_BACKUPS, encoding='utf-8', delay=False) + # Do *NOT* set here, want logger's level to work: self.logger_channel_rotating.setLevel(loglevel) + self.logger_channel_rotating.setFormatter(self.logger_formatter) + self.logger.addHandler(self.logger_channel_rotating) + def get_logger(self) -> logging.Logger: """ Obtain the self.logger of the class instance. diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 3187c9cb..783319c6 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -1036,6 +1036,7 @@ if __name__ == "__main__": if not loglevel: loglevel = logging.INFO logger.setLevel(loglevel) + logger.info('Startup') # TODO: unittests in place of these # logger.debug('Test from __main__') @@ -1083,3 +1084,5 @@ if __name__ == "__main__": root.after(0, messagebox_not_py3) root.mainloop() + + logger.info('Exiting') \ No newline at end of file