Completed
Push — master ( d39756...dddd84 )
by Chad
01:13
created

NewRelicLogger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace SubjectivePHP\Psr\Log;
4
5
use Psr\Log\AbstractLogger;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\LogLevel;
8
use SobanVuex\NewRelic\Agent;
9
10
/**
11
 * PSR-3 Implementation using NewRelic.
12
 */
13
final class NewRelicLogger extends AbstractLogger implements LoggerInterface
14
{
15
    use ExceptionExtractorTrait;
16
    use LevelValidatorTrait;
17
    use MessageValidatorTrait;
18
    use MessageInterpolationTrait;
19
20
    /**
21
     * NewRelic Agent implementation.
22
     *
23
     * @var Agent
24
     */
25
    private $newRelicAgent;
26
27
    /**
28
     * Array of log levels which should be reported to new relic.
29
     *
30
     * @var array
31
     */
32
    private $observedLevels = [];
33
34
    /**
35
     * Default log levels to report.
36
     *
37
     * @var array
38
     */
39
    const DEFAULT_OBSERVED_LEVELS = [
40
        LogLevel::EMERGENCY,
41
        LogLevel::CRITICAL,
42
    ];
43
44
    /**
45
     * Construct a new instance of the logger.
46
     *
47
     * @param Agent $newRelicAgent  NewRelic Agent implementation.
48
     * @param array $observedLevels Array of log levels which should be reported to new relic.
49
     */
50
    public function __construct(Agent $newRelicAgent, array $observedLevels = self::DEFAULT_OBSERVED_LEVELS)
51
    {
52
        $this->newRelicAgent = $newRelicAgent;
53
        $this->observedLevels = $observedLevels;
54
    }
55
56
    /**
57
     * Logs with an arbitrary level.
58
     *
59
     * @param string $level   A valid RFC 5424 log level.
60
     * @param string $message The base log message.
61
     * @param array  $context Any extraneous information that does not fit well in a string.
62
     *
63
     * @return void
64
     */
65
    public function log($level, $message, array $context = [])//@codingStandardsIgnoreLine Interface does not define type-hints or return
66
    {
67
        if (!in_array($level, $this->observedLevels)) {
68
            return;
69
        }
70
71
        $this->validateLevel($level);
72
        $this->validateMessage($message);
73
74
        $exception = $this->getExceptionFromContext($context);
75
        unset($context['exception']);
76
77
        $this->addCustomNewRelicParameters(['level' => $level] + $context);
78
        $this->newRelicAgent->noticeError((string)$this->interpolateMessage($message, $context), $exception);
79
    }
80
81
    private function addCustomNewRelicParameters(array $context)
82
    {
83
        foreach ($context as $key => $value) {
84
            if (!is_scalar($value)) {
85
                $value = var_export($value, true);
86
            }
87
88
            $this->newRelicAgent->addCustomParameter($key, $value);
89
        }
90
    }
91
}
92