cumulati/monolog-context 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cumulati/monolog-context

Composer 安装命令:

composer require cumulati/monolog-context

包简介

A utility to create monolog context, timers and counters.

README 文档

README

About

Easily maintain monolog context, timers and counters.

Installation

You can install the package via composer:

composer require cumulati/monolog-context

Create a logger

require 'vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Cumulati\Monolog\LogContext;
use Monolog\Formatter\LineFormatter;

// create a simple monolog logger
$lineFormat = '%level_name% > %message% %context% %extra%' . PHP_EOL;
$formatter = new LineFormatter($lineFormat);
$logger = new Logger('context');
$handler = new StreamHandler('php://stdout');
$handler->setFormatter($formatter);
$logger->pushHandler($handler);
$logger->info('Generic Logger');
INFO > Generic Logger [] []

Create a LogContext

$cx = new LogContext();
$cx->setLogger($logger);
$cx->info('Using manually set logger');
# Using manually set logger [] []

// Logging with the default logger
$cx = new LogContext();

// clear the default logger
LogContext::setDefaultLogger();
$cx->info('This will not log');

// set the default logger
LogContext::setDefaultLogger($logger);
$cx->info('Using the default logger');
INFO > Using manually set logger [] []
INFO > Using the default logger [] []

Add Context

$cx = new LogContext(['Tyrion' => 'good']);

// log with context
$cx->info('This will log with context');

// update context
$cx->addContext(['Tyrion' => 'Lannister']);
$cx->info('This will replace context');

// override context
$cx->info('This will override context', ['Tyrion' => 'bad']);

// replace all context
$cx->setContext(['Dolores' => 'Evan Rachel Wood']);
$cx->info('This will log with replaced context');

$cx->addContext(['Bernard' => 'Jeffrey Wright']);
$cx->info('Add context');
$cx->deleteContext('Bernard');
$cx->deleteContext(['multiple', 'removes']);
$cx->info('Delete some context');

// remove all context
$cx->setContext();
$cx->info('No context added');
INFO > This will log with context {"Tyrion":"good"} []
INFO > This will replace context {"Tyrion":"Lannister"} []
INFO > This will override context {"Tyrion":"bad"} []
INFO > This will log with replaced context {"Dolores":"Evan Rachel Wood"} []
INFO > Add context {"Dolores":"Evan Rachel Wood","Bernard":"Jeffrey Wright"} []
INFO > Delete some context {"Dolores":"Evan Rachel Wood"} []
INFO > No context added [] []

Counters

$cx = new LogContext();

$cx->addCounter('apples');
$cx->info('Log with a counter', ['_counter' => 'apples']);
$cx->info('Log with a counter', ['_counter' => 'apples']);
$cx->info('Not incrementing a counter');
// log with multiple counters, creating one on the fly
$cx->info('Log with a counter', ['_counter' => ['apples', 'bananas']]);

// get the counters
print_r($cx->getCounters());

// reset a counter
$cx->addCounter('apples');
$cx->info('Counter is reset', ['_counter' => 'apples']);

// add multiple counters
$cx->addCounter(['apples', 'bananas']);
$cx->addCounter('pears', 'grapes');

// inline counters
$cx->withCounter('universe')
	->info('With a counter');
$cx->withCounter('hello', 'world')
	->info('With multiple counters');

// set the counter key
$cx->setCounterKey('_c');
$cx->info('Using a different counter key', ['_c' => 'fruits']);

// Set the default counter key
LogContext::setDefaultKeyCounter('C');
$cx = new LogContext();
$cx->addCounter('t');
$cx->info('This has default counter key', ['C' => 't']);
INFO > Log with a counter {"_counter":{"apples":1}} []
INFO > Log with a counter {"_counter":{"apples":2}} []
INFO > Not incrementing a counter [] []
INFO > Log with a counter {"_counter":{"apples":3,"bananas":1}} []
Array
(
    [apples] => 3
    [bananas] => 1
)
INFO > Counter is reset {"_counter":{"apples":1}} []
INFO > With a counter {"_counter":{"universe":1}} []
INFO > With multiple counters {"_counter":{"hello":1,"world":1}} []
INFO > Using a different counter key {"_c":{"fruits":1}} []
INFO > This has default counter key {"C":{"t":1}} []

Timers

$cx = new LogContext();

// create a timer
$cx->addTimer('start');

usleep(20000);
// log with the timer
$cx->info('Log with a timer', ['_timer' => 'start']);

// add multiple timers
$cx->addTimer(['go0', 'go1']);
$cx->addTimer('go2', 'go3');

usleep(10000);
// with an inline timer
$cx->withTimer('start')
->info('Log with a timer inline');

// get the counters
print_r($cx->getCounters());

// set the counter key
$cx->setTimerKey('_t');
$cx->info('Using a different timer key', ['_t' => 'start']);

// Set the default timer key
LogContext::setDefaultKeyTimer('T');
$cx = new LogContext();
$cx->addTimer('x');
usleep(10000);
$cx->info('This has default timer key', ['T' => 'x']);
INFO > Log with a timer {"_timer":{"start":0.022}} []
INFO > Log with a timer inline {"_timer":{"start":0.034}} []
INFO > Using a different timer key {"_t":{"start":0.034}} []
INFO > This has default timer key {"T":{"x":0.012}} []

Context Ids

$cx = new LogContext();
$cx->setAppendCtxId(true);
$cx->info('Add context id');
$cx->info('Which is shared with all messages through this context');

// set the ctxId key
$cx->setCtxIdKey('_');
$cx->info('Different ctx key');

// remove custom ctxIdKey
$cx->setCtxIdKey(null);

// Set default ctxId key
LogContext::setDefaultKeyCtxId('___');
$cx->info('Apple');
INFO > Add context id {"_ctx":"a554920f5edb20d1"} []
INFO > Which is shared with all messages through this context {"_ctx":"a554920f5edb20d1"} []
INFO > Different ctx key {"_":"a554920f5edb20d1"} []
INFO > Apple {"___":"73ae2580f5b59a9e"} []

Levels

All levels defined in RFC 5424 are supported.

$cx = new LogContext();
$cx->debug('This is a debug message');
$cx->info('This is a info message');
$cx->notice('This is a notice message');
$cx->warning('This is a warning message');
$cx->error('This is a error message');
$cx->critical('This is a critical message');
$cx->alert('This is a alert message');
$cx->emergency('This is fine.');
DEBUG > This is a debug message [] []
INFO > This is a info message [] []
NOTICE > This is a notice message [] []
WARNING > This is a warning message [] []
ERROR > This is a error message [] []
CRITICAL > This is a critical message [] []
ALERT > This is a alert message [] []
EMERGENCY > This is fine. [] []

cumulati/monolog-context 适用场景与选型建议

cumulati/monolog-context 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.86k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 10 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「log」 「monolog」 「Context」 「logger」 「timer」 「counter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cumulati/monolog-context 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cumulati/monolog-context 我们能提供哪些服务?
定制开发 / 二次开发

基于 cumulati/monolog-context 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 17.86k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-10-30