thinkphp6:配置应用的日志(thinkphp6.0.5 / php 7.4.9)
一,指定日志的级别和文件路径
修改config/log.php
设置两项:
//指定日志的级别,
//默认的级别:debug, info, notice, warning, error, critical, alert, emergency,sql
'level' => ['error','critical', 'alert', 'emergency','warning'],
//指定日志的目录:
// 日志保存目录 'path' => '/data/logs/phplogs/tlog',
效果如下:
<?php // +---------------------------------------------------------------------- // | 日志设置 // +---------------------------------------------------------------------- return [ // 默认日志记录通道 'default' => env('log.channel', 'file'), // 日志记录级别 //debug, info, notice, warning, error, critical, alert, emergency,sql 'level' => ['error','critical', 'alert', 'emergency','warning'], // 日志类型记录的通道 ['error'=>'email',...] 'type_channel' => [], // 关闭全局日志写入 'close' => false, // 全局日志处理 支持闭包 'processor' => null, // 日志通道列表 'channels' => [ 'file' => [ // 日志记录方式 'type' => 'File', // 日志保存目录 'path' => '/data/logs/phplogs/tlog', // 单文件日志写入 'single' => false, // 独立日志级别 'apart_level' => [], // 最大日志文件数量 'max_files' => 0, // 使用JSON格式记录 'json' => false, // 日志处理 'processor' => null, // 关闭通道日志写入 'close' => false, // 日志输出格式化 'format' => '[%s][%s] %s', // 是否实时写入 'realtime_write' => false, ], // 其它日志通道配置 ], ];
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/26/thinkphp6-pei-zhi-ying-yong-de-ri-zhi-thinkphp6-5-php-7-4-9/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,测试效果:
在一个controller中中加上除0的代码,然后进行访问:
http://127.0.0.1:81/adm
查看日志:
liuhongdi@ku:/data/logs/phplogs/tlog/202012$ more 24.log
[2020-12-24T14:45:11+08:00][error] [2]Division by zero[/data/php/mytp/app/adm/controller/Index.php:11]
三,如何设置记录发生错误时的堆栈信息?
修改config/log.php
增加一项:
//记录堆栈信息 'record_trace' => true,
效果如下:
<?php // +---------------------------------------------------------------------- // | 日志设置 // +---------------------------------------------------------------------- return [ // 默认日志记录通道 'default' => env('log.channel', 'file'), // 日志记录级别 //debug, info, notice, warning, error, critical, alert, emergency,sql 'level' => ['error','critical', 'alert', 'emergency','warning'], // 日志类型记录的通道 ['error'=>'email',...] 'type_channel' => [], // 关闭全局日志写入 'close' => false, // 全局日志处理 支持闭包 'processor' => null, //记录堆栈信息 'record_trace' => true, // 日志通道列表 'channels' => [ 'file' => [ // 日志记录方式 'type' => 'File', // 日志保存目录 'path' => '/data/logs/phplogs/tlog', // 单文件日志写入 'single' => false, // 独立日志级别 'apart_level' => [], // 最大日志文件数量 'max_files' => 0, // 使用JSON格式记录 'json' => false, // 日志处理 'processor' => null, // 关闭通道日志写入 'close' => false, // 日志输出格式化 'format' => '[%s][%s] %s', // 是否实时写入 'realtime_write' => false, ], // 其它日志通道配置 ], ];
四,测试效果:
访问写有除0错代码的url:
http://127.0.0.1:81/adm
查看日志:
liuhongdi@ku:/data/logs/phplogs/tlog/202012$ more 24.log [2020-12-24T14:45:11+08:00][error] [2]Division by zero[/data/php/mytp/app/adm/controller/Index.php:11] [2020-12-24T14:50:48+08:00][error] [2]Division by zero[/data/php/mytp/app/adm/controller/Index.php:11] #0 /data/php/mytp/app/adm/controller/Index.php(11): think\initializer\Error->appError() #1 [internal function]: app\adm\controller\Index->index() #2 /data/php/mytp/vendor/topthink/framework/src/think/Container.php(343): ReflectionMethod->invokeArgs() #3 /data/php/mytp/vendor/topthink/framework/src/think/route/dispatch/Controller.php(110): think\Container->invokeReflectMethod() #4 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(59): think\route\dispatch\Controller->think\route\dispatch\{closure}() #5 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(66): think\Pipeline->think\{closure}() #6 /data/php/mytp/vendor/topthink/framework/src/think/route/dispatch/Controller.php(113): think\Pipeline->then() #7 /data/php/mytp/vendor/topthink/framework/src/think/route/Dispatch.php(89): think\route\dispatch\Controller->exec() #8 /data/php/mytp/vendor/topthink/framework/src/think/Route.php(772): think\route\Dispatch->run() #9 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(59): think\Route->think\{closure}() #10 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(66): think\Pipeline->think\{closure}() #11 /data/php/mytp/vendor/topthink/framework/src/think/Route.php(773): think\Pipeline->then() #12 /data/php/mytp/vendor/topthink/framework/src/think/Http.php(216): think\Route->dispatch() #13 /data/php/mytp/vendor/topthink/framework/src/think/Http.php(206): think\Http->dispatchToRoute() #14 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(59): think\Http->think\{closure}() #15 /data/php/mytp/vendor/topthink/think-multi-app/src/MultiApp.php(71): think\Pipeline->think\{closure}() #16 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(59): think\app\MultiApp->think\app\{closure}() #17 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(66): think\Pipeline->think\{closure}() #18 /data/php/mytp/vendor/topthink/think-multi-app/src/MultiApp.php(72): think\Pipeline->then() #19 [internal function]: think\app\MultiApp->handle() #20 /data/php/mytp/vendor/topthink/framework/src/think/Middleware.php(142): call_user_func() #21 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(85): think\Middleware->think\{closure}() #22 /data/php/mytp/vendor/topthink/think-trace/src/TraceDebug.php(71): think\Pipeline->think\{closure}() #23 [internal function]: think\trace\TraceDebug->handle() #24 /data/php/mytp/vendor/topthink/framework/src/think/Middleware.php(142): call_user_func() #25 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(85): think\Middleware->think\{closure}() #26 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(66): think\Pipeline->think\{closure}() #27 /data/php/mytp/vendor/topthink/framework/src/think/Http.php(207): think\Pipeline->then() #28 /data/php/mytp/vendor/topthink/framework/src/think/Http.php(170): think\Http->runWithRequest() #29 /data/php/mytp/public/index.php(20): think\Http->run() #30 {main}
五,查看thinkphp的版本
liuhongdi@ku:/data/php/mytp$ php think version v6.0.5
六,查看php的版本:
liuhongdi@ku:/data/logs/phplogs/tlog/202012$ php --version PHP 7.4.9 (cli) (built: Oct 26 2020 15:17:14) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.9, Copyright (c), by Zend Technologies