mlrl.testbed.log.log module

Author: Michael Rapp (michael.rapp.ml@gmail.com)

Provides classes for writing log messages.

class mlrl.testbed.log.log.IndentationLevel(level: int)

Bases: object

Used to manage the indentation level of log messages.

class Highlighter

Bases: RegexHighlighter

Highlights substrings in log messages:

  • quoted strings: “hello”

  • durations: 1 minute, 30 seconds

  • numbers: 123, 1,234, 1.5

REGEX_DURATION = '\\d+ (?:day|hour|minute|second|millisecond)s?'
REGEX_NUMBER = '\\d{1,3}(?:,\\d{3})*(?:\\.\\d+)?|\\d+(?:\\.\\d+)?'
REGEX_QUOTED = '"[^"]*"'
highlights: ClassVar[Sequence[str]] = ['(?P<bold>"[^"]*")|(?P<turquoise2>\\d+ (?:day|hour|minute|second|millisecond)s?|\\d{1,3}(?:,\\d{3})*(?:\\.\\d+)?|\\d+(?:\\.\\d+)?)']
class IndentedRenderable(renderable: ConsoleRenderable, level: int)

Bases: object

Wraps a ConsoleRenderable and adds indentation, depending on the current level.

PREFIX_STYLE = Style(color=Color('grey50', ColorType.EIGHT_BIT, number=244))
static decorate_with_box(renderable: ConsoleRenderable, box_title: str | None = None) ConsoleRenderable
decrease()

Decreases the indentation level.

static get_prefix(level: int, prefix: str = '│') str

Returns the prefix to be used for log messages at a specific indentation level.

Parameters:
  • level – The indentation level

  • prefix – A text that should be printed before the log message

Returns:

A prefix

increase()

Increase the indentation level.

print(message: str | ConsoleRenderable, style: Style | None = None, box: bool = False, box_title: str | None = None, highlight: bool = False)
class mlrl.testbed.log.log.Log

Bases: object

Allows to write log messages.

static error(message: str | ConsoleRenderable, error: Exception | None = None, box: bool = False, box_title: str | None = None, highlight: bool = False)

Writes a log message at level Log.Level.ERROR and terminates the build system.

Parameters:
  • message – The log message to be written

  • error – An optional error to be included in the log message

  • box – True, if a box should be surrounded by a box, False otherwise

  • box_title – An optional title to be printed at the top of the box surrounding the log message

  • highlight – True, if certain values in the log message should be highlighted, False otherwise

static indented()

A context manager that indents all log messages emitted within the block. Nesting multiple context managers results in deeper indentation levels.

static info(message: str | ConsoleRenderable, box: bool = False, box_title: str | None = None, highlight: bool = False)

Writes a log message at level Log.Level.INFO.

Parameters:
  • message – The log message to be written

  • box – True, if a box should be surrounded by a box, False otherwise

  • box_title – An optional title to be printed at the top of the box surrounding the log message

  • highlight – True, if certain values in the log message should be highlighted, False otherwise

static separator(title: str)

Writes a log messages that acts as a separator with a specific title.

Parameters:

title – The title to be used

static source_code(source_code: str, language: str, box_title: str | None = None)

Writes a log message containing source code in a specific language at level Log.Level.INFO.

Parameters:
  • source_code – The source code to be written

  • language – The language used by the source code

  • box_title – An optional title to be printed at the top of the box surrounding the log message

static success(message: str, box: bool = False, box_title: str | None = None, highlight: bool = False)

Writes a log message at level Log.Level.INFO indicating successful operation of an operation.

Parameters:
  • message – The log message to be written

  • box – True, if a box should be surrounded by a box, False otherwise

  • box_title – An optional title to be printed at the top of the box surrounding the log message

  • highlight – True, if certain values in the log message should be highlighted, False otherwise

static verbose(message: str | ConsoleRenderable, box: bool = False, box_title: str | None = None, highlight: bool = False)

Writes a log message at level Log.Level.VERBOSE.

Parameters:
  • message – The log message to be written

  • box – True, if a box should be surrounded by a box, False otherwise

  • box_title – An optional title to be printed at the top of the box surrounding the log message

  • highlight – True, if certain values in the log message should be highlighted, False otherwise

static warning(message: str | ConsoleRenderable, box: bool = False, box_title: str | None = None, highlight: bool = False)

Writes a log message at level Log.Level.WARNING.

Parameters:
  • message – The log message to be written

  • box – True, if a box should be surrounded by a box, False otherwise

  • box_title – An optional title to be printed at the top of the box surrounding the log message

  • highlight – True, if certain values in the log message should be highlighted, False otherwise

class mlrl.testbed.log.log.LogHandler

Bases: RichHandler

Customizes the appearance of log messages emitted by Python’s “logging” module, depending on the log level.

STYLE_PER_LOG_LEVEL = {10: Style(color=Color('grey50', ColorType.EIGHT_BIT, number=244)), 30: Style(color=Color('yellow', ColorType.STANDARD, number=3)), 40: Style(color=Color('red', ColorType.STANDARD, number=1), bold=True)}
SYMBOL_PER_LOG_LEVEL = {10: '○', 30: '⚠', 40: '✗'}
static format_message(message: str | ConsoleRenderable, log_level: int) str | ConsoleRenderable

Formats a given log message, depending on a given log level.

Parameters:
  • message – The log message to be formatted

  • log_level – The log level

Returns:

The formatted message

static get_style(log_level: int) Style | None

Returns the style to be used for a given log level.

Parameters:

log_level – The log level

Returns:

The style to be used

render_message(record: LogRecord, message: str) ConsoleRenderable

See rich.logging.RichHandler.render_message()

mlrl.testbed.log.log.disable_log()

Prevents any output from being written to stdout or stderr.

mlrl.testbed.log.log.get_console() Console

Returns the console to be used for logging.