cosmolog:结构化Python日志记录器
项目描述
Cosmolog
Cosmolog为您的应用程序提供结构化日志格式化。它将日志视为事件流,并将每个日志事件作为具有模式的json对象进行记录。
CosmologEvent模式
Cosmolog事件看起来如下
{
"version": 0,
"stream_name": "foo_nginx",
"origin": "foo-api1.com",
"timestamp": "2016-09-02T16:34:12.019105Z",
"format": "service {name} started",
"level": 400,
"payload": {"name": "foo"}
}
version (int) The version of the logging schema.
stream_name (string) The name that identifies the log stream.
origin (string) FQDN of the host on which the stream originated.
timestamp (string) UTC ISO8601 formatted datetime string.
format (string) Optional Python Format String to format the payload to
make it human-readable. Please use format strings with
replacement fields that are delimited with "{}".
level (int) The log level.
payload (dict) A flat dictionary of key-value pairs where keys are
strings and values can be any scalar type.
安装
pip install cosmolog
快速开始
from cosmolog import setup_logging
from cosmolog import Cosmologger
setup_logging()
l = Cosmologger('foo')
l.info('Hello World')
l.info('Hello {person}', person='Dave')
l.info(value1=0.98, value2=4.0)
人类
human
是一个命令行工具,用于将机器可读日志格式化为人可读的格式。它读取stdin并将其格式化的行写入stdout。
$ echo '{"origin": "enterprise.starfleet.com", "stream_name": "telemetry", "format": "Measurement complete: gravity={gravity}", "timestamp": "2016-10-19T04:13:15.049920Z", "level": 400, "version": 0, "payload": {"gravity": 1.8}}' | human
Oct 19 04:13:15 enterprise.starfleet.com telemetry: [INFO] Measurement complete: gravity=1.8
高级用法:setup_logging
setup_logging
提供了基本的Python日志配置,其中CosmologgerFormatter
类作为默认的日志格式化器。默认的日志配置如下所示
LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'cosmolog': {
'()': CosmologgerFormatter,
'origin': origin,
'version': 0,
},
},
'handlers': {
'default': {
'class': 'logging.StreamHandler',
'formatter': 'cosmolog'
},
},
'root': {
'handlers': ['default'],
'level': level,
}
}
默认情况下,origin
设置为socket.getfqdn()
。要自己设置
setup_logging(origin='my-fully-qualified-domain.com')
您可以使用自定义配置字典配置Cosmologger,就像您使用常规Python日志一样。假设您想将日志条目以CosmologEvent模式流出到文件,以及以人可读的格式输出stderr
my_custom_config = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'cosmolog': {
'()': CosmologgerFormatter,
'origin': origin,
'version': 0,
},
'cosmolog-human': {
'()': CosmologgerHumanFormatter,
'origin': origin,
'version': 0,
},
},
'handlers': {
'file_handler': {
'class': 'logging.FileHandler',
'formatter': 'cosmolog',
'filename': '/path/to/my/file.log'
},
'stderr': {
'class': 'logging.StreamHandler',
'formatter': 'cosmolog-human',
'color': True
},
},
'root': {
'handlers': ['file_handler', 'stderr'],
'level': 'DEBUG',
}
}
setup_logging(custom_config=my_custom_config)
默认情况下,您的日志级别将设置为INFO
。您可以将其设置为不同的级别
setup_logging(level='DEBUG')
下表显示了级别名称及其在日志输出中将映射到的数值
级别 | 数值 |
---|---|
FATAL | 100 |
ERROR | 200 |
WARN | 300 |
INFO | 400 |
DEBUG | 500 |
TRACE | 600 |
高级用法:异常
通常,所有关键字参数都会忠实传递到 CosmologEvent
有效载荷字段,但日志异常的情况除外。在这里,遵循内置日志库的约定。调用 Cosmologger.exception
或其他带有关键字参数 exc_info=1
的日志方法,会将跟踪信息添加到有效载荷的 exc_text
字段。如果格式字符串中没有指定 {exc_text}
,则格式字段将附加 '\n{exc_text}'
。
开发
pip install -e .[test]
tox
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪一个,请了解更多关于 安装包 的信息。
源分布
cosmolog-0.9.12.tar.gz (17.4 kB 查看哈希值)
构建分布
cosmolog-0.9.12-py3-none-any.whl (14.3 kB 查看哈希值)
关闭
cosmolog-0.9.12.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | f6fcae8b2efc30e352e75e24a635d204d359acc886826c18d2ac377f217dc757 |
|
MD5 | f45ff2772547268d0cf946019e0a44e5 |
|
BLAKE2b-256 | f996f2a4fb9e1c3d91c7f43f795ebf0323f2c1c9f57777b66a67390d351cffd5 |
关闭
cosmolog-0.9.12-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 6f7e9fa8114e6015908db4fd10bfa58ecb015f8a8ff8c880986b9cbee3afa898 |
|
MD5 | 6d269f20da0aaba1e3227614c70c8828 |
|
BLAKE2b-256 | cb2646f7df904f56273b1841423257f74e4ce411737d4838f282f9aaffde6b4e |