Skip to content

Commit

Permalink
use standard python logging facilities #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoia committed Jan 28, 2019
1 parent 6932fd1 commit d8da4b9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 59 deletions.
43 changes: 9 additions & 34 deletions JDI/core/logger/jdi_logger.py
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 0 additions & 23 deletions JDI/core/logger/log_levels.py

This file was deleted.

3 changes: 1 addition & 2 deletions JDI/core/settings/jdi_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
26 changes: 26 additions & 0 deletions logging.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit d8da4b9

Please sign in to comment.