Completed
Pull Request — master (#8)
by Grzegorz
33:07
created

Yii2LogMessage::getException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leinonen\Yii2Monolog;
6
7
use yii\log\Logger;
8
use Psr\Log\LogLevel;
9
use yii\helpers\VarDumper;
10
11
class Yii2LogMessage
12
{
13
    /**
14
     * @var string
15
     */
16
    private $category;
17
18
    /**
19
     * @var array
20
     */
21
    private $trace;
22
23
    /**
24
     * @var int
25
     */
26
    private $memory;
27
28
    /**
29
     * @var string
30
     */
31
    private $message;
32
33
    /**
34
     * @var int
35
     */
36
    private $timestamp;
37
38
    /**
39
     * @var int|mixed
40
     */
41
    private $yiiLogLevel;
42
43
    /**
44
     * @var \Throwable|null
45
     */
46
    private $exception;
47
48
    /**
49
     * Initializes a new Yii2LogMessage.
50
     *
51
     * @param array $message
52
     */
53 13
    public function __construct(array $message)
54
    {
55 13
        $this->setMessage($message[0]);
56
57 13
        $this->yiiLogLevel = $message[1];
58
59 13
        if (isset($message[2])) {
60 13
            $this->category = $message[2];
61
        }
62
63 13
        $this->timestamp = $message[3];
64
65 13
        if (isset($message[4])) {
66 12
            $this->trace = $message[4];
67
        }
68
69 13
        if (isset($message[5])) {
70 13
            $this->memory = $message[5];
71
        }
72 13
    }
73
74
    /**
75
     * Returns the message string.
76
     *
77
     * @return string
78
     */
79 6
    public function getMessage()
80
    {
81 6
        return $this->message;
82
    }
83
84
    /**
85
     * Returns the timestamp for the Yii2LogMessage.
86
     *
87
     * @return int
88
     */
89 1
    public function getTimestamp()
90
    {
91 1
        return $this->timestamp;
92
    }
93
94
    /**
95
     * @return null|\Throwable
96
     */
97 1
    public function getException()
98
    {
99 1
        return $this->exception;
100
    }
101
102
    /**
103
     * Returns the context for the Yii2LogMessage.
104
     *
105
     * @return array
106
     */
107 5
    public function getContext(): array
108
    {
109 5
        $context = [];
110
111 5
        if ($this->category !== null) {
112 5
            $context['category'] = $this->category;
113
        }
114
115 5
        if ($this->trace !== null) {
116 4
            $context['trace'] = $this->trace;
117
        }
118
119 5
        if ($this->memory !== null) {
120 5
            $context['memory'] = $this->memory;
121
        }
122
123 5
        if ($this->exception !== null) {
124 2
            $context['exception'] = $this->exception;
125
        }
126
127 5
        return $context;
128
    }
129
130
    /**
131
     * Returns the PSR-3 compliant log level.
132
     *
133
     * @return string
134
     */
135 10
    public function getPsr3LogLevel(): string
136
    {
137
        $psrLevels = [
138 10
            Logger::LEVEL_ERROR => LogLevel::ERROR,
139
            Logger::LEVEL_WARNING => LogLevel::WARNING,
140
            Logger::LEVEL_INFO => LogLevel::INFO,
141
            Logger::LEVEL_TRACE => LogLevel::DEBUG,
142
            Logger::LEVEL_PROFILE => LogLevel::DEBUG,
143
            Logger::LEVEL_PROFILE_BEGIN => LogLevel::DEBUG,
144
            Logger::LEVEL_PROFILE_END => LogLevel::DEBUG,
145
        ];
146
147 10
        return $psrLevels[$this->yiiLogLevel];
148
    }
149
150
    /**
151
     * @param array|\Throwable|string $message
152
     */
153 13
    private function setMessage($message)
154
    {
155 13
        $this->message = ! \is_string($message)
156 3
            ? $this->convertYiisMessageToString($message)
157 11
            : $message;
158 13
    }
159
160
    /**
161
     * Converts Yii's message to string format.
162
     *
163
     * @param array|\Throwable $message
164
     *
165
     * @return string
166
     */
167 3
    private function convertYiisMessageToString($message): string
168
    {
169 3
        if ($message instanceof \Throwable) {
170 2
            $this->exception = $message;
171
172 2
            return \get_class($message) . ': ' . $message->getMessage();
173
        }
174
175 1
        return VarDumper::export($message);
176
    }
177
}
178