Completed
Pull Request — master (#10)
by Chad
02:21
created

NewRelicLogger::getExceptionFromContext()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 3
nop 1

1 Method

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