Oslo Logging Library
oslo.log is a Python library that provides standardized logging configuration for OpenStack projects. It offers custom formatters, handlers, and support for context-specific logging, enhancing the standard Python logging module. The current version is 8.1.0 and it's actively maintained by the OpenStack community with regular releases.
Warnings
- gotcha When `log_config_append` is specified, all other logging configuration options (e.g., `log_date_format`, `log_file`, `use_syslog`, `default_log_levels`) are ignored. Logging is fully controlled by the specified configuration file.
- gotcha Changing log format strings can be tricky, especially distinguishing between `logging_context_format_string` and `logging_default_format_string`. `log.set_defaults()` only affects the context-based format string.
- deprecated The existing syslog format used by `oslo.log` is deprecated and will be changed to honor RFC5424 in future versions.
Install
-
pip install oslo.log
Imports
- log
from oslo_log import log
- cfg
from oslo_config import cfg
Quickstart
import os
from oslo_config import cfg
from oslo_log import log as logging
# Define a configuration group and options (typically done in a separate config file)
CONF = cfg.CONF
CONF.register_group(cfg.OptGroup('DEFAULT'))
# Register oslo.log options (required for oslo.log to pick up config)
logging.register_options(CONF)
# Set up logging for your application
# The 'product_name' is usually your application's name
# The 'version' is your application's version
DOMAIN = os.environ.get('OSLO_LOG_DOMAIN', 'my_app') # Example for context
logging.setup(CONF, DOMAIN, version='1.0.0')
# Get a logger instance
LOG = logging.getLogger(__name__)
# Log messages
LOG.info("This is an informational message.")
LOG.warning("This is a warning message.")
LOG.error("This is an error message.")
# Example of setting a default log level (if not using a config file)
# This would typically be done via a config file or oslo.config options
# For demonstration, setting it programmatically (ensure it's before logging.setup if permanent)
# logging.set_defaults(default_log_levels=['my_app=DEBUG'])