Changing the Laravel Log File Name (and playing in the Http/Console Kernels)

Here's a video explaining the process in Laravel 5.3:


What I had to do was edit two files:

  1. app/Http/Kernel.php
  2. app/Console/Kernel.php

Within each, I over-rode the __construct method from the parent class, in order to edit the $bootstrapper array - the array of class names telling Laravel which to load and bootstrap for Http vs Console requests.

Each of these loaded in the ConfigureLogging bootstrap class. This class (Illuminate\Foundation\Bootstrap\ConfigureLogging) hard-coded the laravel log file name.

App\Http\Kernel.php

namespace App\Http;

use Illuminate\Routing\Router;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel {
    // boiler plate removed

    // over-ride parent __construct method
    public function __construct(Application $app, Router $router)
    {
        parent::__construct($app, $router);

        // Replace default logger with HelpSpot Logger
        $loggingKey = array_search('Illuminate\Foundation\Bootstrap\ConfigureLogging', $this->bootstrappers);
        $this->bootstrappers[$loggingKey] = 'App\ConfigureLogging';
    }
}

App\Console\Kernel.php

<?php

namespace App\Console;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel {
     // boiler plate removed

    // over-ride parent __construct method
    public function __construct(Application $app, Dispatcher $events)
    {
        parent::__construct($app, $events);

        // Replace default logger with HelpSpot Logger
        $loggingKey = array_search('Illuminate\Foundation\Bootstrap\ConfigureLogging', $this->bootstrappers);
        $this->bootstrappers[$loggingKey] = 'App\ConfigureLogging';
    }
}

App\ConfigureLogging

Then we can make our own class that extends the base ConfigureLogging class and tweaks it as needed:

<?php

namespace App;

use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\ConfigureLogging as BaseLoggingBootstrapper;

class ConfigureLogging extends BaseLoggingBootstrapper
{
    /**
     * Configure the Monolog handlers for the application.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @param  \Illuminate\Log\Writer  $log
     * @return void
     */
    protected function configureSingleHandler(Application $app, Writer $log)
    {
        $log->useFiles(
            $app->storagePath().'/logs/my-app.log',
            $app->make('config')->get('app.log_level', 'debug')
        );
    }

    /**
     * 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');

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

    /**
     * Configure the Monolog handlers for the application.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @param  \Illuminate\Log\Writer  $log
     * @return void
     */
    protected function configureSyslogHandler(Application $app, Writer $log)
    {
        $log->useSyslog(
            'my-app',
            $app->make('config')->get('app.log_level', 'debug')
        );
    }
}

And there we have it - our log file will be named my-app.log instead of laravel.log.

comments powered by Disqus