laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.31k stars 10.95k forks source link

Log error for failed job with line break / carriage return #43961

Closed toto975 closed 2 years ago

toto975 commented 2 years ago

Description:

When the error text contains \r, it's doing a line break in the error file, like this :

The error text is

#0 C:\\path\\of\\Laravel\\releases\\3.0.0\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Bootstrap\\HandleExceptions.php(231): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'file_get_conten...', 'D:\\\\Inetpub\\\\wwwr...', 75)

In the log file there is :

#0 C:\\path\\of\\Laravel\
eleases\\3.0.0\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Bootstrap\\HandleExceptions.php(231): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'file_get_conten...', 'D:\\\\Inetpub\\\\wwwr...', 75)

Steps To Reproduce:

Run a job and fall in failed job

I did some search and found the parameters to put in config\logging.php

        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
            'days' => 14,
            'formatter' => Monolog\Formatter\LineFormatter::class,     <======= add this line
            'formatter_with' => [                    <======= add this line
                'allowInlineLineBreaks' => false         <======= add this line
            ]

It's Ok for error in controller, view, repositories, etc. But not when a job failed.

Same thing happens for emergency log, even if there is in config\logging.php

        'emergency' => [
            'path' => storage_path('logs/laravel.log'),
            'formatter' => Monolog\Formatter\LineFormatter::class,
            'formatter_with' => [
                'allowInlineLineBreaks' => false
            ]
        ],
driesvints commented 2 years ago

Heya, thanks for reporting.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as separate commits on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

toto975 commented 2 years ago

Hello,

I understand that you need more info and code. I work on a corporate intranet, so it's hard to share all of my code with you.

Here is a part of the code for the job :

<?php

namespace App\Jobs\Portail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

use Throwable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Mail;

class ProcessPortail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * The number of times the job may be attempted.
     *
     * @var int
     */
    public $tries = 5;

    /**
     * The number of seconds the job can run before timing out.
     *
     * @var int
     */
    public $timeout = 3600;

    /**
     * Indicate if the job should be marked as failed on timeout.
     *
     * @var bool
     */
    public $failOnTimeout = true;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //Pour ne pas mettre dans un même file tous les jobs, on précise ici son nom
        $this->onQueue('portail');
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $result = file_get_contents('https://myweb_web_site_which_cant_be_reached')  <== Put a website which doesn't exist to fail the job
    }

    /**
     * Handle a job failure.
     *
     * @param  \Throwable  $exception
     * @return void
     */
    public function failed(Throwable $exception)
    {
        mail('my_mail@dot.com', 'error job', $exception->getMessage());
    }
}

For emergency log, i comment all the channels in config\logging.php, except the emergency channel

Hope this helps you with debugging.

driesvints commented 2 years ago

Please create a new repo to reproduce the problem with minimal code as possible. Otherwise I can't help you, sorry.

driesvints commented 2 years ago

Feel free to re-open once the repo is created.