Commit 2ff2f269 authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

Improve logging, and ability to set loglevel on the fly

parent df3b1c36
......@@ -258,6 +258,7 @@ def main():
args = parser.parse_args()
# Set log level
LOGGER.setLevel(cm.LOG_LEVELS[vars(args)['verbose']])
cm.log_level(vars(args)['verbose'])
# launch associated command
args.func(args)
......@@ -40,7 +40,7 @@ DIR_LOGS = tempfile.gettempdir() + '/'
# Logging
LOGGER_NAME = info.PACKAGE_NAME
LOG_LEVEL = 'info'
LOG_LEVEL = 'INFO'
LOG_LEVELS = {'debug': logging.DEBUG,
'info': logging.INFO,
'error': logging.ERROR}
......@@ -55,7 +55,7 @@ def logger(name=LOGGER_NAME, logfilename=None):
return logging.getLogger(name)
_logger = logging.getLogger(LOGGER_NAME)
_logger.setLevel(LOG_LEVELS[LOG_LEVEL])
_logger.setLevel(LOG_LEVEL)
# log file
formatter = logging.Formatter(
......@@ -66,7 +66,7 @@ file_handler = RotatingFileHandler(
dt.datetime.now().strftime("%Y-%m-%d_%H:%M:%S") + '.log',
'a', 100000000, 1
)
file_handler.setLevel(LOG_LEVELS[LOG_LEVEL])
file_handler.setLevel(LOG_LEVEL)
file_handler.setFormatter(formatter)
_logger.addHandler(file_handler)
......@@ -74,14 +74,19 @@ _logger.addHandler(file_handler)
stream_handler = logging.StreamHandler()
formatter = logging.Formatter('%(levelname)s: %(message)s')
stream_handler.setFormatter(formatter)
stream_handler.setLevel(LOG_LEVELS[LOG_LEVEL])
stream_handler.setLevel(LOG_LEVEL)
_logger.addHandler(stream_handler)
def log_level(level):
"""Set terminal log level to given one"""
handlers = (_ for _ in _logger.handlers
if _.__class__ is logging.StreamHandler
)
for handler in handlers:
handler.setLevel(level.upper())
"""Set terminal/file log level to given one.
.. note:: Don't forget the propagation system of messages:
From logger to handlers. Handlers receive log messages only if
the main logger doesn't filter them.
"""
# Main logger
_logger.setLevel(level.upper())
# Handlers
[handler.setLevel(level.upper()) for handler in _logger.handlers
if handler.__class__ in (logging.StreamHandler,
logging.handlers.RotatingFileHandler)
]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment