forked from p15670423/monkey
island: Configure loggin in python code instead of JSON
This commit is contained in:
parent
cc00b85edc
commit
14b68580f8
|
@ -1,38 +1,15 @@
|
||||||
import logging.config
|
import logging
|
||||||
|
import logging.handlers
|
||||||
import os
|
import os
|
||||||
from copy import deepcopy
|
import sys
|
||||||
|
|
||||||
ISLAND_LOG_FILENAME = "monkey_island.log"
|
ISLAND_LOG_FILENAME = "monkey_island.log"
|
||||||
|
LOG_FORMAT = (
|
||||||
LOGGER_CONFIG_DICT = {
|
"%(asctime)s - %(filename)s:%(lineno)s - %(funcName)10s() - %(levelname)s - %(message)s"
|
||||||
"version": 1,
|
)
|
||||||
"disable_existing_loggers": False,
|
FILE_MAX_BYTES = 10485760
|
||||||
"formatters": {
|
FILE_BACKUP_COUNT = 20
|
||||||
"simple": {
|
FILE_ENCODING = "utf8"
|
||||||
"format": "%(asctime)s - %(filename)s:%(lineno)s - "
|
|
||||||
+ "%(funcName)10s() - %(levelname)s - %(message)s"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"handlers": {
|
|
||||||
"console_handler": {
|
|
||||||
"class": "logging.StreamHandler",
|
|
||||||
"formatter": "simple",
|
|
||||||
"stream": "ext://sys.stdout",
|
|
||||||
},
|
|
||||||
"file_handler": {
|
|
||||||
"class": "logging.handlers.RotatingFileHandler",
|
|
||||||
"formatter": "simple",
|
|
||||||
"filename": None, # set in setup_logging()
|
|
||||||
"maxBytes": 10485760,
|
|
||||||
"backupCount": 20,
|
|
||||||
"encoding": "utf8",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"root": {
|
|
||||||
"level": None, # set in setup_logging()
|
|
||||||
"handlers": ["console_handler", "file_handler"],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def setup_logging(data_dir_path, log_level):
|
def setup_logging(data_dir_path, log_level):
|
||||||
|
@ -42,12 +19,32 @@ def setup_logging(data_dir_path, log_level):
|
||||||
:param log_level: level to log from
|
:param log_level: level to log from
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
logger = logging.getLogger()
|
||||||
|
logger.setLevel(log_level.upper())
|
||||||
|
|
||||||
logger_configuration = deepcopy(LOGGER_CONFIG_DICT)
|
formatter = _get_log_formatter()
|
||||||
|
|
||||||
logger_configuration["handlers"]["file_handler"]["filename"] = os.path.join(
|
log_file_path = os.path.join(data_dir_path, ISLAND_LOG_FILENAME)
|
||||||
data_dir_path, ISLAND_LOG_FILENAME
|
_add_file_handler(logger, formatter, log_file_path)
|
||||||
|
|
||||||
|
_add_console_handler(logger, formatter)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_log_formatter():
|
||||||
|
return logging.Formatter(LOG_FORMAT)
|
||||||
|
|
||||||
|
|
||||||
|
def _add_file_handler(logger, formatter, file_path):
|
||||||
|
fh = logging.handlers.RotatingFileHandler(
|
||||||
|
file_path, maxBytes=FILE_MAX_BYTES, backupCount=FILE_BACKUP_COUNT, encoding=FILE_ENCODING
|
||||||
)
|
)
|
||||||
logger_configuration["root"]["level"] = log_level.upper()
|
fh.setFormatter(formatter)
|
||||||
|
|
||||||
logging.config.dictConfig(logger_configuration)
|
logger.addHandler(fh)
|
||||||
|
|
||||||
|
|
||||||
|
def _add_console_handler(logger, formatter):
|
||||||
|
ch = logging.StreamHandler(stream=sys.stdout)
|
||||||
|
ch.setFormatter(formatter)
|
||||||
|
|
||||||
|
logger.addHandler(ch)
|
||||||
|
|
Loading…
Reference in New Issue