log

Extensions to the standard Python logging system.

Attributes

T

LEVELS

NO_DEBUG_LOGGING_MODULES

default_output_stream

pretty_cli

console_logs

__null_handler_set

e3_debug_logger

debug

Classes

JSONFormatter

Logging formatter for creating JSON logs.

E3LoggerAdapter

LoggerAdapter to add custom keywords.

TqdmHandler

Logging handler when used when progress bars are enabled.

Functions

progress_bar(→ collections.abc.Iterator[T])

Create a tqdm progress bar.

getLogger(→ E3LoggerAdapter)

Get a logger with a default handler doing nothing.

add_log_handlers(→ None)

Add log handlers using GMT.

add_logging_argument_group(→ argparse._ArgumentGroup)

Add an argument group with logging options to the argument parser.

activate_with_args(→ None)

Activate the e3 log using argument parsed.

activate(→ None)

Activate default E3 logging.

Module Contents

log.T
log.LEVELS
log.NO_DEBUG_LOGGING_MODULES = ['boto3', 'botocore', 'requests', 'urllib3']
log.default_output_stream: TextIO | IO[str]
log.pretty_cli
log.console_logs: str | None = None
class log.JSONFormatter(date_fmt: str | None = None, context: Mapping[str, Any] | None = None)

Bases: logging.Formatter

Logging formatter for creating JSON logs.

It will print some standard attributes defined in STD_ATTR plus application extra attributes defined in _extra_attr

STD_ATTR = ['asctime', 'levelname', 'name', 'message', 'module', 'exc_text']
_extra_attr: list[str] = ['anod_uui']
context = None
format(record: logging.LogRecord) str

convert record into JSON.

class log.E3LoggerAdapter(logger, extra=None, merge_extra=False)

Bases: logging.LoggerAdapter

LoggerAdapter to add custom keywords.

process(msg: Any, kwargs: Any) tuple[Any, Any]

Allow to handle extra parameter.

It is called by super method log. It is overwritten here because the standard process method will get rid of extra attribute

log(level: int, msg: Any, *args: Any, anod_uui: int = 0, **kwargs: Any) None

Integrate additional keywords using standard interface.

Parameters:
  • level – see logging module

  • args – see logging module

  • anod_uui – Anod UUI

  • kwargs – other parameter supported by std logger._log method

info(msg: Any, *args: Any, anod_uui: int = 0, **kwargs: Any) None

Wrap standard logger.info method.

It allows adding extra keyword parameters

debug(msg: Any, *args: Any, anod_uui: int = 0, **kwargs: Any) None

Wrap standard logger.debug method.

It allows adding extra keyword parameters

warning(msg: Any, *args: Any, anod_uui: int = 0, **kwargs: Any) None

Wrap standard logger.warning method.

It allows adding extra keyword parameters

error(msg: Any, *args: Any, anod_uui: int = 0, **kwargs: Any) None

Wrap standard logger.error method.

It allows adding extra keyword parameters

critical(msg: Any, *args: Any, anod_uui: int = 0, **kwargs: Any) None

Wrap of standard logger.critical method.

It allows adding extra keyword parameters

exception(msg: Any, *args: Any, exc_info: logging._ExcInfoType = True, anod_uui: int = 0, **kwargs: Any) None

Wrap standard logger.exception method.

It allows adding extra keyword parameters

log.progress_bar(it: collections.abc.Iterator[T] | collections.abc.Sequence[T], **kwargs: Any) collections.abc.Iterator[T]

Create a tqdm progress bar.

Parameters:
  • it – an interator

  • kwargs – see tqdm documentation

Returns:

a tqdm progress bar iterator

log.__null_handler_set
class log.TqdmHandler

Bases: logging.StreamHandler

Logging handler when used when progress bars are enabled.

color_subst
emit(record: logging.LogRecord) None

Emit a record.

If a formatter is specified, it is used to format the record. The record is then written to the stream with a trailing newline. If exception information is present, it is formatted using traceback.print_exception and appended to the stream. If the stream has an ‘encoding’ attribute, it is used to determine how to do the output to the stream.

log.getLogger(name: str | None = None, prefix: str = 'e3') E3LoggerAdapter

Get a logger with a default handler doing nothing.

Calling this function instead of logging.getLogger will avoid warnings such as:

'No handler could be found for logger...'
Parameters:
  • name – logger name, if not specified return the root logger

  • prefix – application prefix, will be prepended to the name

log.add_log_handlers(level: int, log_format: str, datefmt: str | None = None, filename: str | None = None, set_default_output: bool = True, json_format: bool = False) None

Add log handlers using GMT.

Parameters:
  • level – set the root logger level to the specified level

  • log_format – format stream for the log handler

  • datefmt – date/time format for the log handler

  • filename – use of a FileHandler, using the specified filename, instead of a StreamHandler. Set default_output_stream to write in this file.

log.add_logging_argument_group(argument_parser: argparse.ArgumentParser, default_level: int = logging.WARNING) argparse._ArgumentGroup

Add an argument group with logging options to the argument parser.

To be used with e3.log.activate_with_args.

Parameters:
  • argument_parser – the parser in which the group will be created

  • default_level – the logging level that will be used by default

log.activate_with_args(args: argparse.Namespace, default_level: int = logging.WARNING) None

Activate the e3 log using argument parsed.

To be used with e3.log.add_logging_argument_group.

Parameters:
  • args – the result of parsing arguments

  • default_level – the logging level assumed by default

log.activate(stream_format: str = '%(levelname)-8s %(message)s', file_format: str = '%(asctime)s: %(name)-24s: %(levelname)-8s %(message)s', datefmt: str | None = None, level: int = logging.INFO, filename: str | None = None, e3_debug: bool = False, json_format: bool = False) None

Activate default E3 logging.

This function should be called only once. Subsequent calls will result in a no-op.

Parameters:
  • level – set the root logger level to the specified level

  • datefmt – date/time format for the log handler

  • stream_format – format string for the stream handler

  • file_format – format string for the file handler

  • filename – redirect logs to a file in addition to the StreamHandler

  • e3_debug – activate full debug of the e3 library

log.e3_debug_logger
log.debug