mozlog

8.1.0 · active · verified Thu Apr 16

mozlog is a Python library from Mozilla designed for robust, structured logging, particularly within test harnesses and the broader Mozilla ecosystem. It outputs a stream of JSON-compatible objects, making it distinct from the standard `logging` module. As of version 8.1.0, it remains actively maintained with regular updates.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up `mozlog` using `setup_logging` and emit various types of structured log messages. The `setup_logging` function provides a convenient way to initialize the logger with default handlers and formatters, often sending JSON output to stdout.

from mozlog.commandline import setup_logging
from mozlog.structuredlog import StructuredLogger
import sys

# Setup logging, directing output to stdout
# This initializes the default logger that ProxyLogger would use,
# and adds handlers/formatters based on default commandline args.
# For a simple script, you might pass minimal args or defaults.
# setup_logging returns a StructuredLogger instance.
logger = setup_logging()

# Emit a simple info message
logger.info('application_start', data={'version': '1.0.0', 'environment': 'dev'})

# Emit a testsuite start message (common in Mozilla's use case)
logger.testsuite_start('my_test_suite', tests=[{'id': 'test1.html'}, {'id': 'test2.js'}]
)

# Emit a log message with a specific action
logger.log(action='my_custom_event', message='Something happened', details={'key': 'value'})

print("\n--- Raw JSON output on stdout (if default formatter used) ---")

view raw JSON →