关于Thinkphp6的日志问题

Thinkphp6的日志问题

日志级别

debug, info, notice, warning, error, critical, alert, emergency

其中有一个特别的级别:sql,专门用来记录sql语句的

设置日志记录级别

对于程序比较重要的业务模块可以进行埋点(进行日志记录)

可以通过设置日志记录级别来开启和关闭记录

有助于排除错误(比每次出现错误去代码里增加记录日志好多了)

        # 修改 config/log.php
        # 配置 'level' => ['notice','warning']
        # level 不为空时,只记录level中指定的错误级别
        # level 为空时,记录所有级别
        $user = UserService::getInstance()->findByUsername('xieruixiang');
        Log::warning("warning:{user}", compact('user'));
        # info 不再 level 中 则不会记录
        Log::info("I'm info");

单一日志

默认的tp日志是写在当前日期(年月)目录下的,如(runtime/admin/log/202204/30_info.log)

单一日志设置 修改config/log.php 中通道single属性为true

设置单一日志后,将不再写在时间目录下(一直写一个固定目录),如(runtime/admin/log/single_info.log)

# 开启单一日志
# channels.file.single
# 这里给file通道开启单一日志
'single' => true

独立日志

每一种日志级别的日志都归类到一个文件之中(推荐开启独立日志)

设置 config/log.php 中通道apart_level属性

        #  设置 file 通道 info,notice,warning 级别开启独立日志
        #  channels.file.apart_level
        # 'apart_level' => ['info', 'notice', 'warning']
        # 在 apart_level中的级别会独立写到一个文件中去

        # write to runtime/admin/log/202204/30_warning.log
        Log::warning("I'm ");
        # write to runtime/admin/log/202204/30_info.log
        Log::info("I'm info");
        # write to runtime/admin/log/202204/30_notice.log
        Log::notice("I'm notice");

日志的写入时机

日志写入时机提供两种(实时写入,程序执行完后写入)

通过设置config/log.php中通道 realtime_write 属性

        # 这里中断程序的执行
        # 如果 realtime_write = false 则无法写到日志中去
        # realtime_write = true 可以写入日志中去
        Log::warning("I'm ");

        # 如果 realtime_write = false
        # 又想实时写入
        # 可以通过 Log::write($msg, $type) 实时写入
        # $msg 信息
        # $type 日志级别
        Log::write("I'm write", 'warning');
        die("日志将不会写入");

日志通道

可以自定义通道

以增加邮件通道为例

将config/log.php 中通道type 改成自定义驱动类即可

        # config/log.php channels 添加
         'email' => [
            'type' => \app\admin\driver\EmailDriver::class,
            # 调试发送邮件时将其设置成实时比较好调试
            'realtime_write' => true,
        ]
        
       # EmailDriver 需要实现 think\contract\LogHandlerInterface
       class EmailDriver implements LogHandlerInterface
       {
          public function save(array $log): bool
          {
          # 这里进行发送邮件逻辑
          # 想知道邮件发送逻辑的可以参考我的文章 《php发送邮件》
          # 不想知道的 可以调用第三方封装好的php发送邮件组件
           return true;
          }
       }

使用邮件通道

 # channel($channelName) 指定发送通道
 # 不指定则使用默认发送通道
 # config/log.php
 # 'default' => env('log.channel', 'file'),
 Log::channel('email')->info("this is info");
 # 就能以邮件形式通知了

Thinkphp6异常处理与日志

异常处理

目标:返回json格式的异常信息 # url_route_must:true强制路由模式下

thinkphp6内置已了一个app\ExceptionHandle异常处理类可供使用

该类绑定在app目录下面的provider.php文件中,直接修改该类的相关方法即可完成应用的自定义异常处理机制。

在这里插入图片描述

app\ExceptionHandle.php 异常处理类,重新定义render方法即可

#app\ExceptionHandle.php 
public function render($request, Throwable $e): Response
 {
 // app_debug模式下按原thinkphp6异常模式处理异常
 if (env('app_debug')) {
 return parent::render($request, $e);
 }
 // 自定义json返回错误
 if ($e instanceof ValidateException) {
 return json($e->getError(), 422);
 return json(['code' => 0, 'msg' => $e->getError()], 422);
 }

 // 自定义json返回异常
 if ($e instanceof HttpException && $request->isAjax()) {
 return json(['code' => 0, 'msg' => $e->getMessage()], $e->getStatusCode());
 }
	// 自定义json返回异常
 if ($e instanceof HttpException) {
 return json(['code' => 0, 'msg' => $e->getMessage()]);
 }
	// 自定义json返回异常
 return json(['code' => 0, 'msg' => 'Biny服务器错误']);
 }

目标:访问未定义的路由时返回json格式的信息 # url_route_must:false 非强制路由模式下

php think make:controller Error --plain

public function index()
 {
 return json([
 'code' => 0,
 'data' => 'Route is Not Found',
 'msg' => 'success'
 ]);
 }

 public function __call($name, $arguments)
 {
 return json([
 'code' => 0,
 'data' => 'Route is Not Found',
 'msg' => 'success'
 ]);
 }

日志

  • DEBUG模式下默认记录error级别和sql执行语句日志
  • 非DEBUG模式默认仅记录error级别日志
  • DEBUG模式在根目录增加.env文件 设置APP_DEBUG = false/true

在这里插入图片描述

手动记录日志

方法描述
record()Log::record(‘record方法记录的日志信息不是实时保存的’,‘info’);
write()Log::write(‘要实时记录的话,可以采用write方法’,‘info’);

系统在请求结束后会自动调用Log::save方法统一进行日志信息写入

关闭日志

Log::close(); //手动关闭本次请求的日志写入

更多日志配置于app\config\log.php文件中配置,参考官方文档配置即可

官方日志文档

总结

作者:xie_rui_xiang原文地址:https://xieruixiang.blog.csdn.net/article/details/124542192

%s 个评论

要回复文章请先登录注册