simplecomplex/json-log

PSR-3 日志记录器,以 JSON 格式记录事件。

1.5.2 2019-04-08 08:15 UTC

This package is auto-updated.

Last update: 2024-08-29 03:21:03 UTC


README

PSR-3 日志记录器,以 JSON 格式记录事件。

列在配置上分为事件、请求和站点特定项的组。
对于每个列,都有一个 JsonLogEvent 方法和 JSON 存储桶名称(这是存储桶名称列表)。

如果等效方法返回空字符串,则跳过几个列(这是非加粗项)。

必须通过 PSR-3 日志记录器方法的 $context 参数设置一些列。

所有列值都是字符串,除了 code 列。

事件
  • 消息
  • @timestamp: ISO-8601
  • message_id: 较为随机的 ID,使用站点 ID 作为盐
  • correlation_id: 通过 $context 设置
  • subtype: 通过 $context 设置,默认 component
  • level: emergency|alert|critical|error|warning|notice|info|debug
  • code: 整数,通过 $context 设置;默认 0
  • exception: PHP 可抛出对象,通过 $context;变为类名
  • trunc: (original byte length/truncated length)
  • user: 通过 $context 设置,或覆盖扩展类中的 JsonLogEvent::user()
  • session: 通过 $context 设置,覆盖扩展类中的 JsonLogEvent::session()
请求
  • method: HTTP 请求方法;CLI 模式下为 cli
  • request_uri: HTTP 请求 URI;CLI 模式下的控制台参数
  • referer: HTTP 引用(已净化)
  • client_ip: 远程地址,经过 reverse_proxy_addresses 过滤
  • useragent: 已净化
站点
  • type: type 设置;默认 webapp
  • host: $_SERVER['SERVER_NAME'] 或空
  • site_id: siteid 设置
  • canonical: canonical 设置
  • tags: tags 设置

设置

通过 'global' SimpleComplex Configlib_simplecomplex_jsonlog 部分设置。

最简单的方法是 使用环境变量,例如
SetEnv lib_simplecomplex_jsonlog__threshold 7.
JsonLog 构造函数使用环境变量作为后备。

  • (int) threshold: 跳过较不严重的事件(不记录);默认 4 (~warning)
  • (str) siteid: 默认为文档根目录上方的目录名称
  • (str) path: 默认 /var/log/[apache2|httpd|nginx]/php-jsonlog
  • (int) truncate (Kb): 截断 message,以确保事件 JSON 不超过该长度;默认 32
  • (str) reverse_proxy_addresses: 以逗号分隔的 IP 地址列表;默认空
  • (str) type: 默认 webapp
  • (str) canonical: 多个实例中的站点标识符;默认空
  • (str) tags: 以逗号分隔的列表;默认空
  • (str) reverse_proxy_header: 默认 HTTP_X_FORWARDED_FOR
  • (str) file_time: 设置为空或 none 以永久写入同一个日志文件;默认 Ymd
  • (str) format: default|pretty|prettierprettier 不是有效的 JSON,但更容易阅读

推荐设置

所有环境
  • siteid: 使用比默认值更有意义的名称
  • truncate: 如果您的系统生成巨大的转储或跟踪,则将其设置得更高,例如 256
生产环境
  • reverse_proxy_addresses: 注意代理服务器
开发/测试环境
  • 阈值: 7 (~调试)
  • 路径: 如果没有运行日志提取器(例如 Kibana+ElasticSearch),使其更靠近家
  • 格式: 如果没有运行日志提取器,使用 prettier

CLI 命令

# List all json-log commands and their help.
php cli.php json-log -h
# One command's help.
php cli.php json-log-xxx -h

# Check/enable JsonLog to write logs.
php cli.php json-log-committable

# Truncate current log file.
php cli.php json-log-truncate

依赖注入容器 ID: logger

建议:通过 DI 容器 ID 'logger' 访问(并因此实例化)JsonLog。
查看 SimpleComplex Utils Dependency

要求

建议