glopgar/monolog-timer-processor 问题修复 & 功能扩展

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

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

glopgar/monolog-timer-processor

最新稳定版本:v0.1.1

Composer 安装命令:

composer require glopgar/monolog-timer-processor

包简介

A processor for Monolog that adds timing info to the message contexts

README 文档

README

Processor for monolog that allows to time fragments of code by adding timers info to the message contexts.

Basic usage

$logger = new \Monolog\Logger('timer.example');
$logger->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout'));

$logger->pushProcessor(new Glopgar\Monolog\Processor\TimerProcessor());

// start a timer with name: 'exampleProcess'
$logger->debug('Process started', ['timer' => ['exampleProcess' => 'start']]);
usleep(1500000);
// stop the timer
$logger->debug('Process ended', ['timer' => ['exampleProcess' => 'stop']]);
$logger->debug('Process started again', ['timer' => ['exampleProcess' => 'start']]);
usleep(2300000);
$logger->debug('Process ended', ['timer' => ['exampleProcess' => 'stop']]);

Log output:

[2016-08-10 17:07:26] timer.example.DEBUG: Process started {"timer":{"exampleProcess":"start"}} []
[2016-08-10 17:07:27] timer.example.DEBUG: Process ended {"timer":{"exampleProcess":{"time":"1.50 s.","totalTime":"1.50 s.","count":1}}} []
[2016-08-10 17:07:27] timer.example.DEBUG: Process started again {"timer":{"exampleProcess":"start"}} []
[2016-08-10 17:07:30] timer.example.DEBUG: Process ended {"timer":{"exampleProcess":{"time":"2.30 s.","totalTime":"3.81 s.","count":2}}} []

Multiple timers

You can start or stop more than one timer on each log line:

$logger->debug('Processes started', ['timer' => ['process1' => 'start', 'process2' => 'start']]);
sleep(1);
$logger->debug('Process ended, process started', ['timer' => ['process1' => 'stop', 'process3' => 'start']]);
sleep(1);
$logger->debug('Processes ended', ['timer' => ['process2' => 'stop', 'process3' => 'stop']]);

Log output:

[2016-08-10 17:17:26] timer.example.DEBUG: Processes started {"timer":{"process1":"start","process2":"start"}} []
[2016-08-10 17:17:27] timer.example.DEBUG: Process ended, process started {"timer":{"process1":{"time":"1.00","totalTime":"1.00","count":1},"process3":"start"}} []
[2016-08-10 17:17:28] timer.example.DEBUG: Processes ended {"timer":{"process2":{"time":"2.01","totalTime":"2.01","count":1},"process3":{"time":"1.00","totalTime":"1.00","count":1}}} []

Getting timers info

You can obtain an array with all the timers information using the TimerProcessor::getTimers method:

This can be useful for logging the timer totals at the end of the process:

$timers = $processor->getTimers();
foreach ($timers as $timer => $info) {
    $logger->notice('%s executed %d times, total time: %.2f s.', $timer, $info['count'], $info['totalTime']);
}

github:glopgar

统计信息

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

GitHub 信息

  • Stars: 14
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-24