mt.logg
Customised logging.
This module extends Python’s package logging with some customisation made specifically for MT’s code. Instead of:
import logging
You do:
from mt import logg
It will import the logging package plus the additional stuff implemented here.
We use acronym logg instead of log to avoid naming conflict with the mathematical log function. In addition, the logging functions critical, error, warning, warn, info and debug have their API extended a bit.
Please see Python package logging for more details.
Functions
make_logger()
: Make a singleton logger.prepare_file_handler()
: Prepares a file handler for logging.init()
: Initialises the module if it has not been initialised.with_logger()
: Wrapper that adds keyword ‘logger=loger’ to the input function.log()
: Wrapslogging.log()
with additional logger keyword.critical()
: Wrapslogging.critical()
with additional logger keyword.error()
: Wrapslogging.error()
with additional logger keyword.info()
: Wrapslogging.info()
with additional logger keyword.warning()
: Wrapslogging.warning()
with additional logger keyword.warn()
: Wrapslogging.warning()
with additional logger keyword.debug()
: Wrapslogging.debug()
with additional logger keyword.scoped_log()
: Scoped log function that can be used in a with statement.scoped_log_if()
: Undocumented.
- mt.logg.make_logger(logger_name, max_indent=10)
Make a singleton logger.
- Parameters:
- logger_namestr
name of the logger
- max_indentint
max number of indents. Default to 10.
The generated logger has 2 handlers, one standard output and one watched file handler. Both handlers threshold at DEBUG. The standard handler further thresholds at max_indent. The debug handler thresholds at DEBUG level. The standard handler is brief, not showing thread id. The debug level is verbose, showing everything.
- mt.logg.prepare_file_handler(prefix='ml', filepath=None)
Prepares a file handler for logging.
- mt.logg.init()
Initialises the module if it has not been initialised.
- mt.logg.with_logger(func, logger: IndentedLoggerAdapter | None = None)
Wrapper that adds keyword ‘logger=loger’ to the input function.
- mt.logg.log(level: int, msg: str | bytes, logger: ~logging.Logger | None = <IndentedLoggerAdapter mtbase (DGB4)>, *args, **kwargs)
Wraps
logging.log()
with additional logger keyword.- Parameters:
level (int) – level. Passed as-is to
logging.log()
.msg (str or bytes) – message. Passed as-is to
logging.log()
.logger (logging.Logger, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, no message will be logged.
*args (tuple) – positional arguments passed as-is to
logging.log()
.*kwargs (dict) – keyword arguments passed as-is to
logging.log()
.
- mt.logg.critical(msg: str | bytes, logger: ~logging.Logger | None = <IndentedLoggerAdapter mtbase (DGB4)>, *args, **kwargs)
Wraps
logging.critical()
with additional logger keyword.- Parameters:
msg (str or bytes) – message. Passed as-is to
logging.critical()
.logger (logging.Logger, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, no message will be logged.
*args (tuple) – positional arguments passed as-is to
logging.critical()
.*kwargs (dict) – keyword arguments passed as-is to
logging.critical()
.
- mt.logg.error(msg: str | bytes, logger: ~logging.Logger | None = <IndentedLoggerAdapter mtbase (DGB4)>, *args, **kwargs)
Wraps
logging.error()
with additional logger keyword.- Parameters:
msg (str or bytes) – message. Passed as-is to
logging.error()
.logger (logging.Logger, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, no message will be logged.
*args (tuple) – positional arguments passed as-is to
logging.error()
.*kwargs (dict) – keyword arguments passed as-is to
logging.error()
.
- mt.logg.info(msg: str | bytes, logger: ~logging.Logger | None = <IndentedLoggerAdapter mtbase (DGB4)>, *args, **kwargs)
Wraps
logging.info()
with additional logger keyword.- Parameters:
msg (str or bytes) – message. Passed as-is to
logging.info()
.logger (logging.Logger, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, no message will be logged.
*args (tuple) – positional arguments passed as-is to
logging.info()
.*kwargs (dict) – keyword arguments passed as-is to
logging.info()
.
- mt.logg.warning(msg: str | bytes, logger: ~logging.Logger | None = <IndentedLoggerAdapter mtbase (DGB4)>, *args, **kwargs)
Wraps
logging.warning()
with additional logger keyword.- Parameters:
msg (str or bytes) – message. Passed as-is to
logging.warning()
.logger (logging.Logger, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, no message will be logged.
*args (tuple) – positional arguments passed as-is to
logging.warning()
.*kwargs (dict) – keyword arguments passed as-is to
logging.warning()
.
- mt.logg.warn(msg: str | bytes, logger: ~logging.Logger | None = <IndentedLoggerAdapter mtbase (DGB4)>, *args, **kwargs)
Wraps
logging.warning()
with additional logger keyword.- Parameters:
msg (str or bytes) – message. Passed as-is to
logging.warning()
.logger (logging.Logger, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, no message will be logged.
*args (tuple) – positional arguments passed as-is to
logging.warning()
.*kwargs (dict) – keyword arguments passed as-is to
logging.warning()
.
- mt.logg.debug(msg: str | bytes, logger: ~logging.Logger | None = <IndentedLoggerAdapter mtbase (DGB4)>, *args, **kwargs)
Wraps
logging.debug()
with additional logger keyword.- Parameters:
msg (str or bytes) – message. Passed as-is to
logging.debug()
.logger (logging.Logger, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, no message will be logged.
*args (tuple) – positional arguments passed as-is to
logging.debug()
.*kwargs (dict) – keyword arguments passed as-is to
logging.debug()
.
- mt.logg.scoped_log(level, msg: str | bytes, logger: ~mt.logg.IndentedLoggerAdapter | None = <IndentedLoggerAdapter mtbase (DGB4)>, curly: bool = False)
Scoped log function that can be used in a with statement.
Partial functions derived from the function include: scoped_critical, scoped_error, scoped_warning, scoped_warn, scoped_info and scoped_debug.
- Parameters:
level (int) – level. Passed as-is to
ScopeLog
.msg (str or bytes) – message. Passed as-is to
ScopeLog
.logger (IndentedLoggerAdapter, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, a null context is returned.
curly (bool) – whether or not to print a curly bracket. Passed as-is to
ScopeLog
.
- mt.logg.scoped_log_if(cond, func, indented_logger_adapter, level, msg: str | bytes, curly: bool = False, func_args: tuple = (), func_kwargs: dict = {}, return_value_if_false=None)
Classes
IndentedLoggerAdapter
: Logger with indenting capability.ScopedLog
: Scoped-log a message.
- class mt.logg.IndentedLoggerAdapter(logger, extra)
Logger with indenting capability.
Inheritance
digraph inheritance26f252a7e7 { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "IndentedLoggerAdapter" [URL="#mt.logg.IndentedLoggerAdapter",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Logger with indenting capability."]; "LoggerAdapter" -> "IndentedLoggerAdapter" [arrowsize=0.5,style="setlinewidth(0.5)"]; "LoggerAdapter" [fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",tooltip="An adapter for loggers which makes it easier to specify contextual"]; }- critical(msg: str | bytes, *args, **kwargs)
Delegate a critical call to the underlying logger.
- debug(msg: str | bytes, *args, **kwargs)
Delegate a debug call to the underlying logger.
- error(msg: str | bytes, *args, **kwargs)
Delegate an error call to the underlying logger.
- info(msg: str | bytes, *args, **kwargs)
Delegate an info call to the underlying logger.
- process(msg: str | bytes, kwargs)
Process the logging message and keyword arguments passed in to a logging call to insert contextual information. You can either manipulate the message itself, the keyword args or both. Return the message and kwargs modified (or not) to suit your needs.
Normally, you’ll only need to override this one method in a LoggerAdapter subclass for your specific needs.
- warn(msg: str | bytes, *args, **kwargs)
Delegate a warning call to the underlying logger.
- warn_func_move(old_func, new_func)
Warns that an old function has been moved to a new func.
- Parameters:
old_func (str) – short string representing the old function
new_func (str) – short string representing the new function
- warn_last_exception(**kwargs)
Warns last exception, printing out any keyword arguments.
- warn_module_move(old_module, new_module)
Warns that an old module has been moved to a new module.
- Parameters:
old_module (str) – short string representing the old module
new_module (str) – short string representing the new module
- warning(msg: str | bytes, *args, **kwargs)
Delegate a warning call to the underlying logger.
- class mt.logg.ScopedLog(indented_logger_adapter, level, msg: str | bytes, curly: bool = False)
Scoped-log a message.
>>> from mt import logg >>> with logg.ScopedLog(logg.logger, logg.DEBUG, 'hello world'): ... a = 1 ... logg.logger.info("Hi there") hello world: Hi there
Inheritance
digraph inheritance8646a69bcf { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "ScopedLog" [URL="#mt.logg.ScopedLog",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Scoped-log a message."]; }
Variables
- mt.logg.logger
Logger with indenting capability.
<IndentedLoggerAdapter mtbase (Level 1)>
- mt.logg.scoped_critical
Scoped log function that can be used in a with statement.
Partial functions derived from the function include: scoped_critical, scoped_error, scoped_warning, scoped_warn, scoped_info and scoped_debug.
- Parameters:
level (int) – level. Passed as-is to
ScopeLog
.msg (str or bytes) – message. Passed as-is to
ScopeLog
.logger (IndentedLoggerAdapter, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, a null context is returned.
curly (bool) – whether or not to print a curly bracket. Passed as-is to
ScopeLog
.
functools.partial(<function scoped_log at 0x7f2f44e94430>, 50)
- mt.logg.scoped_error
Scoped log function that can be used in a with statement.
Partial functions derived from the function include: scoped_critical, scoped_error, scoped_warning, scoped_warn, scoped_info and scoped_debug.
- Parameters:
level (int) – level. Passed as-is to
ScopeLog
.msg (str or bytes) – message. Passed as-is to
ScopeLog
.logger (IndentedLoggerAdapter, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, a null context is returned.
curly (bool) – whether or not to print a curly bracket. Passed as-is to
ScopeLog
.
functools.partial(<function scoped_log at 0x7f2f44e94430>, 40)
- mt.logg.scoped_info
Scoped log function that can be used in a with statement.
Partial functions derived from the function include: scoped_critical, scoped_error, scoped_warning, scoped_warn, scoped_info and scoped_debug.
- Parameters:
level (int) – level. Passed as-is to
ScopeLog
.msg (str or bytes) – message. Passed as-is to
ScopeLog
.logger (IndentedLoggerAdapter, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, a null context is returned.
curly (bool) – whether or not to print a curly bracket. Passed as-is to
ScopeLog
.
functools.partial(<function scoped_log at 0x7f2f44e94430>, 20)
- mt.logg.scoped_warn
Scoped log function that can be used in a with statement.
Partial functions derived from the function include: scoped_critical, scoped_error, scoped_warning, scoped_warn, scoped_info and scoped_debug.
- Parameters:
level (int) – level. Passed as-is to
ScopeLog
.msg (str or bytes) – message. Passed as-is to
ScopeLog
.logger (IndentedLoggerAdapter, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, a null context is returned.
curly (bool) – whether or not to print a curly bracket. Passed as-is to
ScopeLog
.
functools.partial(<function scoped_log at 0x7f2f44e94430>, 30)
- mt.logg.scoped_warning
Scoped log function that can be used in a with statement.
Partial functions derived from the function include: scoped_critical, scoped_error, scoped_warning, scoped_warn, scoped_info and scoped_debug.
- Parameters:
level (int) – level. Passed as-is to
ScopeLog
.msg (str or bytes) – message. Passed as-is to
ScopeLog
.logger (IndentedLoggerAdapter, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, a null context is returned.
curly (bool) – whether or not to print a curly bracket. Passed as-is to
ScopeLog
.
functools.partial(<function scoped_log at 0x7f2f44e94430>, 30)
- mt.logg.scoped_debug
Scoped log function that can be used in a with statement.
Partial functions derived from the function include: scoped_critical, scoped_error, scoped_warning, scoped_warn, scoped_info and scoped_debug.
- Parameters:
level (int) – level. Passed as-is to
ScopeLog
.msg (str or bytes) – message. Passed as-is to
ScopeLog
.logger (IndentedLoggerAdapter, optional) – which logger to process the message. Default is the default logger of mtbase. If None is provided, a null context is returned.
curly (bool) – whether or not to print a curly bracket. Passed as-is to
ScopeLog
.
functools.partial(<function scoped_log at 0x7f2f44e94430>, 10)