clapper.logging

logging.Logger setup and stream separation.

Functions

setup(logger_name[, format, ...])

Return a logger object that is ready for console logging.

clapper.logging.setup(logger_name, format='[%(levelname)s] %(message)s (%(name)s, %(asctime)s)', low_level_stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, high_level_stream=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, formatter=None)[source]

Return a logger object that is ready for console logging.

Retrieves (as with logging.getLogger()) the given logger, and then attaches 2 handlers (defined on the module) to it:

  1. A logging.StreamHandler to output messages with level equal or lower than logging.INFO to the text-I/O stream low_level_stream. This is implemented by attaching a filter to the respective stream-handler to limit message records at this level.

  2. A logging.StreamHandler to output warning, error messages and above to the text-I/O stream high_level_stream, with an internal level set to logging.WARNING.

If you do not provide a formatter argument, a new formatter, with the format string as defined by the format argument is set on both handlers. In this way, the global logger level can still be controlled from one single place. If output is generated, then it is sent to the right stream.

You can provide a custom formatter if format is not sufficient to customize the looks of your logs (e.g. you want to add colors).

Parameters:
  • logger_name (str) – The name of the module to generate logs for

  • format (str) – The format of the logs, see logging.LogRecord for more details. By default, the log contains the logger name, the log time, the log level and the message. Ignored if ``formatter`` is set.

  • low_level_stream (TextIO) – The stream where to output info messages and below

  • high_level_stream (TextIO) – The stream where to output warning messages and above

  • formatter (Formatter | None) – A formatter to replace the default logging.Formatter.

Return type:

Logger

Returns:

The configured logger. The same logger can be retrieved using the logging.getLogger() function.