mozlog
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
-
RuntimeError: Default logger not initialized
cause This error occurs when a `ProxyLogger` is instantiated or attempts to log before a default `StructuredLogger` has been set up using `mozlog.commandline.setup_logging()` or `mozlog.structuredlog.set_default_logger()`.fixCall `mozlog.commandline.setup_logging()` early in your application's lifecycle to ensure the default logger is properly initialized. For example: `logger = setup_logging()`. -
My MOZ_LOG environment variable isn't affecting my mozlog output.
cause The `MOZ_LOG` environment variable is primarily used for configuring *internal Firefox* logging and is not related to the Python `mozlog` library.fixTo configure the Python `mozlog` library, use its dedicated API (e.g., `setup_logging` parameters, `StructuredLogger` methods) or adjust the handlers/formatters programmatically. The `MOZ_LOG` environment variable has no effect on this Python library. -
TypeError: 'StructuredLogger' object has no attribute 'addHandler'
cause Developers accustomed to Python's `stdlib` `logging` module often try to use `addHandler` or other standard `logging` methods. `mozlog` has a different API and does not provide these methods.fixReview the `mozlog` documentation for its specific methods for adding handlers (`mozlog.commandline.setup_handlers`) and configuring output, which are distinct from the `stdlib` `logging` module. You interact with `mozlog` by calling specific action methods like `info`, `warning`, `test_start`, etc., which accept structured data.
Warnings
- gotcha mozlog is NOT based on Python's standard `logging` module. It uses its own API and internal data model, which can be a common source of confusion for developers expecting `logging` module compatibility.
- gotcha mozlog is NOT process-safe for applications using multiple processes (e.g., via `multiprocessing`). All logging should be arranged to happen in a single process to avoid issues.
- gotcha Using `mozlog.proxy.ProxyLogger` without prior initialization of a default logger (e.g., via `mozlog.commandline.setup_logging()`) will raise a `RuntimeError`.
- gotcha Loggers with the same name in `mozlog` share the same internal state (a 'Borg' pattern). Typically, only one logger object should be instantiated per program.
- gotcha Severity levels used by `mozlog` might not directly map to those used by external logging systems like Google Cloud Logging (GCL), potentially causing incorrect rendering of log records in cloud platforms.
Install
-
pip install mozlog
Imports
- StructuredLogger
from mozlog.structuredlog import StructuredLogger
- setup_logging
from mozlog.commandline import setup_logging
- ProxyLogger
from mozlog.proxy import ProxyLogger
Quickstart
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) ---")