petert82/monolog-logfmt
Composer 安装命令:
composer require petert82/monolog-logfmt
包简介
A logfmt formatter for Monolog
README 文档
README
A logfmt formatter for Monolog.
Installation
The formatter can be installed using Composer:
$ composer require petert82/monolog-logfmt
Requirements
- PHP 7.4.0 or later.
Usage
Simply set an instance of Petert82\Monolog\Formatter\LogfmtHandler as the formatter for the Handler that you wish to output logfmt formatted logs.
use Monolog\Logger; use Monolog\Handler\StreamHandler; use Petert82\Monolog\Formatter\LogfmtFormatter; $log = new Logger('name'); $handler = new StreamHandler('php://stdout', Logger::WARNING); $handler->setFormatter(new LogfmtFormatter()); $log->pushHandler($handler); $log->addError('Danger! High voltage!', ['voltage' => 9000]);
Running this example would output something like:
ts=2017-11-21T20:02:10+00:00 lvl=ERROR chan=name msg="Danger! High voltage!" voltage=9000
Customisation
By default these keys will be used for the default log fields in the logfmt output:
| Key | Content |
|---|---|
ts |
Timestamp. |
lvl |
Log level name. |
chan |
Channel name. |
msg |
Log message. |
All of these keys, and the format used for formatting logged DateTimes, can be customised by passing the appropriate parameters to the formatter's constructor. For example:
$tsKey = 'date'; $levelKey = 'level'; $channelKey = 'channel'; $msgKey = 'message'; $dateFormat = 'Ymd-His'; $formatter = new LogfmtFormatter($tsKey, $levelKey, $channelKey, $msgKey, $dateFormat);
Logs formatted using this formatter would look like this:
date=20171119-190000 level=INFO channel=app message=Message
The standard keys can also be excluded from the output by passing an empty string (or null) to the appropriate constructor param. For example, to include only the message:
$formatter = new LogfmtFormatter('', '', '', 'msg');
The formatter's output would now look like this:
msg=Message
Structure Flattening
By default, nested arrays and objects in the log context and extra fields are serialized as JSON strings. If you would prefer to instead flatten such structures into logfmt-compatible key-value pairs, set the $flattenStructures constructor parameter true:
// Default JSON serialization $formatter = new LogfmtFormatter(); // Produces output like: user={"name":"John","roles":["admin","editor"]} // With flattening enabled $formatter = new LogfmtFormatter( 'ts', 'lvl', 'chan', 'msg', DateTime::RFC3339, "\n", true // Enable flattening ); // Outfut now looks like: user_name=John user_roles_0=admin user_roles_1=editor
Development
A Makefile is provided to test the library locally, the only requirement for this is that Docker be installed on your
development machine. Simply run make in the project root to execute the test suite.
License
Licensed under the MIT License - see the LICENSE file for details
petert82/monolog-logfmt 适用场景与选型建议
petert82/monolog-logfmt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 254.93k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2017 年 11 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「log」 「logging」 「monolog」 「logfmt」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 petert82/monolog-logfmt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 petert82/monolog-logfmt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 petert82/monolog-logfmt 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A GeoIP decorator/processor for monolog
A Zend Framework module that sets up Monolog for logging in applications.
Laravel 5.6 Monolog custom telegram channel
A Monolog processor to add X-Request-Id or UNIQUE_ID env to monolog stack if present, provide a default one otherwise. Optionally, add a forwarded request-id list.
PSR-3 compatible logger with dynamic file naming and hourly rotation, powered by Monolog
Asynchronous Sentry for Symfony - Fire and forget
统计信息
- 总下载量: 254.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-21