Skip to content

tdfs4ds.log_output

tdfs4ds.log_output

Console output of the tdfs4ds logger: stdout, one colour per level.

Before 0.3.4.2 import tdfs4ds called logging.basicConfig on the ROOT logger. Two consequences, both seen in notebooks:

  • every line went to stderr, which Jupyter shades pink whatever the level, so an INFO looked like an ERROR;
  • basicConfig does nothing once anything else configured the root logger, so in some kernels the lines went through another handler instead (plain black and white). The import order decided which, and the level never did. It also switched on INFO for every other library (httpx's "HTTP Request" lines).

Now the tdfs4ds logger has its own handler: stdout, the historical format, and the level as a colour (DEBUG grey, INFO plain, WARNING orange, ERROR and CRITICAL red). The root logger is left alone.

Settings (module attributes, TDFS4DS_* env / .env / JSON):

  • LOG_COLOR: None = auto (a notebook, or a terminal that shows ANSI colours, and NO_COLOR unset), True / False to force. Read at each line, so changing it takes effect at once.
  • LOG_PROPAGATE: False (default) = tdfs4ds prints its own lines and does not pass them to the root logger, so they are never printed twice. True = no tdfs4ds console output; the lines go to the application's own logging (root handlers, a file, a collector). Applied by configure_logging(), at import and by load_config().

FORMAT module-attribute

FORMAT = '%(asctime)s - %(levelname)s - %(message)s'

DATEFMT module-attribute

DATEFMT = '%Y-%m-%d %H:%M:%S'

_RESET module-attribute

_RESET = '\x1b[0m'

_COLORS module-attribute

_COLORS = {DEBUG: '\x1b[38;5;244m', WARNING: '\x1b[38;5;166m', ERROR: '\x1b[1;38;5;196m', CRITICAL: '\x1b[1;4;38;5;196m'}

LevelColorFormatter

Bases: Formatter

The historical format, the whole line coloured by level when enabled.

format

format(record)

ConsoleHandler

Bases: StreamHandler

Writes to the CURRENT sys.stdout (a notebook or pytest replaces it).

emit

emit(record)

_in_notebook

_in_notebook() -> bool

_terminal_shows_colors

_terminal_shows_colors(stream) -> bool

use_color

use_color(stream=None) -> bool

Whether a line written now to stream (default stdout) gets a colour.

_console_handler

_console_handler(lg)

configure_logging

configure_logging(color=None, propagate=None)

Apply the console settings to the tdfs4ds logger.

color / propagate set tdfs4ds.LOG_COLOR / tdfs4ds.LOG_PROPAGATE first when given. Idempotent: one console handler at most.