log

Extensions to the standard Python logging system.

Module Contents

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(→ tqdm.tqdm[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.

Attributes

T

LEVELS

NO_DEBUG_LOGGING_MODULES

default_output_stream

pretty_cli

console_logs

__null_handler_set

e3_debug_logger

debug

log.T
log.LEVELS
log.NO_DEBUG_LOGGING_MODULES = ['boto3', 'botocore', 'requests', 'requests_cache', 'urllib3']
log.default_output_stream: TextIO | IO[str]
log.pretty_cli
log.console_logs: str | None
class log.JSONFormatter(date_fmt: str | None = None, context: collections.abc.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: ClassVar[list[str]] = ['asctime', 'levelname', 'name', 'message', 'module', 'exc_text']
_extra_attr: ClassVar[list[str]] = ['anod_uui']
format(record: logging.LogRecord) str

Convert record into JSON.

Parameters:

record – logging record

class log.E3LoggerAdapter(logger, extra=None)

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

Parameters:
  • msg – log message

  • kwargs – keyword arguments

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

  • msg – log message

  • 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

Parameters:
  • msg – log message

  • args – see logging module

  • anod_uui – Anod UUI

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

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

Wrap standard logger.debug method.

It allows adding extra keyword parameters

Parameters:
  • msg – log message

  • args – see logging module

  • anod_uui – Anod UUI

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

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

Wrap standard logger.warning method.

It allows adding extra keyword parameters

Parameters:
  • msg – log message

  • args – see logging module

  • anod_uui – Anod UUI

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

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

Wrap standard logger.error method.

It allows adding extra keyword parameters

Parameters:
  • msg – log message

  • args – see logging module

  • anod_uui – Anod UUI

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

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

Wrap of standard logger.critical method.

It allows adding extra keyword parameters

Parameters:
  • msg – log message

  • args – see logging module

  • anod_uui – Anod UUI

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

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

Parameters:
  • msg – log message

  • args – see logging module

  • exc_info – exception info (default: True)

  • anod_uui – Anod UUI

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

log.progress_bar(it: collections.abc.Iterator[T] | collections.abc.Sequence[T], **kwargs: Any) tqdm.tqdm[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 with colors.

Parameters:

record – logging record

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.

  • set_default_output – if True, set default_output_stream to the file handler stream when filename is not None

  • json_format – if True, use JSON formatter for log output

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