Passed
Push — master ( d47bdf...6296de )
by Kirill
03:33
created

LogFactory::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 4
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Logger;
13
14
use Psr\Log\LoggerInterface;
15
use Spiral\Logger\Event\LogEvent;
16
17
/**
18
 * Routes log information to various listeners.
19
 */
20
final class LogFactory implements LogsInterface
21
{
22
    /** @var ListenerRegistryInterface */
23
    private $listenedRegistry;
24
25
    /**
26
     * @param ListenerRegistryInterface $listenedRegistry
27
     */
28
    public function __construct(ListenerRegistryInterface $listenedRegistry)
29
    {
30
        $this->listenedRegistry = $listenedRegistry;
31
    }
32
33
    /**
34
     * @param string $channel
35
     * @return LoggerInterface
36
     */
37
    public function getLogger(string $channel): LoggerInterface
38
    {
39
        return new NullLogger([$this, 'log'], $channel);
40
    }
41
42
    /**
43
     * @param string $channel
44
     * @param mixed  $level
45
     * @param string $message
46
     * @param array  $context
47
     */
48
    public function log($channel, $level, $message, array $context = []): void
49
    {
50
        $e = new LogEvent(
51
            new \DateTime(),
52
            $channel,
53
            $level,
54
            $message,
55
            $context
56
        );
57
58
        foreach ($this->listenedRegistry->getListeners() as $listener) {
59
            call_user_func($listener, $e);
60
        }
61
    }
62
}
63