webman:配置日志(v1.5.7)

发布时间 2023-08-23 09:47:57作者: 刘宏缔的架构森林
 

一,官方文档:

https://www.workerman.net/doc/webman/log.html

二,代码:

1,配置:

config/log.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */
 
return [
    'default' => [
        'handlers' => [
            [
                'class' => Monolog\Handler\RotatingFileHandler::class,
                'constructor' => [
                    '/data/logs/webmanlogs/webman.log',
                    7, //$maxFiles
                    Monolog\Logger::DEBUG,
                ],
                'formatter' => [
                    'class' => Monolog\Formatter\LineFormatter::class,
                    'constructor' => [null, 'Y-m-d H:i:s', true],
                ],
            ]
        ],
    ],
 
    // log2通道
    'logBusiness' => [
        // 处理logBusiness通道的handler,可以设置多个
        'handlers' => [
            [
                // handler类的名字
                'class' => Monolog\Handler\RotatingFileHandler::class,
                // handler类的构造函数参数
                'constructor' => [
                    '/data/logs/webmanlogs/business.log',
                    Monolog\Logger::DEBUG,
                ],
                // 格式相关
                'formatter' => [
                    // 格式化处理类的名字
                    'class' => Monolog\Formatter\LineFormatter::class,
                    // 格式化处理类的构造函数参数
                    'constructor' => [ null, 'Y-m-d H:i:s', true],
                ],
            ]
        ],
    ],
];

2,调用:

ImageController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
namespace app\controller;
 
use support\Request;
use app\result\Result;
use support\Log;
 
class ImageController
{
    //图片的详情
    public function detail(Request $request)
    {
 
         //写日志,指定通道
         $log = Log::channel('logBusiness');
         $log->info('logBusiness test:测试记录一行日志');
         //返回
         $res = ['url'=>'https://wx3.sinaimg.cn/mw690/7f8d08b2gy1hh0qy7n57qj213y0u0wj2.jpg'];
         return  Result::Success($res);
    }

三,测试效果:

访问detail这个url后,

查看日志内容:

[2023-08-19 14:49:57] logBusiness.INFO: logBusiness test:测试记录一行日志 [] []

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/08/19/webman-pei-zhi-ri-zhi/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com

四,查看webman的版本:

liuhongdi@lhdpc:/data/webman/imageadmin$ composer show workerman/webman-framework
name     : workerman/webman-framework
descrip. : High performance HTTP Service Framework.
keywords : High Performance, http service
versions : * v1.5.7
...