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

lshfong

问题描述

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

370 2 3
2个回答

胡桃

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 28天前

    可以了,感谢

  • lshfong 28天前

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

  • forwebreg 16天前
            [
                '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],
                ],
            ]
        ],
    ],
];
×
🔝