Examples

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(user='6ce90b4d',
44                       tenant='d6134462',
45                       project_domain='a6b9360e')
46LOG.info("Message with context")
47
48context = context.RequestContext(user='ace90b4d',
49                                 tenant='b6134462',
50                                 project_domain='c6b9360e')
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 = "%(user)s/%(tenant)s@%(project_domain)s"
37logging.setup(CONF, DOMAIN)
38
39LOG = logging.getLogger(__name__)
40
41LOG.info("Message without context")
42# ids in Openstack are 32 characters long
43# For readability a shorter id value is used
44context.RequestContext(request_id='req-abc',
45                       user='6ce90b4d',
46                       tenant='d6134462',
47                       project_domain='a6b9360e')
48LOG.info("Message with context")
Creative Commons Attribution 3.0 License

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