Logger::getLogger()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace Nip\Logger;
4
5
use Psr\Log\AbstractLogger;
6
use Psr\Log\LoggerAwareTrait;
7
use Psr\Log\LoggerInterface;
8
9
/**
10
 * Class Logger
11
 * @package Nip\Logger
12
 */
13
class Logger extends AbstractLogger
14
{
15
    use LoggerAwareTrait;
16
17
    protected $formatter;
18
19
    /**
20
     * Create a new log writer instance.
21
     *
22
     * @param \Psr\Log\LoggerInterface $logger
23
     * @return void
24
     */
25 4
    public function __construct(LoggerInterface $logger)
26
    {
27 4
        $this->setLogger($logger);
28 4
        $this->formatter = [$this, 'format'];
29 4
    }
30
31
    /**
32
     * Logs with an arbitrary level.
33
     *
34
     * @param mixed $level
35
     * @param string $message
36
     * @param mixed[] $context
37
     *
38
     * @return void
39
     *
40
     * @throws \Psr\Log\InvalidArgumentException
41
     */
42
    public function log($level, $message, array $context = [])
43
    {
44
        $formatter = $this->formatter;
45
46
        $this->logger->{$level}($formatter($level, $message, $context), $context);
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function getFormatter(): array
53
    {
54
        return $this->formatter;
55
    }
56
57
    /**
58
     * @return LoggerInterface
59
     */
60 4
    public function getLogger(): LoggerInterface
61
    {
62 4
        return $this->logger;
63
    }
64
65
    protected function format(string $level, string $message, array $context): string
66
    {
67
        if (false !== strpos($message, '{')) {
68
            $replacements = [];
69
            foreach ($context as $key => $val) {
70
                if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
71
                    $replacements["{{$key}}"] = $val;
72
                } elseif ($val instanceof \DateTimeInterface) {
73
                    $replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
74
                } elseif (\is_object($val)) {
75
                    $replacements["{{$key}}"] = '[object ' . \get_class($val) . ']';
76
                } else {
77
                    $replacements["{{$key}}"] = '[' . \gettype($val) . ']';
78
                }
79
            }
80
81
            $message = strtr($message, $replacements);
82
        }
83
84
        return sprintf('%s [%s] %s', date(\DateTime::RFC3339), $level, $message) . PHP_EOL;
85
    }
86
87
    /**
88
     * Dynamically proxy method calls to the underlying logger.
89
     *
90
     * @param string $method
91
     * @param array $parameters
92
     * @return mixed
93
     */
94 1
    public function __call($method, $parameters)
95
    {
96 1
        return $this->logger->{$method}(...$parameters);
97
    }
98
}
99