简体   繁体   中英

Laravel 5.5 fix for newline in laravel.log when reporting errors with file path

I just noticed a problem in the laravel.log when errors are reported. Laravel or Monolog is inserting a carriage return in the file path because my application directory starts with the letter "r". This is new. I have other applications that start with 'r' and they have the same problem. I'm sure it has something to do my environment. I'm using Windows 10, Laragon, Laravel 5.5, git, github, and PhpStorm.

The name of the directory is rli-ccm. So you can see that the \rli-ccp is replaced and a carriage return is inserted before \routes.

[2018-01-30 10:51:10] testing.ERROR: Method [middleware] does not exist on 
view. {"exception":"[object] (BadMethodCallException(code: 0): Method 
[middleware] does not exist on view. at C:\\laragon\\www\
li-ccm\\vendor\\laravel\\framework\\src\\Illuminate\\View\\View.php:399)
[stacktrace]
#0 C:\\laragon\\www\
li-ccm\
outes>\\web.php(17): Illuminate\\View\\View->__call('middleware', Array)

Not to easy to read the log file like this. I considered reformatting the log but thought I would ask first. Any ideas would be appreciated. This is what the file looks like in notepad with end of line characters.

在此处输入图像描述

I've faced more or less the same problem - in my app i need to keep all logs entries as a single lines, but after upgrading laravel to 5.5 monolog started to write data into log files without replacing /r/n . This is caused by getDefaultFormatter() method in Illuminate\Log\Writer :

protected function getDefaultFormatter()
{
    return tap(new LineFormatter(null, null, true, true), function ($formatter) {
        $formatter->includeStacktraces();
    });
}

Where 3rd argument stands for allowing line breaks. My solution is to remove all line breaks in context and message. I'm doing other manipulations with logs, so i've created service provider to encapsualate there that logic, here is the part of code which makes log entry to be a single line:

class LoggerServiceProvider extends ServiceProvider
{
    public function boot()
    {
    }

    public function register()
    {
        \Log::getMonolog()->pushProcessor(function ($record) {
            $record['context'] = $this->sanitizeContext($record['context']);
            $record['message'] = remove_line_breaks($record['message']);
            return $record;
        });
    }

    private function sanitizeContext(array $context = []): array
    {
        array_walk_recursive($context, function (&$value) {
            if (is_string($value)) {
                $value = remove_line_breaks($value);
            }
        });
        return $context;
    }
}

where remove_line_breaks() is tiny helper, which looks like

function remove_line_breaks(string $item): string
{
    return str_replace(["\r\n", "\r", "\n"], ' ', $item);
}

Hope it would be useful

I fixed my problem by edit the replaceNewline function in the Monolog LineFormatter to do the following.

protected function replaceNewlines($str)
{
    if ($this->allowInlineLineBreaks) {
        if (0 === strpos($str, '{')) {
            return str_replace(array('\r', '\n'), array("\r", "\n"), str_replace(array("\\\\"), "/", $str));
        }

        return str_replace(array("\\\\"), "/", $str);
    }

    return str_replace(array("\r\n", "\r", "\n"), ' ', str_replace(array("\\\\"), "/", $str));
}

A more permanent fix would be to somehow manipulate the file & trace on the exception object before it is passed to parent::report($exception) in the handler.

update app/logging.php file , add formatter and formatter_with , like this:

<?php
use Monolog\Handler\StreamHandler;
return [
    'default' => env('LOG_CHANNEL', 'syslog'),
    'channels' => [
        'syslog' => [
            'driver' => 'syslog',
            'level' => 'debug',
            'formatter' => Monolog\Formatter\LineFormatter::class,
            'formatter_with' => [
                'allowInlineLineBreaks' => false
            ]
        ]
    ]
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM