monolog 应该是世界上最好的日志插件了

Posted jiqing9006

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了monolog 应该是世界上最好的日志插件了相关的知识,希望对你有一定的参考价值。

引入

composer require monolog/monolog

官网
https://github.com/Seldaek/monolog

技术分享图片

创建工具类

<?php
/**
 * Created by PhpStorm.
 * User: jiqing
 * Date: 18-6-27
 * Time: 上午11:19
 */

require '../vendor/autoload.php';

use MonologLogger;
use MonologHandlerStreamHandler;
use MonologProcessorUidProcessor;
use MonologProcessorProcessIdProcessor;
use MonologFormatterLineFormatter;
use MonologFormatterJsonFormatter;

class MyLog {
    public static function setLog($content = [],$msg = "msg",$level = Logger::NOTICE ,$name = "jq",$formatter = "line",$path = "/tmp/jq.log") {
        // create a log channel
        $log = new Logger($name);
        $stream_handler = new StreamHandler($path, Logger::DEBUG); // 过滤级别
        switch (strtolower($formatter)) {
            case "line":
                $stream_handler->setFormatter(new LineFormatter());
                break;
            case "json":
                $stream_handler->setFormatter(new JsonFormatter());
                break;
            default:
        }
        $log->pushHandler($stream_handler);
        $uid_obj = new UidProcessor();
        $log->pushProcessor($uid_obj);

        $pid_obj = new ProcessIdProcessor();
        $log->pushProcessor($pid_obj);

        // add records to the log
        switch (strtoupper($level)) {
            case Logger::DEBUG:
            case 1:
            case "DEBUG":
                $log->debug($msg,$content);
                break;
            case Logger::INFO:
            case 2:
            case "INFO":
                $log->info($msg,$content);
                break;
            case Logger::NOTICE:
            case 3:
            case "NOTICE":
                $log->notice($msg,$content);
                break;
            case Logger::WARNING:
            case 4:
            case "WARNING":
                $log->warning($msg,$content);
                break;
            case Logger::ERROR:
            case 5:
            case "ERROR":
                $log->error($msg,$content);
                break;
            case Logger::CRITICAL:
            case 6:
            case "CRITICAL":
                $log->critical($msg,$content);
                break;
            case Logger::ALERT:
            case 7:
            case "ALERT":
                $log->alert($msg,$content);
                break;
            case Logger::EMERGENCY:
            case 8:
            case "EMERGENCY":
                $log->emergency($msg,$content);
                break;
            default:
                $log->debug($msg,$content);
        }

        return $uid_obj->getUid();
    }
}

创建测试类

<?php
/**
 * Created by PhpStorm.
 * User: jiqing
 * Date: 18-6-27
 * Time: 上午11:23
 */

class TestAction extends IndexAction
{
    public function testLog() {
        vendor('Log.MyLog');
        MyLog::setLog("content1");
        MyLog::setLog("content2","");
        MyLog::setLog("content3","","error");
        MyLog::setLog("content4","","5");
        MyLog::setLog("content5","","400");
        MyLog::setLog("content6","","400","jiqing","json");
        MyLog::setLog("content7","","400","jiqing","line");
        MyLog::setLog(["content8"],"","400","jiqing","line");
        MyLog::setLog(["content8"],"","400","jiqing","line","/tmp/jq.log");
        MyLog::setLog(["content8"],"","400","jiqing","line","/tmp/jq.log");
    }
}

查看结果

[2018-06-27 12:55:24] jq.NOTICE: msg content1 {"process_id":2729,"uid":"ecbd811"}
[2018-06-27 12:55:24] jq.NOTICE:  content2 {"process_id":2729,"uid":"4b2041e"}
[2018-06-27 12:55:24] jq.ERROR:  content3 {"process_id":2729,"uid":"805f3d6"}
[2018-06-27 12:55:24] jq.ERROR:  content4 {"process_id":2729,"uid":"c3c1706"}
[2018-06-27 12:55:24] jq.ERROR:  content5 {"process_id":2729,"uid":"c01087f"}
{"message":"","context":"content6","level":400,"level_name":"ERROR","channel":"jiqing","datetime":{"date":"2018-06-27 12:55:24.037391","timezone_type":3,"timezone":"PRC"},"extra":{"process_id":2729,"uid":"f4528b7"}}
[2018-06-27 12:55:24] jiqing.ERROR:  content7 {"process_id":2729,"uid":"532ac58"}
[2018-06-27 12:55:24] jiqing.ERROR:  ["content8"] {"process_id":2729,"uid":"3d5751c"}

它支持功能很多,这里只是其中的一小部分。可以line展示,可以json展示,可以html展示结果。
可以记录每一次的进程id,每一次用户id。记录时间。
内容支持字符串,支持数组。

可以支持日志级别。
可以设置名称,区分是谁操作的。

很牛逼。

ps:学会封装,学会分层。

以上是关于monolog 应该是世界上最好的日志插件了的主要内容,如果未能解决你的问题,请参考以下文章

Monolog 自动填充 RequestId / TraceId 请求链路追踪标识

Monolog:将不同通道和多个处理程序记录到分组日志文件 + 专用日志文件

在 Symfony2 中使用 Monolog 进行日志旋转

Symfony2/Monolog:日志级别 - 仅显示 app.INFO?

如何配置 Monolog 从日志中排除 404 错误

配置 symfony monolog 保留 apache 日志