A convenient class to initialize the python logging system.
Module sppas.config
Class sppasLogSetup
Description
Constructor
Create a sppasLogSetup instance.
By default, the NullHandler is assigned: no output. The numeric values of logging levels are the followings:
- CRITICAL 50
- ERROR 40
- WARNING 30
- INFO 20
- DEBUG 10
- NOTSET 0
Logging messages which are less severe than the given level value will be ignored. When NOTSET is assigned, all messages are printed.
Parameters
- log_level: Set the threshold logger value
View Source
def __init__(self, log_level=0):
"""Create a sppasLogSetup instance.
By default, the NullHandler is assigned: no output.
The numeric values of logging levels are the followings:
- CRITICAL 50
- ERROR 40
- WARNING 30
- INFO 20
- DEBUG 10
- NOTSET 0
Logging messages which are less severe than the given level value
will be ignored. When NOTSET is assigned, all messages are printed.
:param log_level: Set the threshold logger value
"""
self._log_level = int(log_level)
format_msg = '%(asctime)s [%(levelname)s] %(message)s'
self._formatter = logging.Formatter(format_msg)
for h in reversed(list(logging.getLogger().handlers)):
logging.getLogger().removeHandler(h)
self._handler = logging.NullHandler()
logging.getLogger().addHandler(self._handler)
logging.getLogger().setLevel(self._log_level)
Public functions
set_log_level
Fix the log level.
Parameters
- log_level: Set the threshold for the logger
View Source
def set_log_level(self, log_level):
"""Fix the log level.
:param log_level: Set the threshold for the logger
"""
if log_level == self._log_level:
return
self._log_level = int(log_level)
if self._handler is not None:
self._handler.setLevel(self._log_level)
logging.getLogger().setLevel(self._log_level)
logging.info('Logging set up level={:d}'.format(self._log_level))
stream_handler
Start to redirect to logging StreamHandler.
View Source
def stream_handler(self):
"""Start to redirect to logging StreamHandler.
"""
self.__stop_handler()
self._handler = logging.StreamHandler()
self._handler.setFormatter(self._formatter)
self._handler.setLevel(self._log_level)
logging.getLogger().addHandler(self._handler)
logging.info('Logging redirected to StreamHandler (level={:d}).'.format(self._log_level))
null_handler
Start to redirect to logging NullHandler.
View Source
def null_handler(self):
"""Start to redirect to logging NullHandler.
"""
self.__stop_handler()
self._handler = logging.NullHandler()
logging.getLogger().addHandler(self._handler)
file_handler
Start to redirect to logging FileHandler.
Parameters
- filename: a FileHandler has to be created using this filename.
- with_stream: (bool) A StreamHandler is added too.
View Source
def file_handler(self, filename, with_stream=False):
"""Start to redirect to logging FileHandler.
:param filename: a FileHandler has to be created using this filename.
:param with_stream: (bool) A StreamHandler is added too.
"""
self.__stop_handler()
self._handler = logging.FileHandler(filename, 'a+')
self._handler.setFormatter(self._formatter)
self._handler.setLevel(self._log_level)
logging.getLogger().addHandler(self._handler)
if with_stream is True:
console_handler = logging.StreamHandler()
console_handler.setFormatter(self._formatter)
console_handler.setLevel(self._log_level)
logging.getLogger().addHandler(console_handler)
logging.info('Logging redirected to FileHandler (level={:d}) in file {:s}.'.format(self._log_level, filename))
Protected functions
__stop_handler
Stops the current handler.
View Source
def __stop_handler(self):
"""Stops the current handler."""
if self._handler is not None:
logging.info('Stops current logging handler.')
logging.getLogger().removeHandler(self._handler)
for h in reversed(list(logging.getLogger().handlers)):
logging.getLogger().removeHandler(h)
self._handler = None