Rich Console Logging

April 20, 2026 · View on GitHub

mollog ships with rich as a base dependency, exposed as a formatter rather than a handler. Pair it with any handler that writes strings (StreamHandler, FileHandler, ...).

Basic usage

import sys

from mollog import Logger, RichFormatter, StreamHandler

handler = StreamHandler(stream=sys.stderr)
handler.set_formatter(RichFormatter())

logger = Logger("cli")
logger.add_handler(handler)

logger.info("render complete", frame=128)
logger.warning("value outside expected range", field="charge", observed="2+")

Configuration

RichFormatter supports:

  • show_time, show_logger_name, show_extra toggles
  • time_format string (strftime)
  • markup — enable Rich's inline [bold]...[/bold] markup in messages
  • highlighter — swap the default ReprHighlighter used for extras
  • color_system"truecolor", "256", "standard", or None
  • force_terminal — force ANSI output even when the attached stream is not a tty
from mollog import RichFormatter, StreamHandler

formatter = RichFormatter(
    show_time=False,
    color_system="256",
    markup=True,
)

handler = StreamHandler()
handler.set_formatter(formatter)

Writing colored logs to files

Because Rich is a formatter, the same instance works with FileHandler too — the file will contain ANSI escape codes, which tools like less -R and most modern terminals can render:

from mollog import FileHandler, RichFormatter

handler = FileHandler("/var/log/myapp.log")
handler.set_formatter(RichFormatter(color_system="256"))

To log to a file without ANSI codes (most production setups), just use TextFormatter or JSONFormatter on the file handler and keep RichFormatter on the stream handler.