Oslo Logging Library

8.1.0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates the basic setup and usage of oslo.log. It involves importing `cfg` from `oslo_config` to manage configuration options, registering `oslo.log`'s options, and then initializing the logging system with `logging.setup()`. Finally, it shows how to obtain a logger and emit messages at different levels.

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'])

view raw JSON →