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): 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM