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

Merged
merged 12 commits into from
Nov 2, 2024
Merged

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())
                    ),
            ],
        ],
    ],
];

src/log/MonologTarget.php Outdated Show resolved Hide resolved
@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
src/log/MonologTarget.php Outdated Show resolved Hide resolved
@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
@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
@brandonkelly brandonkelly changed the base branch from 4.x to 4.13 November 2, 2024 14:15
@brandonkelly brandonkelly merged commit 0f0ecd7 into 4.13 Nov 2, 2024
@brandonkelly brandonkelly deleted the feature/configure-monolog-handlers branch November 2, 2024 14:32
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.

2 participants