jztools.logging#

Functions

configure_logging_handler([level, ...])

Appends a handler to the specified logging module (the root by default).

get_formatter_class(colored, timezone_aware)

log_exception(logger, error[, level])

log_time(name[, logger, severity])

with log_time(logger, 'operation_name'):

logged_exception(logger, fxn[, level, do_raise])

Classes

AndFilter(*filters)

Logical and of all provided filters.

BlockingFilter(name, level[, msg])

Blocks all messages for the given name (and children) at the specified or lower level.

Filter()

FilterSpec(name, patterns[, level])

ReBlockingFilter(specs)

Finds all messages that match the specified filter and blocks them

TimeZoneFormatter(*args[, timezone])

override logging.Formatter to use an aware datetime object

class jztools.logging.Filter#

Bases: ABC

class jztools.logging.AndFilter(*filters)#

Bases: Filter

Logical and of all provided filters.

class jztools.logging.ReBlockingFilter(specs: List[FilterSpec])#

Bases: Filter

Finds all messages that match the specified filter and blocks them

class jztools.logging.BlockingFilter(name, level, msg=None)#

Bases: Filter

Blocks all messages for the given name (and children) at the specified or lower level. If msg is specified, it is interpreted as a regular expression. Records will further need to have messages matching the regex msg to be blocked.

Usage: handler.addFilter(BlockingFilter(…))

jztools.logging.configure_logging_handler(level: str = 'ERROR', log_filter: Filter | None = None, filename: str | Path | None = None, timezone: timezone | None = None, fmt: str | None = None, datefmt: str | None = None, name: str | None = '')#

Appends a handler to the specified logging module (the root by default). If a filename is specified, this handler will write to that file. Otherwise, it will write to the console.

Note

The level will only be in effect if it is higher than or equal to the level of the root logger. You can set the level of the root logger as follows:

import logging # Import Python's logging module
logging.root.setLevel('INFO')
Parameters:
  • level – Minimum level logged by the created handler.

  • log_filter – An optional filter, e.g., AndFilter or BlockingFilter.

  • filename – If specified, a file handler is created instead of a console handler. If this is a filename, it is used directly as the log output. If a directory, a default filename is created within that directory using the current time (in the datefmt format) as the name.

  • timezone – Log times will be in this timezone (the local timezone by default). If specified, the default datefmt shows the timezone.

  • fmtlogging.Formatter fmt parameter.

  • datefmtlogging.Formatter datefmt parameter.

  • name – The name of the logging module to configure (the root name '' by default).

Return filename:

Returns filename.

jztools.logging.log_time(name, logger=<Logger jztools.logging (WARNING)>, severity=20)#
with log_time(logger, ‘operation_name’):

class jztools.logging.TimeZoneFormatter(*args, timezone: timezone = datetime.timezone(datetime.timedelta(0), 'UTC'), **kwargs)#

Bases: object

override logging.Formatter to use an aware datetime object

https://stackoverflow.com/questions/32402502/how-to-change-the-time-zone-in-python-logging/47104004

Parameters:

timezone – A tzinfo object to convert times to.