自动登录模块的可配置日志模板
clogging的Python项目详细描述
阻塞-自动登录模块的可配置日志模板。
关于
自动登录(https://github.com/mzipay/Autologging)对于 在python中自动登录;但是,它并不是完全没有样板文件。
这个模块,阻塞,解决了我本来需要解决的任务 根据我构建的应用程序。
它的功能:
- 用一次调用完成复杂根日志程序实例的设置
- 通过yaml配置或通过关键字args可配置登录
- 较低日志级别的详细输出列
- 较高日志级别的抑制输出列
- 可选的旋转文件处理程序
- 这里提供了一个使用阻塞/自动登录的演示“Hello World”应用程序,
- https://github.com/RyanMillerC/DemoCloggingApp
技术上的阻塞可用于配置标准python 日志模块,因为这不直接与自动登录交互, 但它是专门为填补空白和节省时间而设计的 正在生成使用自动登录的应用程序
安装
pip install clogging
文件
导入此模块时使用:
import clogging
这两个函数将开始记录。
从'u yaml
开始用法:
log = clogging.start_from_yaml('path/to/file.yaml')
根据YAML配置文件中的条目开始日志记录这是 设计用于使用现有的应用程序设置.yAML文件,但 不必在内部为您的应用程序添加任何附加内容 它。所有阻塞条目应嵌套在根目录的“阻塞”下 在yaml文件中。如果不清楚,请看演示中的示例 本文档前面提到的应用程序
此函数返回根记录器实例。
您的yaml文件在根级别的结构应该像,
clogging: file: log/app.log format: '%(asctime)22s - %(levelname)8s - %(name)20s - %(message)s' format_ext: '%(asctime)22s - %(levelname)8s - %(name)20s - \ %(funcName)20s - %(message)s' level: INFO max_file_size: 5 MB max_retention: 5 verbose_levels: ['TRACE', 'DEBUG'] app: // Application or other configurations not necessary, shown ... // here for example only ...
所有yaml设置都是可选的。如果在 配置,将使用默认值以获取可用的 选项和默认值,请参见下面的选项部分。
要在没有yaml文件的情况下使用阻塞,请参见下文。
从args
开始用法:
log = clogging.start_from_args( file='log/app.log', format='%(asctime)22s - %(levelname)8s - %(name)20s - %(message)s', format_ext='%(asctime)22s - %(levelname)8s - %(name)20s - ' \ '%(funcName)20s - %(message)s', level='INFO', max_file_size='5 MB', max_retention=5, verbose_levels=['TRACE', 'DEBUG'] )
根据关键字参数开始日志记录此函数将接受 选项名称和值与start_-yaml相同。
此函数返回根记录器实例。
所有参数都是可选的如果未提供任何参数,则为默认值 使用值。甚至可以使用0个参数运行,在这种情况下 默认值将用于所有内容。
有关可用参数和默认值的列表,请参见选项 下面一节。
这个例子是将阻塞添加到项目中并开始的最简单的方法 信息级登录到控制台,
log = clogging.start_from_args(level='INFO')
或者,使用旋转文件处理程序启动调试级日志记录的另一个示例,
log = clogging.start_from_args( file='logs/app.log', level='DEBUG' )
选项
以下是可用的选项及其说明。如果有的话 不提供这些选项,将使用默认值。这些 选项名称可以设置为yaml格式或作为关键字参数 从参数开始。
file: | Path to log file. By default, file logging is disabled. If ‘file’ is set to a file path, for example, ‘log/app.log’, it will enable rotating file logging. Note: In the example ‘log/app.log’, the log file itself, ‘app.log’, does not need to exist; however, the base directory ‘log’ MUST exist. By default the log file will rotate when it reaches 5 MB, with up to 5 rotations being kept before overwriting the oldest. These values can be configured using ‘max_file_size’ and ‘max_retention’. Default: None |
---|---|
format: | Logging format for all non-verbose levels. By default non-verbose is considered to be INFO and higher. Default: ‘%(asctime)22s - %(levelname)8s - %(name)20s - %(message)s’ |
format_ext: | Logging format for all verbose levels. By default this is considered to be DEBUG and TRACE levels. Additional levels can be added to use this format in ‘verbose_levels’. Default: ‘%(asctime)22s - %(levelname)8s - %(name)20s - %(funcName)20s - %(message)s’ |
level: | Logging level. Default: ‘INFO’ |
max_file_size: | Maximum log file size before rollover. This value can either be an integer byte size or a proper string like: “5 MB”, “50 kB”, etc. Setting to 0 will cause the log file to grow infinitely with no rollover. This option has no impact if ‘file’ is set to None. Default: ‘5 MB’ |
max_retention: | Maximum number of rollover logs to keep. Logs will be saved as log.1, log.2, …etc., until max_retention is reached. At that point the oldest of the rollover logs will be purged. This option has no impact if ‘file’ is set to None, or if ‘max_file_size’ is set to 0. Default: 5 |
verbose_levels: | Logging levels in this list are considered verbose levels and will use format_ext for formatting. This is typically done to follow low level logs which show funcName alongside name. Default: [‘TRACE’, ‘DEBUG’] |