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://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: 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): thinkinitializerError->appError()
#1 [internal function]: appadmcontrollerIndex->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): thinkContainer->invokeReflectMethod()
#4 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(59): think
outedispatchController->think
outedispatch{closure}()
#5 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(66): thinkPipeline->think{closure}()
#6 /data/php/mytp/vendor/topthink/framework/src/think/route/dispatch/Controller.php(113): thinkPipeline->then()
#7 /data/php/mytp/vendor/topthink/framework/src/think/route/Dispatch.php(89): think
outedispatchController->exec()
#8 /data/php/mytp/vendor/topthink/framework/src/think/Route.php(772): think
outeDispatch->run()
#9 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(59): thinkRoute->think{closure}()
#10 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(66): thinkPipeline->think{closure}()
#11 /data/php/mytp/vendor/topthink/framework/src/think/Route.php(773): thinkPipeline->then()
#12 /data/php/mytp/vendor/topthink/framework/src/think/Http.php(216): thinkRoute->dispatch()
#13 /data/php/mytp/vendor/topthink/framework/src/think/Http.php(206): thinkHttp->dispatchToRoute()
#14 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(59): thinkHttp->think{closure}()
#15 /data/php/mytp/vendor/topthink/think-multi-app/src/MultiApp.php(71): thinkPipeline->think{closure}()
#16 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(59): thinkappMultiApp->thinkapp{closure}()
#17 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(66): thinkPipeline->think{closure}()
#18 /data/php/mytp/vendor/topthink/think-multi-app/src/MultiApp.php(72): thinkPipeline->then()
#19 [internal function]: thinkappMultiApp->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): thinkMiddleware->think{closure}()
#22 /data/php/mytp/vendor/topthink/think-trace/src/TraceDebug.php(71): thinkPipeline->think{closure}()
#23 [internal function]: think	raceTraceDebug->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): thinkMiddleware->think{closure}()
#26 /data/php/mytp/vendor/topthink/framework/src/think/Pipeline.php(66): thinkPipeline->think{closure}()
#27 /data/php/mytp/vendor/topthink/framework/src/think/Http.php(207): thinkPipeline->then()
#28 /data/php/mytp/vendor/topthink/framework/src/think/Http.php(170): thinkHttp->runWithRequest()
#29 /data/php/mytp/public/index.php(20): thinkHttp->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
原文地址:https://www.cnblogs.com/architectforest/p/14184091.html