lumen/laravel 毫秒时间戳日志

1.修改 bootstrap/app.php

$app->configureMonologUsing(function(Monolog\Logger $monolog) use ($app) {

   return $monolog->pushHandler( new \App\Handler\MonoLogFormatLineHandler($app->storagePath().'/logs/lumen.log') );

});

2.创建文件app/Handle/MonoLogFormatLineHandler.php

namespace App\Handler;

/**

* Created by PhpStorm.

* User: xiaojun

* Date: 18-5-19

* Time: 下午6:04

*/

use Monolog\Handler\RotatingFileHandler;

use Monolog\Handler\HandlerInterface;

use Monolog\Formatter\LineFormatter;

class MonoLogFormatLineHandler extends RotatingFileHandler implements HandlerInterface {

    const MICROTIME = 'Y-m-d H:i:s.u';

    public function __construct($filename){

      parent::__construct($filename);

}

    public function handle(array $record)

{

        if (!$this->isHandling($record)) {

            return false;

}

        $record = $this->processRecord($record);

        $this->setFormatter(new LineFormatter(null, self::MICROTIME));

        $record['formatted'] = $this->getFormatter()->format($record);

        $this->write($record);

        return false === $this->bubble;

}

}

3.日志样式

[2018-05-20 03:13:13.417198] lumen.INFO: RESPONSE>>> {"_o_status":200,"_o_content":null,"curResponse":{"_o_requestid":"c762bd3d-8288-4ea8-bbf1-03245dafac05","_o_time":"2018-05-20-03:13:13"}} []

你可能感兴趣的:(lumen/laravel 毫秒时间戳日志)