Examples

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

python_logging.py

 1# Copyright (c) 2016 OpenStack Foundation
 2#
 3# Licensed under the Apache License, Version 2.0 (the "License"); you may
 4# not use this file except in compliance with the License. You may obtain
 5# a copy of the License at
 6#
 7#   http://www.apache.org/licenses/LICENSE-2.0
 8#
 9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15"""A syntax example of Python Logging"""
16
17import logging
18
19LOG = logging.getLogger(__name__)
20
21# Define a default handler at INFO logging level
22logging.basicConfig(level=logging.INFO)
23
24LOG.info("Python Standard Logging")
25LOG.warning("Python Standard Logging")
26LOG.error("Python Standard Logging")

oslo_logging.py

# Copyright (c) 2016 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""A minimal syntax example of Oslo Logging"""

from oslo_config import cfg
from oslo_log import log as logging

LOG = logging.getLogger(__name__)
CONF = cfg.CONF
DOMAIN = "demo"

logging.register_options(CONF)
logging.setup(CONF, DOMAIN)

# Oslo Logging uses INFO as default
LOG.info("Oslo Logging")
LOG.warning("Oslo Logging")
LOG.error("Oslo Logging")

usage.py

 1# Copyright (c) 2016 OpenStack Foundation
 2#
 3# Licensed under the Apache License, Version 2.0 (the "License"); you may
 4# not use this file except in compliance with the License. You may obtain
 5# a copy of the License at
 6#
 7#   http://www.apache.org/licenses/LICENSE-2.0
 8#
 9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15"""A usage example of Oslo Logging
16
17This example requires the following package to be installed.
18
19$ pip install oslo.log
20
21Additional Oslo packages installed include oslo.config, oslo.context,
22oslo.i18n, oslo.serialization and oslo.utils.
23
24More information about Oslo Logging can be found at:
25
26  https://docs.openstack.org/oslo.log/latest/user/index.html
27"""
28
29from oslo_config import cfg
30from oslo_log import log as logging
31
32LOG = logging.getLogger(__name__)
33CONF = cfg.CONF
34DOMAIN = 'demo'
35
36
37def prepare():
38    """Prepare Oslo Logging (2 or 3 steps)
39
40    Use of Oslo Logging involves the following:
41
42    * logging.register_options
43    * logging.set_defaults (optional)
44    * logging.setup
45    """
46
47    # Required step to register common, logging and generic configuration
48    # variables
49    logging.register_options(CONF)
50
51    # Optional step to set new defaults if necessary for
52    # * logging_context_format_string
53    # * default_log_levels
54    #
55    # These variables default to respectively:
56    #
57    #  import oslo_log
58    #  oslo_log._options.DEFAULT_LOG_LEVELS
59    #  oslo_log._options.log_opts[0].default
60    #
61
62    extra_log_level_defaults = [
63        'dogpile=INFO',
64        'routes=INFO'
65        ]
66
67    logging.set_defaults(
68        default_log_levels=logging.get_default_log_levels() +
69        extra_log_level_defaults)
70
71    # Required setup based on configuration and domain
72    logging.setup(CONF, DOMAIN)
73
74
75if __name__ == '__main__':
76    prepare()
77    # NOTE: These examples do not demonstration Oslo i18n messages
78
79    LOG.info("Welcome to Oslo Logging")
80    LOG.debug("A debugging message")
81    LOG.warning("A warning occurred")
82    LOG.error("An error occurred")
83    try:
84        raise Exception("This is exceptional")
85    except Exception:
86        LOG.exception("An Exception occurred")

usage_helper.py

  1# Copyright (c) 2016 OpenStack Foundation
  2#
  3# Licensed under the Apache License, Version 2.0 (the "License"); you may
  4# not use this file except in compliance with the License. You may obtain
  5# a copy of the License at
  6#
  7#   http://www.apache.org/licenses/LICENSE-2.0
  8#
  9# Unless required by applicable law or agreed to in writing, software
 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 12# License for the specific language governing permissions and limitations
 13# under the License.
 14
 15"""A usage example with helper debugging of minimum Oslo Logging
 16
 17This example requires the following package to be installed.
 18
 19$ pip install oslo.log
 20
 21Additional Oslo packages installed include oslo.config, oslo.context,
 22oslo.i18n, oslo.serialization and oslo.utils.
 23
 24More information about Oslo Logging can be found at:
 25
 26  https://docs.openstack.org/oslo.log/latest/user/index.html
 27"""
 28
 29# Use default Python logging to display running output
 30import logging as py_logging
 31from oslo_config import cfg
 32from oslo_log import log as logging
 33
 34
 35LOG = py_logging.getLogger(__name__)
 36CONF = cfg.CONF
 37DOMAIN = "demo"
 38
 39
 40def prepare():
 41    """Prepare Oslo Logging (2 or 3 steps)
 42
 43    Use of Oslo Logging involves the following:
 44
 45    * logging.register_options
 46    * logging.set_defaults (optional)
 47    * logging.setup
 48    """
 49
 50    LOG.debug("Prepare Oslo Logging")
 51
 52    LOG.info("Size of configuration options before %d", len(CONF))
 53
 54    # Required step to register common, logging and generic configuration
 55    # variables
 56    logging.register_options(CONF)
 57
 58    LOG.info("Size of configuration options after %d", len(CONF))
 59
 60    # Optional step to set new defaults if necessary for
 61    # * logging_context_format_string
 62    # * default_log_levels
 63    #
 64    # These variables default to respectively:
 65    #
 66    #  import oslo_log
 67    #  oslo_log._options.DEFAULT_LOG_LEVELS
 68    #  oslo_log._options.log_opts[0].default
 69    #
 70
 71    custom_log_level_defaults = logging.get_default_log_levels() + [
 72        'dogpile=INFO',
 73        'routes=INFO'
 74        ]
 75
 76    logging.set_defaults(default_log_levels=custom_log_level_defaults)
 77
 78    # NOTE: We cannot show the contents of the CONF object
 79    # after register_options() because accessing this caches
 80    # the default_log_levels subsequently modified with set_defaults()
 81    LOG.info("List of Oslo Logging configuration options and current values")
 82    LOG.info("=" * 80)
 83    for c in CONF:
 84        LOG.info("%s = %s" % (c, CONF[c]))
 85    LOG.info("=" * 80)
 86
 87    # Required setup based on configuration and domain
 88    logging.setup(CONF, DOMAIN)
 89
 90
 91if __name__ == '__main__':
 92    py_logging.basicConfig(level=py_logging.DEBUG)
 93
 94    prepare()
 95    # NOTE: These examples do not demonstration Oslo i18n messages
 96    LOG.info("Welcome to Oslo Logging")
 97    LOG.debug("A debugging message")
 98    LOG.warning("A warning occurred")
 99    LOG.error("An error occurred")
100    try:
101        raise Exception("This is exceptional")
102    except Exception:
103        LOG.exception("An Exception occurred")

usage_context.py

 1# Copyright (c) 2016 OpenStack Foundation
 2#
 3# Licensed under the Apache License, Version 2.0 (the "License"); you may
 4# not use this file except in compliance with the License. You may obtain
 5# a copy of the License at
 6#
 7#   http://www.apache.org/licenses/LICENSE-2.0
 8#
 9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15"""A usage example of Oslo Logging with context
16
17This example requires the following package to be installed.
18
19$ pip install oslo.log
20
21Additional Oslo packages installed include oslo.config, oslo.context,
22oslo.i18n, oslo.serialization and oslo.utils.
23
24More information about Oslo Logging can be found at:
25
26  https://docs.openstack.org/oslo.log/latest/user/index.html
27  https://docs.openstack.org/oslo.context/latest/user/index.html
28"""
29
30from oslo_config import cfg
31from oslo_context import context
32from oslo_log import log as logging
33
34LOG = logging.getLogger(__name__)
35CONF = cfg.CONF
36DOMAIN = 'demo'
37
38
39def prepare():
40    """Prepare Oslo Logging (2 or 3 steps)
41
42    Use of Oslo Logging involves the following:
43
44    * logging.register_options
45    * logging.set_defaults (optional)
46    * logging.setup
47    """
48
49    # Required step to register common, logging and generic configuration
50    # variables
51    logging.register_options(CONF)
52
53    # Optional step to set new defaults if necessary for
54    # * logging_context_format_string
55    # * default_log_levels
56    #
57    # These variables default to respectively:
58    #
59    #  import oslo_log
60    #  oslo_log._options.DEFAULT_LOG_LEVELS
61    #  oslo_log._options.log_opts[0].default
62    #
63
64    extra_log_level_defaults = [
65        'dogpile=INFO',
66        'routes=INFO'
67        ]
68
69    logging.set_defaults(
70        default_log_levels=logging.get_default_log_levels() +
71        extra_log_level_defaults)
72
73    # Required setup based on configuration and domain
74    logging.setup(CONF, DOMAIN)
75
76
77if __name__ == '__main__':
78    prepare()
79
80    LOG.info("Welcome to Oslo Logging")
81    LOG.info("Without context")
82    context.RequestContext(user='6ce90b4d',
83                           tenant='d6134462',
84                           domain='a6b9360e')
85    LOG.info("With context")