安装依赖
$ pwd
/d/apps/wamp/www/phpweb
$ composer require monolog/monolog
Using version ^3.5 for monolog/monolog
./composer.json has been updated
集成 monolog
编辑core/Controller.php
,引入相关类
<?php
namespace core;
use core\tpl\TwigTpl;
use core\tpl\PhpTpl;
use core\tpl\Tpl;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
编辑core/Controller.php
,添加monolog
相关代码
public function debug($message, array $content = []) {
$this->logCreate();
$this->_log->debug($message, $content);
}
public function info($message, array $content = []) {
$this->logCreate();
$this->_log->info($message, $content);
}
public function error($message, array $content = []) {
$this->logCreate();
$this->_log->error($message, $content);
}
/** @var Logger */
private $_log = null;
public function logCreate($filename = '') {
if ($this->_log && !$filename) return;
$filename = $filename ?: date('Ymd') . '.log';
$filename = PATH_LOG . $filename;
$this->_log = new Logger('logger');
// 用户自定义日志级别
if (defined('APP_LOG_LEVEL')) {
$this->_log->pushHandler(new StreamHandler($filename, $this->_logGetLevel(APP_LOG_LEVEL)));
} else {
$logLevel = APP_DEBUG ? 'debug' : 'error';
$this->_log->pushHandler(new StreamHandler($filename, $this->_logGetLevel($logLevel)));
}
}
private function _logGetLevel($levelName) {
if ($levelName == 'debug') {
return Logger::DEBUG;
} else if ($levelName == 'info') {
return Logger::INFO;
} else {
return Logger::ERROR;
}
}
编辑public/index.php
,定义APP_LOG_LEVEL
常量日志等级
// Twig 模板引擎缓存目录(使用 twig 模板引擎才需要定义)
define('PATH_TWIG_CACHE', PATH_RUNTIME . 'twig/');
// 日志等级(debug|info|error)
//define('APP_LOG_LEVEL', 'error');
// 默认应用
define('DEFAULT_APP', 'home');
使用日志
编辑app/home/Hello.php
,添加log
和log2
方法
public function log() {
$this->debug('debug 日志来自 Hello.log');
$this->info('info 日志来自 Hello.log');
$this->error('error 日志来自 Hello.log', ['name' => 'five', 'age' => 18]);
}
public function log2() {
$this->logCreate('log2.log');
$this->debug('debug 日志来自 Hello.log2');
$this->info('info 日志来自 Hello.log2');
$this->error('error 日志来自 Hello.log2', ['name' => 'five', 'age' => 18]);
}
## 测试
浏览器访问 http://phpweb.com/home/hello/log,日志文件 runtime/log/20200428.log 增加
[2022-04-28T13:00:53.686150+08:00] logger.DEBUG: debug 日志来自 Hello.log [] []
[2022-04-28T13:00:53.688133+08:00] logger.INFO: info 日志来自 Hello.log [] []
[2022-04-28T13:00:53.688187+08:00] logger.ERROR: error 日志来自 Hello.log {"name":"five","age":18} []
浏览器访问 http://phpweb.com/home/hello/log2,日志文件 runtime/log/log2.log 增加
[2022-04-28T13:04:29.075890+08:00] logger.DEBUG: debug 日志来自 Hello.log2 [] []
[2022-04-28T13:04:29.078215+08:00] logger.INFO: info 日志来自 Hello.log2 [] []
[2022-04-28T13:04:29.078270+08:00] logger.ERROR: error 日志来自 Hello.log2 {"name":"five","age":18} []
编辑public/index.php
,修改常量APP_DEBUG
为false
浏览器访问 http://phpweb.com/home/hello/log,日志文件 runtime/log/20200428.log 增加
[2022-04-28T13:05:41.393067+08:00] logger.ERROR: error 日志来自 Hello.log {"name":"five","age":18} []
浏览器访问 http://phpweb.com/home/hello/log2,日志文件 runtime/log/log2.log 增加
[2022-04-28T13:05:54.468236+08:00] logger.ERROR: error 日志来自 Hello.log2 {"name":"five","age":18} []
编辑public/index.php
,修改常量APP_DEBUG
为true
,修改常量APP_LOG_LEVEL
为info
浏览器访问 http://phpweb.com/home/hello/log,日志文件 runtime/log/20200428.log 增加
[2022-04-28T13:06:54.743879+08:00] logger.INFO: info 日志来自 Hello.log [] []
[2022-04-28T13:06:54.745876+08:00] logger.ERROR: error 日志来自 Hello.log {"name":"five","age":18} []
浏览器访问 http://phpweb.com/home/hello/log2,日志文件 runtime/log/log2.log 增加
[2022-04-28T13:07:05.279270+08:00] logger.INFO: info 日志来自 Hello.log2 [] []
[2022-04-28T13:07:05.280969+08:00] logger.ERROR: error 日志来自 Hello.log2 {"name":"five","age":18} []
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。