LogAdapter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 154
rs 10
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 3 1
A setContext() 0 4 1
A setTraceId() 0 3 1
A alert() 0 3 1
A notice() 0 3 1
A warning() 0 3 1
A critical() 0 3 1
A error() 0 3 1
A info() 0 3 1
A debug() 0 3 1
A emergency() 0 3 1
A __construct() 0 19 1
1
<?php
2
3
namespace Arquivei\LogAdapter;
4
5
use Arquivei\LogAdapter\Processors\DateTimeProcessor;
6
use Arquivei\LogAdapter\Processors\DynamicContextProcessor;
7
use Arquivei\LogAdapter\Processors\TraceIdProcessor;
8
use Monolog\Logger;
9
use Monolog\Handler\StreamHandler;
10
use Monolog\Processor\MemoryPeakUsageProcessor;
11
use Monolog\Processor\MemoryUsageProcessor;
12
use Monolog\Formatter\JsonFormatter;
13
use Monolog\Processor\ProcessorInterface;
14
use stdClass;
15
16
class LogAdapter implements Log
17
{
18
    private Logger $logger;
19
    private TraceIdProcessor $traceIdProcessor;
20
    private DynamicContextProcessor $dynamicContextProcessor;
21
22
    public function __construct()
23
    {
24
        $stream = env('LOGGER_STREAM', 'php://stdout');
25
        $loggerLevel = env('LOGGER_LEVEL', 200);
26
        $loggerName = env('LOGGER_NAME', 'arquivei_log_adapter');
27
28
        $this->traceIdProcessor = new TraceIdProcessor();
29
        $this->dynamicContextProcessor = new DynamicContextProcessor();
30
31
        $handler = new StreamHandler($stream, $loggerLevel);
32
        $handler->setFormatter(new JsonFormatter());
33
        $this->logger = new Logger($loggerName);
34
        $this->logger->pushHandler($handler);
35
36
        $this->logger->pushProcessor(new MemoryUsageProcessor())
37
            ->pushProcessor(new MemoryPeakUsageProcessor())
38
            ->pushProcessor(new DateTimeProcessor())
39
            ->pushProcessor($this->traceIdProcessor)
40
            ->pushProcessor($this->dynamicContextProcessor);
41
    }
42
43
    /**
44
     * @param stdClass|null $stdClass
45
     */
46
    public function setContext(?stdClass $stdClass): void
47
    {
48
        $this->dynamicContextProcessor->setStdClass($stdClass);
49
        $this->logger->reset();
50
    }
51
52
    /**
53
     * @param string|null $traceId
54
     */
55
    public function setTraceId(?string $traceId = null): void
56
    {
57
        $this->traceIdProcessor->setTraceId($traceId);
58
    }
59
60
    /**
61
     * System is unusable.
62
     *
63
     * @param string $message
64
     * @param array $context
65
     * @return void
66
     */
67
    public function emergency($message, array $context = []): void
68
    {
69
        $this->logger->emergency($message, $context);
70
    }
71
72
    /**
73
     * Action must be taken immediately.
74
     *
75
     * Example: Entire website down, database unavailable, etc. This should
76
     * trigger the SMS alerts and wake you up.
77
     *
78
     * @param string $message
79
     * @param array $context
80
     * @return void
81
     */
82
    public function alert($message, array $context = []): void
83
    {
84
        $this->logger->alert($message, $context);
85
    }
86
87
    /**
88
     * Critical conditions.
89
     *
90
     * Example: Application component unavailable, unexpected exception.
91
     *
92
     * @param string $message
93
     * @param array $context
94
     * @return void
95
     */
96
    public function critical($message, array $context = []): void
97
    {
98
        $this->logger->critical($message, $context);
99
    }
100
101
    /**
102
     * Runtime errors that do not require immediate action but should typically
103
     * be logged and monitored.
104
     *
105
     * @param string $message
106
     * @param array $context
107
     * @return void
108
     */
109
    public function error($message, array $context = []): void
110
    {
111
        $this->logger->error($message, $context);
112
    }
113
114
    /**
115
     * Exceptional occurrences that are not errors.
116
     *
117
     * Example: Use of deprecated APIs, poor use of an API, undesirable things
118
     * that are not necessarily wrong.
119
     *
120
     * @param string $message
121
     * @param array $context
122
     * @return void
123
     */
124
    public function warning($message, array $context = []): void
125
    {
126
        $this->logger->warning($message, $context);
127
    }
128
129
    /**
130
     * Normal but significant events.
131
     *
132
     * @param string $message
133
     * @param array $context
134
     * @return void
135
     */
136
    public function notice($message, array $context = []): void
137
    {
138
        $this->logger->notice($message, $context);
139
    }
140
141
    /**
142
     * Interesting events.
143
     *
144
     * Example: User logs in, SQL logs.
145
     *
146
     * @param string $message
147
     * @param array $context
148
     * @return void
149
     */
150
    public function info($message, array $context = []): void
151
    {
152
        $this->logger->info($message, $context);
153
    }
154
155
    /**
156
     * Detailed debug information.
157
     *
158
     * @param string $message
159
     * @param array $context
160
     * @return void
161
     */
162
    public function debug($message, array $context = []): void
163
    {
164
        $this->logger->debug($message, $context);
165
    }
166
167
    public function log($level, $message, array $context = array()): void
168
    {
169
        $this->logger->log($level, $message, $context);
170
    }
171
}
172