文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

使用Composer从零开发一个简单的web框架(08)-monolog

admin

admin

2024-04-02 17:21

关注

安装依赖

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);
}


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,添加loglog2方法

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_DEBUGfalse

浏览器访问 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_DEBUGtrue,修改常量APP_LOG_LEVELinfo

浏览器访问 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} []
阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     428人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     199人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     159人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     239人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     62人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯