用法¶
在应用程序中¶
在使用 Python 的标准 logging 库 时,以下最小设置演示了基本的日志记录。
1import logging
2
3LOG = logging.getLogger(__name__)
4
5# Define a default handler at INFO logging level
6logging.basicConfig(level=logging.INFO)
7
8LOG.info("Python Standard Logging")
9LOG.warning("Python Standard Logging")
10LOG.error("Python Standard Logging")
在使用 Oslo Logging 时,以下设置演示了与 Python 标准 logging 相比的语法。
1from oslo_config import cfg
2from oslo_log import log as logging
3
4LOG = logging.getLogger(__name__)
5CONF = cfg.CONF
6DOMAIN = "demo"
7
8logging.register_options(CONF)
9logging.setup(CONF, DOMAIN)
10
11# Oslo Logging uses INFO as default
12LOG.info("Oslo Logging")
13LOG.warning("Oslo Logging")
14LOG.error("Oslo Logging")
Oslo Logging 设置方法¶
应用程序需要使用 oslo.log 配置函数来注册与日志记录相关的配置选项,并在使用标准 logging 函数之前配置根和其他默认 logger。
在解析任何应用程序命令行选项之前,使用 oslo.config CONF 对象调用 register_options()。
1CONF = cfg.CONF
2
3def prepare():
4
5 # Required step to register common, logging and generic configuration
6 # variables
7 logging.register_options(CONF)
如果需要,在设置之前可选地调用 set_defaults() 以更改默认的日志级别。
1 # Optional step to set new defaults if necessary for
2 # * logging_context_format_string
3 # * default_log_levels
4
5 extra_log_level_defaults = [
6 'dogpile=INFO',
7 'routes=INFO'
8 ]
9
10 logging.set_defaults(
11 default_log_levels=logging.get_default_log_levels() +
12 extra_log_level_defaults)
使用注册对象时使用的 oslo.config CONF 对象,以及域和可选版本调用 setup(),以配置应用程序的日志记录。
1DOMAIN = 'demo'
2
3def prepare():
4
5 # Required setup based on configuration and domain
6 logging.setup(CONF, DOMAIN)
Oslo Logging 函数¶
使用标准的 Python logging 函数在适用的日志级别生成日志记录。
1 # NOTE: These examples do not demonstration Oslo i18n messages
2
3 LOG.info("Welcome to Oslo Logging")
4 LOG.debug("A debugging message")
5 LOG.warning("A warning occurred")
6 LOG.error("An error occurred")
7 try:
示例日志输出
2016-01-14 21:07:51.394 12945 INFO __main__ [-] Welcome to Oslo Logging
2016-01-14 21:07:51.395 12945 WARNING __main__ [-] A warning occurred
2016-01-14 21:07:51.395 12945 ERROR __main__ [-] An error occurred
2016-01-14 21:07:51.396 12945 ERROR __main__ [-] An Exception occurred
2016-01-14 21:07:51.396 12945 ERROR __main__ None
2016-01-14 21:07:51.396 12945 ERROR __main__
Oslo Log 翻译¶
从 Pike 版本开始,应用程序中的日志记录不应再使用 Oslo International Utilities (i18n) 标记函数来提供语言翻译功能。
向日志记录添加上下文¶
通过使用 Oslo Context,日志记录还可以包含适用于应用程序的额外上下文信息。
1 LOG.info("Welcome to Oslo Logging")
2 LOG.info("Without context")
3 context.RequestContext(user='6ce90b4d',
4 project='d6134462',
5 domain='a6b9360e')
6 LOG.info("With context")
示例日志输出
2016-01-14 20:04:34.562 11266 INFO __main__ [-] Welcome to Oslo Logging
2016-01-14 20:04:34.563 11266 INFO __main__ [-] Without context
2016-01-14 20:04:34.563 11266 INFO __main__ [req-bbc837a6-be80-4eb2-8ca3-53043a93b78d 6ce90b4d d6134462 a6b9360e - -] With context
不带上下文的日志记录输出格式由 logging_default_format_string 配置变量定义。指定上下文时,使用 logging_context_format_string 配置变量。
Oslo RequestContext 对象包含许多可以在 logging_context_format_string 中指定的属性。应用程序可以扩展此对象以提供可以指定在日志记录中的其他属性。
示例¶
examples/usage.py 提供了一个关于 Oslo Logging 设置的文档示例。
examples/usage_helper.py 提供了一个示例,显示了 Oslo Logging 设置的每个步骤的调试日志记录,详细介绍了配置和每个步骤的日志记录。
examples/usage_context.py 提供了一个关于 Oslo Logging 与 Oslo Context 的文档示例。
在库中¶
oslo.log 主要用于在应用程序中配置日志记录,但它也包含一些在库中可能有用的辅助函数。
getLogger() 包装了 Python 标准库中同名函数,以添加一个 KeywordArgumentAdapter,从而更容易将数据传递给 oslo.log 提供的格式化程序并由应用程序配置。