Laravel stdout log在日志中添加额外的关键字[重复]

cyvaqqii  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(85)

此问题已存在

Laravel json log with info/warn keywords
10天前关闭。
我已经尝试了内置通道和自定义通道,以及每当我在终端/stdout WARN关键字之前添加日志的东西。
Laravel log in terminal
但出现的日志与日志文件Laravel log in file中应有的日志相同
在我的用例中,我需要终端中的JSON格式的日志,但由于关键字,它是字符串。
这是日志记录方法(我已经尝试了相同的内置通道,如单...但相同)

'methodlog' => [
            'driver' => 'custom',
            'via' => App\Logging\MethodLogChannel::class,
            'formatter' => App\Logging\CustomJsonFormatter::class,
            'name' => 'methodlog',
            'with' => [
                'path' => storage_path('logs/laravel.log'),//'php://stdout',
                'level' => env('LOG_LEVEL', 'debug'),
            ],
        ],

编辑:CustomJsonFormatter.php

class CustomJsonFormatter extends JsonFormatter
{
    private $DEFAULT_JSON_FLAGS = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION | JSON_INVALID_UTF8_SUBSTITUTE | JSON_PARTIAL_OUTPUT_ON_ERROR ;
    public function format(LogRecord $record): string
    {
        $formatted = $record['context'];

        if ($this->includeStacktraces) {
            $formatted['stack_trace'] = $record['formatted']['exception'];
        }

        return $this->toJson($formatted, true) . ($this->appendNewline ? "\n" : '');
    }
}

MethodLogChannel.php

class MethodLogChannel
{
    public function __invoke(array $config)
    {
        $log = new Logger($config['name']);
        $handler = new StreamHandler($config['with']['path'], $config['with']['level']);
        $handler->setFormatter(new CustomJsonFormatter());
        $log->pushHandler($handler);
        $log->pushProcessor(new IntrospectionProcessor());
        $log->pushProcessor(new WebProcessor($_SERVER));
        return $log;
    }
}

用于保存日志
Log::channel('methodlog ')->info('',$log_message);

toe95027

toe950271#

您可以尝试自定义此频道的格式:

'formatter' => Monolog\Formatter\LineFormatter::class,
    'formatter_with' => [
        'format' => "%message%",
    ],

这将删除其他匹配器,例如:

  • %datetime%
  • %channel%
  • %level_name%(这是您的问题中的一个)
  • %message%
  • %context%
  • %extra%

相关问题