Error on overriding ConfigureLogging bootstrap class

Published 2 years ago by aspiresuvedi

While trying to implement logentries on L5, I tried to override ConfigureLogging class. Firstly , I override the $bootstrappers property of HttpKernel in Kernel class. using

protected $bootstrappers = [
        'App\Bootstrap\ConfigureLogging',
        'Illuminate\Foundation\Bootstrap\DetectEnvironment',
        'Illuminate\Foundation\Bootstrap\LoadConfiguration',
        'Illuminate\Foundation\Bootstrap\HandleExceptions',
        'Illuminate\Foundation\Bootstrap\RegisterFacades',
        'Illuminate\Foundation\Bootstrap\RegisterProviders',
        'Illuminate\Foundation\Bootstrap\BootProviders',
    ];

My App\Bootstrap\ConfigureLogging class to implement logentries looks like

<?php namespace App\Bootstrap;

use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\ConfigureLogging as BaseConfigureLogging;
use Monolog\Handler\LogEntriesHandler;

class ConfigureLogging extends BaseConfigureLogging {

    /**
     * Custom Monolog handler that for Logentries.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @param  \Illuminate\Log\Writer  $log
     * @return void
     */
    protected function configureCustomHandler(Application $app, Writer $log)
    {
        $logentriesHandler = new LogEntriesHandler(getenv('LOGENTRIES_TOKEN'));
        $log->getMonolog()->pushHandler($logentriesHandler);
        // Also Log to Dayily files too.
        $log->useDailyFiles($app->storagePath().'/logs/laravel.log', 5);
    }

}

But it throws error like this

Fatal error: Uncaught exception 'ReflectionException' with message 'Class log does not exist' in /var/www/project/vendor/laravel/framework/src/Illuminate/Container/Container.php

Best Answer (As Selected By aspiresuvedi)
aspiresuvedi

Actually , what I did above was all correct and I need to do additional interface binding for ConfigureLogging class in bootstrap/app.php which would otherwise fail on composer update

$app->singleton(
    'Illuminate\Foundation\Bootstrap\ConfigureLogging',
    'App\Bootstrap\ConfigureLogging'
);

also I changed the log key on config/app.php to custom and it worked.

toltech

Ok, a few thoughts...

  1. If you're going to extend the Illuminate\Foundation\Bootstrap\ConfigureLogging class, you need to override the configureHandlers() method it implements in order to add your custom handlers. The only way your current code could work is if you had edited the BaseConfigureLogging class to call $this->configureCustomHandlers($app, $this->registerLogger($app)); (line 17) - which would be a BAD idea. Your other choice is to just copy the entire BaseConfigureLogging Class to the bootstrap directory and make any changes you want there. But I think extending is fine - just change configureCustomHandlers to configureHandlers.
  2. Next make sure you've registered your App/Bootstrap/ namespace in composer.json and ran composer dumpautoload

I have this code which is working for me:

<?php namespace Bootstrap;

use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\ConfigureLogging as BaseConfigureLogging;
use Monolog\Handler\StreamHandler;

class ConfigureLogging extends BaseConfigureLogging{

    /**
     * OVERRIDE PARENT
     * Configure the Monolog handlers for the application.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @param  \Illuminate\Log\Writer  $log
     * @return void
     */
    protected function configureHandlers(Application $app, Writer $log)
    {
        $log->getMonolog()->pushHandler( new StreamHandler( storage_path() . "/logs/oneOff.log"));
        $log->useDailyFiles($app->storagePath().'/logs/daily.log');
    }
}
toltech

if this solved your issue please mark it as the correct answer :)

aspiresuvedi

Actually , what I did above was all correct and I need to do additional interface binding for ConfigureLogging class in bootstrap/app.php which would otherwise fail on composer update

$app->singleton(
    'Illuminate\Foundation\Bootstrap\ConfigureLogging',
    'App\Bootstrap\ConfigureLogging'
);

also I changed the log key on config/app.php to custom and it worked.

Bardia

What does stream handler do? And in my case, I'm using Loggly, why do you need to have a Stream Handler when using Loggly Handler?

Sign In or create a forum account to participate in this discussion.