跳转到主要内容

将日志格式化为与GCP兼容的JSON。

项目描述

gcp-log

Python库,用于将日志格式化为GCP兼容的JSON。

特性

  • 与标准logging库兼容。
  • 支持结构化日志的extra
  • 100%与官方规范兼容。
  • 非侵入式,不启动任何线程或进程,不进行网络或系统调用。
  • 零配置。
  • 快速。
  • 纯Python。
  • 类型安全。
  • 无依赖。
  • 可选与orjson集成以提高性能。

安装

python3 -m pip install gcp-log

可选:如果您也安装了orjson,gcp-log将自动使用它而不是stdlib json进行更快的序列化

python3 -m pip install orjson

用法

要使用它,只需将gcp_log.Formatter设置为logger的格式化器

import logging
import gcp_log

logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = gcp_log.Formatter()
handler.setFormatter(formatter)
logger.addHandler(handler)

然后您就可以使用logger了

logger.info('aragorn', extra=dict(father='arathorn'))

输出

{"message":"aragorn","severity":"INFO","timestamp":"2022-03-17T10:09:58.393124+00:00Z","logging.googleapis.com/sourceLocation":{"file":"/full/path/to/example.py.py","line":24,"function":"test_gcp_formatter"},"father":"arathorn"}

可读版本

{
    "message": "aragorn",
    "severity": "INFO",
    "timestamp": "2022-03-17T10:09:58.393124+00:00Z",
    "logging.googleapis.com/sourceLocation": {
        "file": "/full/path/to/example.py.py",
        "line": 24,
        "function": "test_gcp_formatter",
    },
    "father": "arathorn",
}

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码发行版

gcp_log-1.0.1.tar.gz (5.4 kB 查看哈希值)

上传时间: 源代码

构建分布

gcp_log-1.0.1-py3-none-any.whl (4.4 kB 查看哈希值)

上传时间: Python 3

由...