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 easy config of Monolog handlers #14974

Draft
wants to merge 2 commits into
base: 4.x
Choose a base branch
from

Conversation

timkelty
Copy link
Contributor

@timkelty timkelty commented May 10, 2024

Description

Adds a \craft\log\MonologTarget::$handlers to easily configure or override the handlers for Monolog.

Since this is a fairly common thing to want to (e.g. to send logs to a 3rd party service, I figured it should be a little more straightforward.

Before:

<?php

return [
    'components' => [
        'log' => function() {
            $dispatcher = new Dispatcher();
            foreach ($dispatcher->targets as $target) {
                $handler = new \Monolog\Handler\StreamHandler('php://stderr', \Monolog\Logger::ERROR);
                $target->getLogger()->setHandlers([$handler]);
            }
            return $dispatcher;
        },
    ],
];

After:

Adding a new target with only given handlers:

<?php

return [
    'components' => [
        'log' => [
            'targets' => [
                'handlers' => fn(\craft\log\MonologTarget $target) => [
                    (new \Monolog\Handler\StreamHandler(
                        'php://stderr',
                        \Monolog\Logger::ERROR,
                    ))->setFormatter($target->formatter),
                ],
             ],
        ],
    ],
];

Appending to default handlers:

<?php

return [
    'components' => [
        'log' => [
            'monologTargetConfig' => [
                'handlers' => fn(\craft\log\MonologTarget $target) => array_merge(
                    $target->getDefaultHandlers(),
                    [
                        (new \Monolog\Handler\StreamHandler(
                            'php://stderr',
                            \Monolog\Logger::ERROR,
                        ))->setFormatter($target->getFormatter()),
                    ]
                ),
            ],
        ],
    ],
];

public function getFormatter(): ?FormatterInterface
{
return $this->formatter;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should be gettable so you can set the configured formatter when setting your handler.

}
$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
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