|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Valkyrja Framework package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Valkyrja\Log; |
|
15
|
|
|
|
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
use Throwable; |
|
18
|
|
|
use Valkyrja\Log\Contract\Logger as Contract; |
|
19
|
|
|
use Valkyrja\Log\Enum\LogLevel; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class PsrLogger. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Melech Mizrachi |
|
25
|
|
|
*/ |
|
26
|
|
|
class PsrLogger implements Contract |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* PsrAdapter constructor. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct( |
|
32
|
|
|
protected LoggerInterface $logger |
|
33
|
|
|
) { |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritDoc |
|
38
|
|
|
*/ |
|
39
|
|
|
public function debug(string $message, array $context = []): void |
|
40
|
|
|
{ |
|
41
|
|
|
$this->logger->debug($message, $context); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritDoc |
|
46
|
|
|
*/ |
|
47
|
|
|
public function info(string $message, array $context = []): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->logger->info($message, $context); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritDoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function notice(string $message, array $context = []): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->logger->notice($message, $context); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @inheritDoc |
|
62
|
|
|
*/ |
|
63
|
|
|
public function warning(string $message, array $context = []): void |
|
64
|
|
|
{ |
|
65
|
|
|
$this->logger->warning($message, $context); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @inheritDoc |
|
70
|
|
|
*/ |
|
71
|
|
|
public function error(string $message, array $context = []): void |
|
72
|
|
|
{ |
|
73
|
|
|
$this->logger->error($message, $context); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritDoc |
|
78
|
|
|
*/ |
|
79
|
|
|
public function critical(string $message, array $context = []): void |
|
80
|
|
|
{ |
|
81
|
|
|
$this->logger->critical($message, $context); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @inheritDoc |
|
86
|
|
|
*/ |
|
87
|
|
|
public function alert(string $message, array $context = []): void |
|
88
|
|
|
{ |
|
89
|
|
|
$this->logger->alert($message, $context); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @inheritDoc |
|
94
|
|
|
*/ |
|
95
|
|
|
public function emergency(string $message, array $context = []): void |
|
96
|
|
|
{ |
|
97
|
|
|
$this->logger->emergency($message, $context); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @inheritDoc |
|
102
|
|
|
*/ |
|
103
|
|
|
public function log(LogLevel $level, string $message, array $context = []): void |
|
104
|
|
|
{ |
|
105
|
|
|
$this->logger->log($level->value, $message, $context); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritDoc |
|
110
|
|
|
*/ |
|
111
|
|
|
public function exception(Throwable $exception, string $message, array $context = []): void |
|
112
|
|
|
{ |
|
113
|
|
|
$traceCode = $this->getExceptionTraceCode($exception); |
|
114
|
|
|
$logMessage = "\nTrace Code: $traceCode" |
|
115
|
|
|
. "\nException Message: {$exception->getMessage()}" |
|
116
|
|
|
. "\nMessage: $message" |
|
117
|
|
|
. "\nStack Trace:" |
|
118
|
|
|
. "\n==================================" |
|
119
|
|
|
. "\n{$exception->getTraceAsString()}" |
|
120
|
|
|
. "\n==================================\n"; |
|
121
|
|
|
|
|
122
|
|
|
$this->error($logMessage, $context); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get exception trace code. |
|
127
|
|
|
* |
|
128
|
|
|
* @param Throwable $exception The exception |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function getExceptionTraceCode(Throwable $exception): string |
|
133
|
|
|
{ |
|
134
|
|
|
return md5($exception::class . $exception->getTraceAsString()); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|