Logging
Mountaineer bundles a mountaineer.logging
module for service-level logs. To adjust
the verbosity of Mountaineer's logs, set the MOUNTAINEER_LOG_LEVEL
environment
variable.
MOUNTAINEER_LOG_LEVEL=WARN mountaineer runserver
We also provide some helper functions to use our same logging convention in your own code. We optimize for structured logging, where your logs are outputted as a stream of JSON-line formatted logs so they can be quickly parsed by bash pipes or by logging aggregation tools.
setup_logger
Constructor for the main logger used by Mountaineer and optionally for client applications as well. Provided convenient defaults for log level and formatting, alongside coloring of stdout/stderr messages and JSON fields for structured parsing.
Logs are formatted one per line:
Code
{"level": "INFO", "name": "myapp.logging", "timestamp": "2025-02-25 20:40:35,896", "message": "Application started"}
To grep over these logs and filter for level, do:
Code
# Filter for specific log level
grep '"level": "ERROR"' logfile.txt
# Filter logs by service name
grep '"name": "myapp.logging"' logfile.txt
# With jq for more advanced JSON parsing
cat logfile.txt | jq 'select(.level=="ERROR" or .level=="WARNING")'
# Filter by message content
grep -E '"message": ".*database.*"' logfile.txt
Parameters
- Name
name
- Type
- Description
The name of the logger, typically the module name
- Name
log_level
- Type
- Description
Default: logging.DEBUG
The logging level. Defaults to logging.DEBUG to log everything.
Code
from mountaineer.logging import setup_logger import logging # Create a logger for your module logger = setup_logger(__name__, log_level=logging.INFO) # Use the logger logger.info("Application started") logger.warning("Configuration incomplete") logger.error("Failed to connect to database")
log_time_duration
Context manager to time a code block at runtime.
Code
with log_time_duration("Long computation"):
# Simulate work
sleep(10)
Parameters
- Name
message
- Type
- str
- Description