Skip to content

Allow easier Monolog config#14974

Merged
brandonkelly merged 12 commits into4.13from
feature/configure-monolog-handlers
Nov 2, 2024
Merged

Allow easier Monolog config#14974
brandonkelly merged 12 commits into4.13from
feature/configure-monolog-handlers

Conversation

@timkelty
Copy link
Copy Markdown
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())
                    ),
            ],
        ],
    ],
];

Comment thread src/log/MonologTarget.php
Comment thread src/log/MonologTarget.php Outdated
@timkelty timkelty requested a review from brandonkelly May 10, 2024 16:51
@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
Comment thread src/log/MonologTarget.php Outdated
@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
Comment thread src/log/MonologTarget.php
@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
[ci skip]
[ci skip]
@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