trlcdoc.log

Console output of trlcdoc: logging setup, verbosity levels and redaction of secrets.

All trlcdoc modules log with logging.getLogger(__name__), below the ‘trlcdoc’ logger configured here. INFO is the default output, DEBUG (-v) adds decisions and tool details, TRACE (-vv) adds per-item details.

Attributes

TRACE

LOGGER_NAME

VERBOSITY_LEVELS

SECRET_NAME

REDACTED

Classes

ConsoleHandler

Write INFO and below to stdout, WARNING and above to stderr.

LevelFormatter

Format a message unchanged, and prefix debug and trace messages with their level name.

Functions

setup_logging([verbosity]) → None

Configure the trlcdoc logger for the console.

redact_url(text) → str

Replace the user and password of a URL, e.g. of a PlantUML server.

redact_env(env) → dict[str, str]

Replace the secrets of environment variables.

command_line(cmd) → str

Format a command so it can be pasted into a shell of the system.

Module Contents

trlcdoc.log.TRACE = 5
trlcdoc.log.LOGGER_NAME = 'trlcdoc'
trlcdoc.log.VERBOSITY_LEVELS
trlcdoc.log.SECRET_NAME
trlcdoc.log.REDACTED = '***'
class trlcdoc.log.ConsoleHandler(level=NOTSET)[source]

Bases: logging.Handler

Inheritance diagram of trlcdoc.log.ConsoleHandler

Write INFO and below to stdout, WARNING and above to stderr.

The stream is looked up for every message instead of once, so output redirected later, e.g. by pytest, receives the messages. Every message is flushed, so it appears before the output of a tool started next.

emit(record: logging.LogRecord) → None[source]

Write a message to its stream.

Parameters:

record (logging.LogRecord) – The message.

class trlcdoc.log.LevelFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]

Bases: logging.Formatter

Inheritance diagram of trlcdoc.log.LevelFormatter

Format a message unchanged, and prefix debug and trace messages with their level name.

The default output keeps its plain text, the verbose lines are recognizable.

format(record: logging.LogRecord) → str[source]

Format a message.

Parameters:

record (logging.LogRecord) – The message.

Returns:

The message, with ‘DEBUG: ‘ or ‘TRACE: ‘ in front for verbose messages.

Return type:

str

trlcdoc.log.setup_logging(verbosity: int = 0) → None[source]

Configure the trlcdoc logger for the console.

It can be called again, e.g. for each run in the tests: the handler is replaced, not added.

Parameters:

verbosity (int) – Number of -v options: 0 for the default output, 1 for DEBUG, 2 or more for TRACE.

trlcdoc.log.redact_url(text: str) → str[source]

Replace the user and password of a URL, e.g. of a PlantUML server.

Parameters:

text (str) – A URL or any other text.

Returns:

The text, with *** instead of the user info if it is a URL with one.

Return type:

str

trlcdoc.log.redact_env(env: dict[str, str]) → dict[str, str][source]

Replace the secrets of environment variables.

Parameters:

env (dict[str, str]) – Environment variables.

Returns:

A copy with *** as value of variables named like a secret, and URLs without user info.

Return type:

dict[str, str]

trlcdoc.log.command_line(cmd: list[str]) → str[source]

Format a command so it can be pasted into a shell of the system.

Parameters:

cmd (list[str]) – Command and its arguments.

Returns:

The command line, quoted for cmd.exe on Windows and for a POSIX shell elsewhere.

Return type:

str