日志如何同时打印在控制台

lshfong

问题描述

Log::info('输出日志');
记录日志的时候如何同时打印在控制台

430 3 4
3个回答

胡桃

config/log.php

<?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' => [
                    runtime_path() . '/logs/webman.log',
                    7, //$maxFiles
                    Monolog\Logger::DEBUG,
                ],
                'formatter' => [
                    'class' => Monolog\Formatter\LineFormatter::class,
                    'constructor' => [null, 'Y-m-d H:i:s', true],
                ],
            ],
            [
                'class' => \Monolog\Handler\StreamHandler::class,
                'constructor' => [
                    STDOUT,
                ],
            ]
        ],
    ],
];
  • lshfong 2024-09-17

    可以了,感谢

  • lshfong 2024-09-17

    打印的日志能设置颜色吗,我想把各类日志区分一下

  • forwebreg 23天前
            [
                'class' => \Monolog\Handler\StreamHandler::class,
                'constructor' => [
                    STDOUT,
                ],
            ]

    进程守护模式下 好像不能加这个 要不然进程全部busy了

胡桃

app/formatter/MyFormatter.php

declare(strict_types=1);

namespace app\formatter;

use Monolog\Formatter\LineFormatter;
use Monolog\LogRecord;
use Psr\Log\LogLevel;

class MyFormatter extends LineFormatter
{
    const string ERROR = "\033[31m";
    const string INFO = "\033[32m";
    const string END = "\033[0m";

    public function format(LogRecord $record): string
    {
        $vars = $this->normalizeRecord($record);
        $color = match (strtolower($vars['level_name'])) {
            LogLevel::INFO => self::INFO,
            LogLevel::ERROR => self::ERROR,
            default => '',
        };
        return $color . parent::format($record) . self::END;
    }
}

config/log.php

<?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' => [
                    runtime_path() . '/logs/webman.log',
                    7, //$maxFiles
                    Monolog\Logger::DEBUG,
                ],
                'formatter' => [
                    'class' => Monolog\Formatter\LineFormatter::class,
                    'constructor' => [null, 'Y-m-d H:i:s', true],
                ],
            ],
            [
                'class' => \Monolog\Handler\StreamHandler::class,
                'constructor' => [
                    STDOUT,
                ],
                'formatter' => [
                    'class' => \app\formatter\MyFormatter::class,
                    'constructor' => [null, 'Y-m-d H:i:s', true],
                ],
            ]
        ],
    ],
];
hlw101

config/log.php

<?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' => [
                    runtime_path() . '/logs/webman.log',
                    7, //$maxFiles
                    Monolog\Logger::DEBUG,
                ],
                'formatter' => [
                    'class' => Monolog\Formatter\LineFormatter::class,
                    'constructor' => [null, 'Y-m-d H:i:s', true],
                ],
            ],
            [
                'class' => \Monolog\Handler\StreamHandler::class,
                'constructor' => [
                    STDOUT,
                ],
                'formatter' => [
                    'class' => common\LogFormatter::class,
                    'constructor' => [null, 'Y-m-d H:i:s', true],
                ],
            ]
        ],
    ],
];

创建文件 common\LogFormatter.php

<?php
declare(strict_types=1);

namespace common;

use Monolog\Formatter\LineFormatter;
use Monolog\LogRecord;
use Psr\Log\LogLevel;

class LogFormatter extends LineFormatter
{
    const ERROR = "\033[31m";
    const INFO = "\033[32m";
    const WARNING = "\033[33m";
    const END = "\033[0m";

    public function format(LogRecord|array $record): string
    {
        $color = match (strtolower($record['level_name'])) {
            strtolower(LogLevel::INFO) => self::INFO,
            strtolower(LogLevel::WARNING) => self::WARNING,
            strtolower(LogLevel::ERROR) => self::ERROR,
            default => '',
        };
        return $color . parent::format($record) . self::END;
    }
}
  • 暂无评论
×
🔝