Skip to content

Commit

Permalink
Add group()
Browse files Browse the repository at this point in the history
  • Loading branch information
lozovoyv committed Feb 2, 2019
1 parent 2055f02 commit 737a466
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -49,7 +49,8 @@ Configuration array consists of two keys. Value of `'default'` must contain name
(or array of names) of logger to be used as default logger. `'loggers'` is a set
of loggers to be used keyed by name. Required parameter of each logger is a
`'driver'` containing class name of logger to be used with corresponding name
(See examples below).
(See examples below). Third additional key `'groups'` contain array of driver
groups keyed by name.

Log manager extends [container](https://github.com/opxcore/container), so loggers
will be resolved by it with all dependency injections. All loggers will be resolved
Expand All @@ -74,6 +75,8 @@ In both cases `LoggerProxy` class with chosen loggers bindings will be returned,
so `$manager->driver([$name1, $name2])->log($message)` will call log action on
each of logger.

To use group logging use `$manager->group($name)` or `$manager->group([$name1, $name2])`.

## PSR-3
Log manager implements PSR-3. So available methods are:
```php
Expand Down Expand Up @@ -105,6 +108,10 @@ $config = [
'driver' => \OpxCore\Log\LogNull::class,
]
],
'groups' => [
'local' => ['file', 'null'],
'network' => ['email'],
],
];
```
Logger class:
Expand Down
24 changes: 24 additions & 0 deletions src/LogManager.php
Expand Up @@ -193,6 +193,30 @@ public function driver($names = null): LoggerInterface
return new LoggerProxy($drivers);
}

/**
* Resolve a group of loggers.
*
* @param string|array $names
*
* @return \Psr\Log\LoggerInterface
*
* @throws \OpxCore\Log\Exceptions\LogManagerException
*/
public function group($names): LoggerInterface
{
$drivers = [[]];

foreach ((array)$names as $name) {
if (!isset($this->config['groups'][$name])) {
throw new LogManagerException("Group {$name} not found");
}

$drivers[] = $this->config['groups'][$name];
}

return $this->driver(array_merge(...$drivers));
}

/**
* Detect if default driver has to be used. Additionally convert string to
* array if single driver given.
Expand Down
57 changes: 57 additions & 0 deletions tests/LogManagerTest.php
Expand Up @@ -136,6 +136,63 @@ public function test_Normal(): void
);
}

public function test_No_Group():void
{
$logger = new \OpxCore\Log\LogManager([
'default' => 'testing',
'groups' => [
'local' => ['testing1', 'testing2'],
],
]);

$driver1 = new TestingLogger('testing 1');
$driver2 = new TestingLogger('testing 2');

$logger->bind('testing1', function () use ($driver1) {
return $driver1;
});

$logger->bind('testing2', function () use ($driver2) {
return $driver2;
});

$this->expectException(\OpxCore\Log\Exceptions\LogManagerException::class);

$logger->group('nogroup')->debug('Test');
}

public function test_Group():void
{
$logger = new \OpxCore\Log\LogManager([
'default' => 'testing',
'groups' => [
'local' => ['testing1', 'testing2'],
],
]);

$driver1 = new TestingLogger('testing 1');
$driver2 = new TestingLogger('testing 2');

$logger->bind('testing1', function () use ($driver1) {
return $driver1;
});

$logger->bind('testing2', function () use ($driver2) {
return $driver2;
});

$logger->group('local')->debug('Test');

$this->assertEquals(
[['level' => Psr\Log\LogLevel::DEBUG,'message' => 'Test', 'context' => []]],
$driver1->logs
);
$this->assertEquals(
[['level' => Psr\Log\LogLevel::DEBUG,'message' => 'Test', 'context' => []]],
$driver2->logs
);
}

public function test_Wrong_Logger(): void
{
$logger = new \OpxCore\Log\LogManager([
Expand Down

0 comments on commit 737a466

Please sign in to comment.