用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1101|回复: 0
收起左侧

[讨论/交流] CI4 搭建 与 日志修改

[复制链接]
发表于 2020-4-9 11:42:48 | 显示全部楼层 |阅读模式

本人喜欢看日志,可能经常开发服务器的原因,日志对于生产环境尤为重要,不要还停留在vardump

最近工作不是很忙,闲着就想弄下自己的网站,下面是搭建Ci4遇到的一些问题
1,原来web路径需要指到public目录下.

2,生产环境配置(默认环境)app/Config/Boot/production.php 页面显示错误信息
   ini_set('display_errors', '1');        #1为显示,0为不显示。如果你的日志还未生效,但是又找不到错误原因,可以使用此方法,他会把错误显示到页面,但生产环境肯定是要关的,所以日志要弄出来

3,缓存日志目录需要给读写权限,日志目录就在这下面writable/logs
    chown apache.apache -R writable  //我的环境是apache用户调用的php

4,日志打印的时间有问题,时区不对 app/Config/App.php这个文件中
    public $appTimezone = PRC;  //设置中国时区

现在日志看着正常,其实还是挺难看,比起ci3 ci2差多了,可能是作者懒

我需要的日志是能显示我调用日志的函数还有行号,显然Ci4作者没弄,不能忍,改了,先给你们看看效果

代码:
10    public function file_list(){
15       log_message("debug","测试打印");
16    }
日志:
    INFO - 10:59:23,Controller "App\Controllers\Files" loaded.
    DEBUG - 10:59:23,file_list:15 --> 测试打印

现在很直观,file_list这个函数 15行打印了“测试打印”,上面的这个时间(10:59:23)可以设置 app/Config/Logger.php ,public $dateFormat = 'H:i:s';

改动很小,源码(./system/Common.php),作者好像留一个重写函数的文件(./app/Common.php),可以把你要重写的函数放里面,我没去弄,直接改了
697     function log_message(string $level, string $message, array $context = [])
698     {
699         // When running tests, we want to always ensure that the
700         // TestLogger is running, which provides utilities for
701         // for asserting that logs were called in the test code.
702         $debug_info = debug_backtrace();  
703         $func = $debug_info[1]["function"];
704         $line = $debug_info[0]["line"];
705         if (ENVIRONMENT === 'testing')
706         {
707             $logger = new TestLogger(new Logger());
708
709             return $logger->log($level, $func.":".$line." --> ".$message, $context);
710         }
711
712         // @codeCoverageIgnoreStart
713         return Services::logger(true)
714             ->log($level, $func.":".$line." --> ".$message, $context);
715         // @codeCoverageIgnoreEnd
716     }

顺眼很多了,又码了一会代码
10     public function file_list(){
11         //$cache = \Config\Services::cache();
12         //$a = $cache->save('a',"1111");
13         //$a = $cache->get('a');
14         //log_message("debug","a = ".$a);
15         log_message("debug","测试打印");
16         $file = $this->request->getPost("file");
17         $id = $this->request->getPost("id");
18         $files_model = new FilesModel();
19         $list["id"] = $id;
20         $list["list"] = [];
21         $files_model->scan_dir("/home/ycx/coffer".$file,$list["list"],$file);
22         echo json_encode($list);
23     s}


开始调试,报错,
CRITICAL - 11:14:40,exceptionHandler:153  --> syntax error, unexpected '}'
#0 /home/ycx/web/ycxwoo/api/system/Router/Router.php(192): CodeIgniter\Router\Router->autoRoute()
#1 /home/ycx/web/ycxwoo/api/system/CodeIgniter.php(785): CodeIgniter\Router\Router->handle()
#2 /home/ycx/web/ycxwoo/api/system/CodeIgniter.php(358): CodeIgniter\CodeIgniter->tryToRouteIt()
#3 /home/ycx/web/ycxwoo/api/system/CodeIgniter.php(306): CodeIgniter\CodeIgniter->handleRequest()
#4 /home/ycx/web/ycxwoo/api/public/index.php(45): CodeIgniter\CodeIgniter->run()
#5 {main}


完全看不出来哪里错了,只知道 } 附近有问题,如果有几千行代码,一二十个文件我还不找疯,不能忍,改了
日志看出exceptionHandler这个函数 153 行调用了log_message打印日志
143     public function exceptionHandler(Throwable $exception)
144     {
145         $codes      = $this->determineCodes($exception);
146         $statusCode = $codes[0];
147         $exitCode   = $codes[1];
148
149         // Log it
150         if ($this->config->log === true && ! in_array($statusCode, $this->config->ignoreCodes))
151         {
152             log_message('critical',$exception->getMessage() . "\n{trace}", [
153                             'trace' => $exception->getTraceAsString(),
154                         ]);
155         }


这玩意$exception->getMessage()应该就是错误消息了,百度这个类型Throwable,
Throwable {
/* Methods */
abstract public getMessage ( void ) : string
abstract public getCode ( void ) : int
abstract public getFile ( void ) : string
abstract public getLine ( void ) : int
abstract public getTrace ( void ) : array
abstract public getTraceAsString ( void ) : string
abstract public getPrevious ( void ) : Throwable
abstract public __toString ( void ) : string
}

这不是有文件名和行号吗,作者就是懒 getFile ( void ) ,getLine ( void ) ,改了


143     public function exceptionHandler(Throwable $exception)
144     {
145         $codes      = $this->determineCodes($exception);
146         $statusCode = $codes[0];
147         $exitCode   = $codes[1];
148
149         // Log it
150         if ($this->config->log === true && ! in_array($statusCode, $this->config->ignoreCodes))
151         {
152             log_message('critical', $exception->getFile().":".$exception->getLine()." --> ".$exception->getMessage() . "\n{trace}", [
153                             'trace' => $exception->getTraceAsString(),
154                         ]);
155         }


再调试下


CRITICAL - 11:24:58,exceptionHandler:153 --> /home/ycx/app/Controllers/Files.php:23 --> syntax error, unexpected '}'
#0 /home/ycx/web/ycxwoo/api/system/Router/Router.php(192): CodeIgniter\Router\Router->autoRoute()
#1 /home/ycx/web/ycxwoo/api/system/CodeIgniter.php(785): CodeIgniter\Router\Router->handle()
#2 /home/ycx/web/ycxwoo/api/system/CodeIgniter.php(358): CodeIgniter\CodeIgniter->tryToRouteIt()
#3 /home/ycx/web/ycxwoo/api/system/CodeIgniter.php(306): CodeIgniter\CodeIgniter->handleRequest()
#4 /home/ycx/web/ycxwoo/api/public/index.php(45): CodeIgniter\CodeIgniter->run()
#5 {main}


不错,我很满意

献给那些不会调试错误和看日志的朋友

本版积分规则