引入
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 Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Processor\UidProcessor;
use Monolog\Processor\ProcessIdProcessor;
use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\JsonFormatter;
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:学会封装,学会分层。
来源:oschina
链接:https://my.oschina.net/u/4339254/blog/3924449