Override format log

2k views Asked by At

I need to override format log on project made with Laravel 5.3

I use as a guide this post but does not work.

First create a bootstrap/ConfigureLogging.php class

<?php
namespace Bootstrap;

use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger as Monolog;
use Monolog\Formatter\LineFormatter;
use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\ConfigureLogging as BaseConfigureLogging;
use Monolog\Handler\StreamHandler;
use Carbon\Carbon;

class ConfigureLogging extends BaseConfigureLogging
{
     /**
     * Configure the Monolog handlers for the application.
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @param  \Illuminate\Log\Writer  $log
     * @return void
     */
    protected function configureDailyHandler(Application $app, Writer $log)
    {
        $config = $app->make('config');
        $maxFiles = $config->get('app.log_max_files');

        // Formatting
        // the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
        $logFormat = "[%datetime%] [%level_name%]: %message% %context% %extra%\n";
        $formatter = new LineFormatter($logFormat);
        //$logStreamHandler->setFormatter($formatter); // Here I'm lost


        $log->useDailyFiles(
            $app->storagePath().'/logs/cprsync.log', is_null($maxFiles) ? 5 : $maxFiles, $config->get('app.log_level', 'debug')
        );
}

This change (override) name of laravel logs for my app. But when I try to add code for override format, I'm lost, and code on original post not work.

I see code for Monolog, and I see that I need send my format, but I don't know.

1

There are 1 answers

2
Saumini Navaratnam On BEST ANSWER

The link you are referring should work to my knowledge. You are missing pushHandler

protected function configureDailyHandler(Application $app, Writer $log) {
    $config   = $app->make('config');
    $maxFiles = $config->get('app.log_max_files');
    $path     = $app->storagePath().'/logs/cprsync.log';

    // Daily handler
    $handler = new RotatingFileHandler($path, is_null($maxFiles) ? 5 : $maxFiles,
        $config->get('app.log_level', 'debug'));

    // Formatting
    $logFormat = "%datetime% [%level_name%] (%channel%): %message% %context% %extra%\n";
    $formatter = new LineFormatter($logFormat);
    $handler->setFormatter($formatter);

    // Push handler
    $log->getMonolog()->pushHandler($handler);
}