From d8da4b9dfdfda3298257e7b0ab637984bf81336d Mon Sep 17 00:00:00 2001 From: Zoia Date: Mon, 28 Jan 2019 15:10:04 +0300 Subject: [PATCH] use standard python logging facilities #5 --- JDI/core/logger/jdi_logger.py | 43 +++++++------------------------ JDI/core/logger/log_levels.py | 23 ----------------- JDI/core/settings/jdi_settings.py | 3 +-- logging.json | 26 +++++++++++++++++++ 4 files changed, 36 insertions(+), 59 deletions(-) delete mode 100644 JDI/core/logger/log_levels.py create mode 100644 logging.json diff --git a/JDI/core/logger/jdi_logger.py b/JDI/core/logger/jdi_logger.py index d39a77e..ecf46e9 100644 --- a/JDI/core/logger/jdi_logger.py +++ b/JDI/core/logger/jdi_logger.py @@ -1,43 +1,18 @@ +import json import logging +import logging.config -from JDI.core.logger.log_levels import LogLevels +from JDI.core.settings.jdi_settings import PropertyPath class JDILogger(object): + JDI_LOGGING_CONFIG_FILE_PATH = PropertyPath().get_property_file(file_name_init="logging.json") def __init__(self, name="JDI Logger"): - self.logger = logging.getLogger(name) - self.__basic_settings() - - log_level = {LogLevels.INFO, LogLevels.FATAL, LogLevels.ERROR} - - def info(self, log_msg): - if LogLevels.INFO in self.log_level: - self.logger.setLevel(LogLevels.INFO.value[0]) - self.logger.info(log_msg) - - def debug(self, log_msg): - if LogLevels.DEBUG in self.log_level: - self.logger.setLevel(LogLevels.DEBUG.value[0]) - self.logger.debug(log_msg) - def fatal(self, log_msg): - if LogLevels.FATAL in self.log_level: - self.logger.setLevel(LogLevels.FATAL.value[0]) - self.logger.fatal(log_msg) - - def warning(self, log_msg): - if LogLevels.WARNING in self.log_level: - self.logger.setLevel(LogLevels.WARNING.value[0]) - self.logger.warning(log_msg) - - def error(self, log_msg): - if LogLevels.ERROR in self.log_level: - self.logger.setLevel(LogLevels.ERROR.value[0]) - self.logger.error(log_msg) - - def __basic_settings(self): - hdlr = logging.FileHandler('jdi.log') - hdlr.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s')) - self.logger.addHandler(hdlr) + with open(self.JDI_LOGGING_CONFIG_FILE_PATH, 'r') as fd: + logging.config.dictConfig(json.load(fd)) + self.logger = logging.getLogger(name) + def __getattr__(self, name): + return getattr(self.logger, name) diff --git a/JDI/core/logger/log_levels.py b/JDI/core/logger/log_levels.py deleted file mode 100644 index 84a0e5f..0000000 --- a/JDI/core/logger/log_levels.py +++ /dev/null @@ -1,23 +0,0 @@ -from enum import Enum - - -class LogLevels(Enum): - OFF = -1, # no logging - FATAL = 50, # unexpected errors - ERROR = 40, # critical errors - WARNING = 30, # errors due to wrong params - INFO = 20, # actions info - DEBUG = 10, # debug info (not for prod) - TRACE = 8, # trace info (not for prod) - ALL = 100 # all log messages - - __priority = None - - def __init__(self, priority): - self.__priority = priority - - def get_priority(self): - return self.__priority - - def equal_or_less_than(self, level): - return self.get_priority() >= level.get_priority() diff --git a/JDI/core/settings/jdi_settings.py b/JDI/core/settings/jdi_settings.py index 31556a9..d3de8a2 100644 --- a/JDI/core/settings/jdi_settings.py +++ b/JDI/core/settings/jdi_settings.py @@ -3,8 +3,7 @@ class PropertyPath: - def get_property_file(self): - file_name_init = "jdi.properties" + def get_property_file(self, file_name_init="jdi.properties"): file_name = file_name_init i = 0 dir_to_search = os.getcwd() diff --git a/logging.json b/logging.json new file mode 100644 index 0000000..dbf71db --- /dev/null +++ b/logging.json @@ -0,0 +1,26 @@ +{ + "version": 1, + "disable_existing_loggers": true, + "formatters": { + "simple": { + "class": "logging.Formatter", + "datefmt": "%I:%M:%S", + "format": "%(asctime)s %(levelname)s %(message)s" + } + }, + "handlers": { + "file_handler": { + "level": "INFO", + "class": "logging.handlers.WatchedFileHandler", + "formatter": "simple", + "filename": "jdi.log", + "mode": "a", + "encoding": "utf-8" + } + }, + "loggers": { }, + "root": { + "handlers": ["file_handler"], + "level": "DEBUG" + } +} \ No newline at end of file