Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,23 @@ $logger->alert('Db connect error', [
]);
```


## Using LogChannel with `stefna/di`

This will add the channel to the logger context.

This is useful to filter logs

```php
<?php declare(strict_types=1);

use Stefna\Logger\Di\Attributes\LogChannel;
use Psr\Log\LoggerInterface;

class SomeDomain {
public function __construct(
#[LogChannel('custom-channel')]
private LoggerInterface $logger
) {}
}
```
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"require-dev": {
"phpunit/phpunit": "^9",
"stefna/codestyle": "^1.12",
"stefna/di": "^1.0",
"monolog/monolog": "^3.0",
"bugsnag/bugsnag": "^3.15",
"psr/http-server-handler": "^1.0",
Expand Down
32 changes: 32 additions & 0 deletions src/Di/Attributes/LogChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace Stefna\Logger\Di\Attributes;

use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Stefna\DependencyInjection\Helper\Attribute\ConfigureAttribute;
use Stefna\DependencyInjection\Helper\Attribute\ResolverAttribute;
use Stefna\Logger\ManagerInterface;
use Stefna\Logger\Wrapper\ChannelWrapper;

#[\Attribute(\Attribute::TARGET_PARAMETER)]
final class LogChannel implements ResolverAttribute
{
public function __construct(
private readonly string $channel,
) {}

public function resolve(string $type, ContainerInterface $container): mixed
{
if ($container->has(ManagerInterface::class)) {
return $container->get(ManagerInterface::class)->getLogger($this->channel);
}
$object = $container->get($type);
if ($object instanceof LoggerInterface && class_exists(ChannelWrapper::class)) {
return new ChannelWrapper($object, $this->channel);
}

// don't know how to add the channel just return the incoming logger
return $object;
}
}
4 changes: 3 additions & 1 deletion src/Wrapper/ChannelWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public function __construct(
*/
public function log($level, string|\Stringable $message, array $context = []): void
{
$context['channel'] = $this->channel;
if (!isset($context['channel'])) {
$context['channel'] = $this->channel;
}
$this->logger->log($level, $message, $context);
}
}
48 changes: 48 additions & 0 deletions tests/Di/LogChannelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types=1);

namespace Stefna\Logger\Di;

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Stefna\DependencyInjection\Container;
use Stefna\DependencyInjection\Definition\DefinitionArray;
use Stefna\DependencyInjection\Helper\Autowire;
use Stefna\Logger\Di\Stub\CustomDomain;
use Stefna\Logger\Logger\TestLogger;

final class LogChannelTest extends TestCase
{
public function testLogChannel(): void
{
$logger = new TestLogger();
$container = new Container(new DefinitionArray([
LoggerInterface::class => fn () => $logger,
CustomDomain::class => Autowire::cls(),
]));

$domain = $container->get(CustomDomain::class);
$domain->logger->info('test');

$logEntry = $logger->getLogAt(0);
$this->assertArrayHasKey('channel', $logEntry['context']);
$this->assertSame('test-channel', $logEntry['context']['channel']);
}

public function testLogChannelDontOverrideManuallySetChannel(): void
{
$logger = new TestLogger();
$container = new Container(new DefinitionArray([
LoggerInterface::class => fn () => $logger,
CustomDomain::class => Autowire::cls(),
]));

$domain = $container->get(CustomDomain::class);
$domain->logger->info('test', [
'channel' => 'manual-channel',
]);

$logEntry = $logger->getLogAt(0);
$this->assertArrayHasKey('channel', $logEntry['context']);
$this->assertSame('manual-channel', $logEntry['context']['channel']);
}
}
14 changes: 14 additions & 0 deletions tests/Di/Stub/CustomDomain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Stefna\Logger\Di\Stub;

use Psr\Log\LoggerInterface;
use Stefna\Logger\Di\Attributes\LogChannel;

final class CustomDomain
{
public function __construct(
#[LogChannel('test-channel')]
public LoggerInterface $logger,
) {}
}