Custom logger with colors on terminal.
And some useful decorators.
pip install mwk-logger
from mwk_logger import MwkLogger
log = MwkLogger(name='mwk',
file='logger.log',
stream_level='DEBUG',
file_level='DEBUG',
time=True)
keyword parameters:
- name - name of the logger, by default = 'mwk',
- file - path to file to log into, by default = 'mwk.log',
- stream_level - logging level for terminal, by default = 'WARNING',
- file_level - logging level for file, by default = None,
- time - if timestamp should be added to terminal log, by default = False,
LEVELS:
None - no logging or:
'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'.
If both levels are set to None stream_level is changed to WARNING.
Timestamp is always added to file logs. One can set if timestamp will be added to terminal logs.
log.debug('This is a debug message.')
log.info('This is an info message.')
log.warning('This is a warning message.')
log.error('This is an error message!')
log.critical('This is a critical message!!!')
log.exception('This is an exception message!')
- @timer - print or log the runtime of the decorated function
- @f_sig - print or log the signature and the return value of the decorated function
Prints on screen.
from mwk_logger import timer, f_sig
@timer
@f_sig
def function(*args, **kwargs):
# ... some function ...
return 'something'
Output is logged with provided logger with level = INFO.
!!! keyword logger is obligatory !!!
from mwk_logger import MwkLogger, timer, f_sig
log = MwkLogger()
@timer(logger=log)
@f_sig(logger=log)
def function(*args, **kwargs):
# ... some function to be logged...
return 'something'