Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow easier Monolog config #14974

Open
wants to merge 5 commits into
base: 4.x
Choose a base branch
from
Open

Conversation

timkelty
Copy link
Contributor

@timkelty timkelty commented May 10, 2024

Description

The most common scenario is to want to add a Monolog handler for a third party service. This is an attempt to make that a bit easier.

As it is, MonologTarget is very much geared toward Craft's use of it and is a bit difficult to extend. One could always just add a target using samdark\log\PsrTarget, (which MonologTarget extends), but you miss out on the default formatting and processors that Craft applies.

This PR allows MonologTarget to be constructed with a Closure for the logger property, allowing you to override anything.

Example: Add a custom log target with your own Monolog handlers and Craft's default formatting

Before:
<?php
return [
    'components' => [
        'log' => function() {
            $target = new \craft\log\MonologTarget(['name' => 'myTarget']);
            $target->getLogger()->setHandlers([
                (new \Monolog\Handler\StreamHandler('php://stderr', \Monolog\Logger::ERROR))
                    ->setFormatter($target->getFormatter()),
            ]);
        
            $dispatcher = new \craft\log\Dispatcher();
            $dispatcher->targets[] = $target;
        
            return $dispatcher;
        },
    ],
];
After:
<?php
// After
return [
    'components' => [
        'log' => [
            'targets' => [
                [
                    'class' => \craft\log\MonologTarget::class,
                    'name' => 'myTarget',
                    'logger' => fn(\craft\log\MonologTarget $target) => $target
                        ->getLogger()
                        ->setHandlers([
                            (new \Monolog\Handler\StreamHandler('php://stdout'))
                                ->setFormatter($target->getFormatter()),
                        ]
                    ),
                ]
            ]
        ],
    ],
];
…or, push a handler onto Craft's default log targets:
<?php
return [
    'components' => [
        'log' => [
            'monologTargetConfig' => [
                'logContext' => false,
                'logger' => fn(\craft\log\MonologTarget $target) => $target
                    ->getLogger()
                    ->pushHandler(
                        (new \Monolog\Handler\StreamHandler('php://stdout'))
                            ->setFormatter($target->getFormatter())
                    ),
            ],
        ],
    ],
];

}
$handlers = $this->handlers instanceof Closure
? ($this->handlers)($this)
: $this->handlers;
Copy link
Contributor Author

@timkelty timkelty May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If $this->handlers is an array, we take it as-is.

This makes the most intuitive sense to me, however, it isn't how \craft\log\Dispatcher::$targets works, where they are appended to the defaults. Happy to change it either way.

@timkelty timkelty force-pushed the feature/configure-monolog-handlers branch from 418b7f2 to 87b6e5f Compare May 13, 2024 16:08
@timkelty timkelty changed the base branch from 5.x to 4.x May 13, 2024 16:08

return $handlers;
}

Copy link
Contributor Author

@timkelty timkelty May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getDefaultHandlers is not static because it needs access to $this->formatter and $this->name.

Its intended use is to be referenced in handlers when a Closure is used (when you want to append a handler to the defaults).

@timkelty timkelty marked this pull request as draft May 28, 2024 15:13
@timkelty timkelty changed the title Allow easy config of Monolog handlers Allow easier Monolog config Jun 6, 2024
{
return $this->name;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds getters for all these so they can be used in the Logger closure

@timkelty timkelty requested review from brandonkelly and removed request for brandonkelly June 6, 2024 20:33
@timkelty timkelty marked this pull request as ready for review June 6, 2024 20:33
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.

None yet

1 participant