Steve Bauman
Steve Bauman

Reputation: 8668

Laravel Log Events

Does Laravel 5.1 broadcast any Logging events that you're able to listen for? For example if a Log entry is created and saved into the log file, does it give the information that's going to be inserted into the log? I can't find any information on the topic, so any help would be great, thanks!

EDIT:

Searched through the Laravel log writer source. Discovered that an event is fired for all log events named illuminate.log, which is then caught by the writer.

https://github.com/laravel/framework/blob/5.1/src/Illuminate/Log/Writer.php#L295

When exceptions are thrown, a log entry is created, however it doesn't seem the event is dispatched when this happens. Only when you explicitly call the log will the event be dispatched. For example the output of this is Log was written to:

Route::get('/', function () {

    Event::listen('illuminate.log', function()
    {
        die('Log was written to');
    });

    Log::info('info');

    throw new Exception('Exception');
});

However, this just displays the Exception:

Route::get('/', function () {

    Event::listen('illuminate.log', function()
    {
        die('Log was written to');
    });

    throw new Exception('Exception');
});

I'm not exactly sure if this is a bug or not? I'm guessing not, and if that's the case, does anyone have any tips on doing this? I just need to catch when the log is being written to and save the information along with it.

Upvotes: 2

Views: 9609

Answers (2)

Steve Bauman
Steve Bauman

Reputation: 8668

Adding a monolog handler through the laravel Log writer ended up being the way to go here. I created a LoggerServiceProvider with the following:

namespace App\Providers;

use Monolog\Logger;
use Illuminate\Log\Writer;
use Illuminate\Support\ServiceProvider;

class LoggerServiceProvider extends ServiceProvider
{
    /**
     * Push the eloquent handler to monolog on boot.
     */
    public function boot()
    {
        $logger = app('log');

        // Make sure the logger is a Writer instance
        if($logger instanceof Writer) {
            $monolog = $logger->getMonolog();

            // Make sure the Monolog Logger is returned
            if($monolog instanceof Logger) {
                // Create your custom handler
                $handler = new App\Handlers\MyCustomHandler();

                // Push it to monolog
                $monolog->pushHandler($handler);
            }
        }
    }

    /**
     * Register bindings in the container.
     */
    public function register()
    {
        //
    }
}

I'd suggest just extending Monologs AbstractHandler like so:

namespace App\Handlers\MyCustomHandler;

use Monolog\Handler\AbstractHandler;

class MyCustomHandler extends AbstractHandler
{
    /**
     * Handle the log record.
     *
     * @param array $record
     *
     * @return bool
     */
    public function handle(array $record = [])
    {
        // Do something with the $record array
    }
}

Then insert your new service provider in your config/app.php file.

Upvotes: 3

stef
stef

Reputation: 1528

If exceptions aren't handling this, then i'd suggest subclassing the logger, and overriding the writeLog method to do handle all of the log actions, then parent::writeLog() to keep the logging.

Then register that with the log, Psr\Log\LoggerInterface, and Illuminate\Contracts\Logging\Log keys in the application container.

Upvotes: 0

Related Questions