A simple pure python colorama/HTML capable logger
This is a library for creating a limited pure Python (3.10+) version of the standard logging object. There are 3 main classes:
SimpleLogger
FileLogger
WarningRedirector
And a few functions:
get_logger
- v1.6 adds functionality of
get_logger2
- v1.6 adds functionality of
get_logger2
(will be removed in v1.7)- more general method in <v1.6 (same in v1.6)
log_exc
(new in v1.5)
SimpleLogger
is limited in that:
- no handlers
The additional features that the SimpleLogger
has:
- support for colorama highlighting
- automatically disabled when piping to a file
- automatically disabled in Spyder
- HTML support for the Jupyter notebook
- automatically enabled
- v1.6 adds modern ipython support
- overwritable log functions in order to integrate the log with a GUI
The additional features that the FileLogger
has beyond SimpleLogger
:
- file writing and/or stream writing
- set_enabled / enable / disable logger
- context manager to close file
with FileLogger(level='debug', filename=None, include_stream=True) as log: log.debug('SimpleLogger') with FileLogger(level='debug', filename='file.log', include_stream=True) as log: log.debug('FileLogger/SimpleLogger') with FileLogger(level='debug', filename='file.log', include_stream=False) as log: log.debug('FileLogger')
The WarningRedirector works as a context manager with both the FileLogger
has beyond SimpleLogger
to redirect other libraries warnings to the logging object.
- using the
SimpleLogger
:log = get_logger2(debug=True, encoding='utf-8') warnings.warn('this goes to stderr') with WarningRedirector(log) as warn: warnings.warn('this goes to cpylog')
- using the
FileLogger
:with FileLogger(level='debug', filename=None, include_stream=True) as log: warnings.warn('this goes to stderr') with WarningRedirector(log) as warn: warnings.warn('this goes to cpylog')
As a bonus (limitation?), it crashes when you have invalid logging statement. This ensures that logging is correct, so if you switch to standard Python logging, that will also be correct. One of the goals of this logging class is that because it implements a subset of standard Python logging, you can replace it with a standard Python log.
from cpylog import get_logger, get_logger2
# if a log already exists, it's passed through
log0 = None
# level: debug, info, warning, critical, exception
log1 = get_logger(log=log0, level='debug', encoding='utf-8')
log1.debug('debug')
log1.info('info')
log1.warning('warning')
log1.exception('exception')
log1.critical('critical')
DEBUG: file.py:4 debug
INFO: file.py:5 info
WARNING: file.py:6 warning
EXCEPTION: file.py:7 exception
CRITICAL: file.py:8 critical
# debug: True=debug, False=info, None=warning
log2 = get_logger2(log=log1, debug=True, encoding='utf-8')
SimpleLogger
is the base class and if we call it directly, we can overwrite the logging message style.
from cpylog import SimpleLogger
log_base = SimpleLogger(self, level: str='debug', encoding: str='utf-8', log_func=None)
# we can call it with an external function, so you can make a custom formatter
# such as an HTML logger
def log_func(typ, filename, n, msg):
print('typ=%r filename=%r n=%r msg=%r' % (typ, filename, n, msg))
log_func = SimpleLogger(level='info', log_func=log_func)
Main/dev