law.logger#

Law logging setup.

get_logger(*args, skip_setup=False, **kwargs)[source]#

Replacement for logging.getLogger that makes sure that the custom Logger class is used when new loggers are created and that the logger is properly set up by setup_logger().

setup_logging()[source]#

Sets up the internal law loggers as well as all other loggers listed in the "logging" config section as (name, level) pairs. This includes loggers that do not use the "law.*" namespace which can be seen as a convenient feature to set up custom loggers.

setup_logger(logger, level=None, add_console_handler=None, clear=False, force=False)[source]#

Sets up a logger, optionally given by its name, configures it to have a certain level and adds a preconfigured console handler when add_console_handler is True. When add_console_handler is a dictionary, its items are forwarded as keyword arguments to the create_stream_handler() which handles the handler setup internally. When None, add_console_handler is default to True in case the logger is not a “law” sublogger and has no tty handlers registered yet.

Each logger is setup only once unless force is *True.

level can either be an integer or the name of a level present in the logging module. When no level is given, the level of the "law" base logger is used as a default. When the logger already existed and clear is True, all handlers and filters are removed first. The logger object is returned.

create_stream_handler(handler_kwargs=None, formatter_kwargs=None, formatter_cls=LogFormatter)[source]#

Creates a new StreamHandler instance, passing all handler_kwargs to its constructor, and returns it. When not None, an instance of formatter_cls is created using formatter_kwargs and added to the handler instance.

is_tty_handler(handler)[source]#

Returns True if a logging handler is a StreamHandler which logs to a tty (i.e. stdout or stderr), an IPython OutStream, or a base Handler with a console attribute evaluating to True. The latter check is intended to cover a variety of handlers provided by custom modules.

get_tty_handlers(logger)[source]#

Returns a list of all handlers of a logger that log to a tty.

class Logger(*args, **kwargs)[source]#

Bases: Logger

Custom logger class that adds an additional set of log methods, i.e., debug_once(), info_once(), warning_once(), error_once(), critical_once() and fatal_once(), that log certain messages only once depending on a string identifier.

class LogFormatter(*args, log_template=None, err_template=None, level_styles=None, name_styles=None, msg_styles=None, format_level=None, format_name=None, format_msg=None, **kwargs)[source]#

Bases: Formatter

Configurable formatter class for colored logs. When set, log_template, err_template, level_styles, name_styles, msg_styles, format_level, format_name and format_msg control the log formats and styles on instance level. When None, they default to the upper case class level attributes described below. All args and kwargs are forwarded to logging.Formatter.

classattribute LOG_TEMPLATE#

type: string

Template for log messages without stack traces.

classattribute ERR_TEMPLATE#

type: string

Template for log messages including stack traces.

classattribute LEVEL_STYLES#

type: dict

Style attributes forwarded to law.util.colored() per log level for styling level names in logs

classattribute NAME_STYLES#

type: dict

Style attributes forwarded to law.util.colored() per log level for styling logger names in logs.

classattribute MSG_STYLES#

type: dict

Style attributes forwarded to law.util.colored() per log level for styling messages in logs.

classattribute FORMAT_LEVEL#

type: callable or None

Custom callback to format the log level using the full record.

classattribute FORMAT_NAME#

type: callable, None

Custom callback to format the loger name using the full record.

classattribute FORMAT_MSG#

type: callable, None

Custom callback to format the log message using the full record.