Completed
Pull Request — master (#10)
by Chad
03:18 queued 56s
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::ALERT,
42
        LogLevel::CRITICAL,
43
        LogLevel::ERROR,
44
    ];
45
46
    /**
47
     * Construct a new instance of the logger.
48
     *
49
     * @param Agent $newRelicAgent  NewRelic Agent implementation.
50
     * @param array $observedLevels Array of log levels which should be reported to new relic.
51
     */
52
    public function __construct(Agent $newRelicAgent, array $observedLevels = self::DEFAULT_OBSERVED_LEVELS)
53
    {
54
        $this->newRelicAgent = $newRelicAgent;
55
        $this->observedLevels = $observedLevels;
56
    }
57
58
    /**
59
     * Logs with an arbitrary level.
60
     *
61
     * @param string $level   A valid RFC 5424 log level.
62
     * @param string $message The base log message.
63
     * @param array  $context Any extraneous information that does not fit well in a string.
64
     *
65
     * @return void
66
     */
67
    public function log($level, $message, array $context = [])//@codingStandardsIgnoreLine Interface does not define type-hints or return
68
    {
69
        if (!in_array($level, $this->observedLevels)) {
70
            return;
71
        }
72
73
        $this->validateLevel($level);
74
        $this->validateMessage($message);
75
76
        $exception = $this->getExceptionFromContext($context);
77
        unset($context['exception']);
78
79
        $this->addCustomNewRelicParameters(['level' => $level] + $context);
80
        $this->newRelicAgent->noticeError((string)$this->interpolateMessage($message, $context), $exception);
81
    }
82
83
    private function addCustomNewRelicParameters(array $context)
84
    {
85
        foreach ($context as $key => $value) {
86
            if (!is_scalar($value)) {
87
                $value = var_export($value, true);
88
            }
89
90
            $this->newRelicAgent->addCustomParameter($key, $value);
91
        }
92
    }
93
}
94