# Examples

These files can be found in the doc/source/examples directory of the git source of this project. They can also be found at the online git repository of this project.

## usage_simple.py

     1#!/usr/bin/env python3
     2#
     3# Copyright (c) 2015 OpenStack Foundation
     4#
     5# Licensed under the Apache License, Version 2.0 (the "License"); you may
     6# not use this file except in compliance with the License. You may obtain
     7# a copy of the License at
     8#
     9#   http://www.apache.org/licenses/LICENSE-2.0
    10#
    11# Unless required by applicable law or agreed to in writing, software
    12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    14# License for the specific language governing permissions and limitations
    15# under the License.
    16
    17"""A simple usage example of Oslo Context
    18
    19This example requires the following modules to be installed.
    20
    21$ pip install oslo.context oslo.log
    22
    23More information can be found at:
    24
    25  https://docs.openstack.org/oslo.context/latest/user/usage.html
    26"""
    27
    28from oslo_config import cfg
    29from oslo_context import context
    30from oslo_log import log as logging
    31
    32CONF = cfg.CONF
    33DOMAIN = "demo"
    34
    35logging.register_options(CONF)
    36logging.setup(CONF, DOMAIN)
    37
    38LOG = logging.getLogger(__name__)
    39
    40LOG.info("Message without context")
    41context.RequestContext()
    42LOG.info("Message with context")

## usage.py

     1#!/usr/bin/env python3
     2#
     3# Copyright (c) 2015 OpenStack Foundation
     4#
     5# Licensed under the Apache License, Version 2.0 (the "License"); you may
     6# not use this file except in compliance with the License. You may obtain
     7# a copy of the License at
     8#
     9#   http://www.apache.org/licenses/LICENSE-2.0
    10#
    11# Unless required by applicable law or agreed to in writing, software
    12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    14# License for the specific language governing permissions and limitations
    15# under the License.
    16
    17"""A representative usage example of Oslo Context
    18
    19This example requires the following modules to be installed.
    20
    21$ pip install oslo.context oslo.log
    22
    23More information can be found at:
    24
    25  https://docs.openstack.org/oslo.context/latest/user/usage.html
    26"""
    27
    28from oslo_config import cfg
    29from oslo_context import context
    30from oslo_log import log as logging
    31
    32CONF = cfg.CONF
    33DOMAIN = "demo"
    34
    35logging.register_options(CONF)
    36logging.setup(CONF, DOMAIN)
    37
    38LOG = logging.getLogger(__name__)
    39
    40LOG.info("Message without context")
    41# ids in Openstack are 32 characters long
    42# For readability a shorter id value is used
    43context.RequestContext(
    44    user_id='6ce90b4d', project_id='d6134462', project_domain_id='a6b9360e'
    45)
    46LOG.info("Message with context")
    47
    48context = context.RequestContext(
    49    user_id='ace90b4d', project_id='b6134462', project_domain_id='c6b9360e'
    50)
    51LOG.info("Message with passed context", context=context)

## usage_user_identity.py

     1#!/usr/bin/env python3
     2#
     3# Copyright (c) 2015 OpenStack Foundation
     4#
     5# Licensed under the Apache License, Version 2.0 (the "License"); you may
     6# not use this file except in compliance with the License. You may obtain
     7# a copy of the License at
     8#
     9#   http://www.apache.org/licenses/LICENSE-2.0
    10#
    11# Unless required by applicable law or agreed to in writing, software
    12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    14# License for the specific language governing permissions and limitations
    15# under the License.
    16
    17"""A usage example of Oslo Context with user_identity
    18
    19This example requires the following modules to be installed.
    20
    21$ pip install oslo.context oslo.log
    22
    23More information can be found at:
    24
    25  https://docs.openstack.org/oslo.context/latest/user/index.html
    26"""
    27
    28from oslo_config import cfg
    29from oslo_context import context
    30from oslo_log import log as logging
    31
    32CONF = cfg.CONF
    33DOMAIN = "demo"
    34
    35logging.register_options(CONF)
    36CONF.logging_user_identity_format = (
    37    "%(user_id)s/%(project_id)s@%(project_domain)s"
    38)
    39logging.setup(CONF, DOMAIN)
    40
    41LOG = logging.getLogger(__name__)
    42
    43LOG.info("Message without context")
    44# ids in Openstack are 32 characters long
    45# For readability a shorter id value is used
    46context.RequestContext(
    47    request_id='req-abc',
    48    user_id='6ce90b4d',
    49    project_id='d6134462',
    50    project_domain_id='a6b9360e',
    51)
    52LOG.info("Message with context")

this page last updated: 2026-07-14 03:05:54

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.

found an error? report a bug