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 = {logging.DEBUG: '\x1b[38;5;244m', logging.WARNING: '\x1b[38;5;166m', logging.ERROR: '\x1b[1;38;5;196m', logging.CRITICAL: '\x1b[1;4;38;5;196m'}

_OWN_PREFIX module-attribute

_OWN_PREFIX = re.compile('(tdfs4ds(?:\\.\\w+)*): ')

LevelColorFormatter

Bases: Formatter

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

format

format(record)

_OncePerRecord

Bases: Filter

Lets a record through once per output, however many handlers receive it.

A terminal shows stdout and stderr together, so the two are one output here: a record already written to either is not written to the other. A file or another stream is its own output. It makes "one record, two handlers" harmless whatever installs the handlers.

__init__

__init__(handler)

filter

filter(record)

ConsoleHandler

Bases: StreamHandler

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

__init__

__init__(*args, **kwargs)

emit

emit(record)

_StripNamePrefix

Bases: Filter

Drops a message's own tdfs4ds.x: prefix when the format already prints the logger name.

Many server messages start with "tdfs4ds.serve: ..."; the server's format adds %(name)s: too, which printed tdfs4ds.serve.bootstrap: tdfs4ds.serve: .... Only a prefix that the record's own logger name already shows is removed.

filter

filter(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.

_add_once

_add_once(handler)

_console_handlers

_console_handlers(lg)

_console_handler

_console_handler(lg)

dedupe_stream_handlers

dedupe_stream_handlers(*loggers)

Put the once-per-record guard on every stream handler of the root logger and of loggers.

Called by the server after it has set up its logging: a handler added by anything else (a library, the host application) writing to the same terminal then cannot repeat a line.

strip_name_prefix

strip_name_prefix(*loggers)

Put :class:_StripNamePrefix on the stream handlers of the root logger and of loggers (the server's handlers, whose format prints the logger name).

describe_handlers

describe_handlers(names=('', 'tdfs4ds', 'tdfs4ds.serve', 'uvicorn', 'uvicorn.error', 'uvicorn.access', 'gradio')) -> str

The handler layout, one line per logger (for a DEBUG line when a copy shows twice).

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: with LOG_PROPAGATE off, exactly one console handler; on, none (every one is removed), so a record can never be written by this logger's own handler AND by the root handler.