mltk.utils.logger

Data logging utilities

See the source code on Github: mltk/utils/logger.py

Functions

add_console_logger(logger[, level])

Add a console logger to the given logger

get_level(level)

Return the logging level as a string

get_logger([name, level, console, log_file, ...])

Get or create a logger, optionally adding a console and/or file handler

make_filelike(logger[, level])

Make the given logger 'file-like'

redirect_stream(logger[, stream, close_atexit])

Redirect std logs to the given logger

set_console_level(logger, level)

Set the logger's console level and return the previous level

timing_decorator(f[, level])

Print the run-time of the decorated function to the logger

get_logger(name='mltk', level='INFO', console=False, log_file=None, log_file_mode='w', parent=None, base_level='DEBUG', file_level='DEBUG')[source]

Get or create a logger, optionally adding a console and/or file handler

Parameters:

parent (Logger) –

add_console_logger(logger, level='INFO')[source]

Add a console logger to the given logger

Parameters:

logger (Logger) –

make_filelike(logger, level=20)[source]

Make the given logger ‘file-like’

Parameters:

logger (Logger) –

redirect_stream(logger, stream='stderr', close_atexit=True)[source]

Redirect std logs to the given logger

NOTE: This redirects ALL logs from the stream

Return type:

Callable

Parameters:
  • logger (Logger) –

  • stream (Union[TextIO, str]) –

timing_decorator(f, level='INFO')[source]

Print the run-time of the decorated function to the logger

If a logger is found in the args then that is used, else if a logger is found in the ‘self’ argument, then that is used

set_console_level(logger, level)[source]

Set the logger’s console level and return the previous level

Return type:

str

Parameters:
  • logger (Logger) –

  • level (str) –

get_level(level)[source]

Return the logging level as a string

Return type:

str

Parameters:

level (Union[str, int]) –

class ConsoleLoggerLevelContext[source]
__init__(logger, level)[source]
Parameters:
  • logger (Logger) –

  • level (str) –

__enter__()[source]
__exit__(type, value, traceback)[source]
__dict__ = mappingproxy({'__module__': 'mltk.utils.logger', '__init__': <function ConsoleLoggerLevelContext.__init__>, '__enter__': <function ConsoleLoggerLevelContext.__enter__>, '__exit__': <function ConsoleLoggerLevelContext.__exit__>, '__dict__': <attribute '__dict__' of 'ConsoleLoggerLevelContext' objects>, '__weakref__': <attribute '__weakref__' of 'ConsoleLoggerLevelContext' objects>, '__doc__': None, '__annotations__': {}})
__module__ = 'mltk.utils.logger'
__weakref__

list of weak references to the object

class DummyLogger[source]
__init__()[source]
debug(*args, **kwargs)[source]
info(*args, **kwargs)[source]
warning(*args, **kwargs)[source]
error(*args, **kwargs)[source]
exception(*args, **kwargs)[source]
write(*args, **kwargs)[source]
flush(*args, **kwargs)[source]
__dict__ = mappingproxy({'__module__': 'mltk.utils.logger', '__init__': <function DummyLogger.__init__>, 'debug': <function DummyLogger.debug>, 'info': <function DummyLogger.info>, 'warning': <function DummyLogger.warning>, 'error': <function DummyLogger.error>, 'exception': <function DummyLogger.exception>, 'write': <function DummyLogger.write>, 'flush': <function DummyLogger.flush>, '__dict__': <attribute '__dict__' of 'DummyLogger' objects>, '__weakref__': <attribute '__weakref__' of 'DummyLogger' objects>, '__doc__': None, '__annotations__': {}})
__module__ = 'mltk.utils.logger'
__weakref__

list of weak references to the object