Skip to content

[5.6] More flexibility for custom log drivers#23184

Merged
taylorotwell merged 1 commit into
5.6from
custom-log-callable
Feb 16, 2018
Merged

[5.6] More flexibility for custom log drivers#23184
taylorotwell merged 1 commit into
5.6from
custom-log-callable

Conversation

@GrahamCampbell

@GrahamCampbell GrahamCampbell commented Feb 15, 2018

Copy link
Copy Markdown
Collaborator

I love the ability to have custom log drivers in config, without needing to call extend on drivers, and it reminded me of something very similar I was doing in an app as a way of specifying lazy values in config, by using callables (in particular, using the array notation since we cannot serialize closures).

So, the example at https://laravel.com/docs/5.6/logging#creating-custom-channels is great, and still works, but the following example now works also:

'channels' => [
    'custom' => [
        'driver' => 'custom',
        'via' => [App\Logging\MyCustomLogger::class, 'make'],
    ],
],

with a static function:

<?php

namespace App\Logging;

use Monolog\Logger;

class MyCustomLogger
{
    public static function make(array $config)
    {
        return new Logger(...);
    }
}

This is very simple, and you don't need to go through the service container. Moreover, you don't need to create a special class with an invoke method, you can just add a static method wherever you feel, even on the logger class itself.

@sisve

sisve commented Feb 16, 2018

Copy link
Copy Markdown
Contributor

Perhaps add a test?

@taylorotwell taylorotwell merged commit 71d7be9 into 5.6 Feb 16, 2018
@taylorotwell taylorotwell deleted the custom-log-callable branch February 22, 2018 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants