Passed
Push — master ( b6f271...9ac357 )
by Melech
01:28
created

PsrLogger::exception()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 13
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\Logger;
15
16
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Psr\Log\LoggerInterface;
18
use Throwable;
19
use Valkyrja\Log\Enum\LogLevel;
20
use Valkyrja\Log\Logger\Contract\LoggerContract as Contract;
21
22
class PsrLogger implements Contract
23
{
24
    public function __construct(
25
        protected LoggerInterface $logger
26
    ) {
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    #[Override]
33
    public function debug(string $message, array $context = []): void
34
    {
35
        $this->logger->debug($message, $context);
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    #[Override]
42
    public function info(string $message, array $context = []): void
43
    {
44
        $this->logger->info($message, $context);
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    #[Override]
51
    public function notice(string $message, array $context = []): void
52
    {
53
        $this->logger->notice($message, $context);
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    #[Override]
60
    public function warning(string $message, array $context = []): void
61
    {
62
        $this->logger->warning($message, $context);
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    #[Override]
69
    public function error(string $message, array $context = []): void
70
    {
71
        $this->logger->error($message, $context);
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    #[Override]
78
    public function critical(string $message, array $context = []): void
79
    {
80
        $this->logger->critical($message, $context);
81
    }
82
83
    /**
84
     * @inheritDoc
85
     */
86
    #[Override]
87
    public function alert(string $message, array $context = []): void
88
    {
89
        $this->logger->alert($message, $context);
90
    }
91
92
    /**
93
     * @inheritDoc
94
     */
95
    #[Override]
96
    public function emergency(string $message, array $context = []): void
97
    {
98
        $this->logger->emergency($message, $context);
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    #[Override]
105
    public function log(LogLevel $level, string $message, array $context = []): void
106
    {
107
        $this->logger->log($level->value, $message, $context);
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    #[Override]
114
    public function throwable(Throwable $throwable, string $message, array $context = []): void
115
    {
116
        $traceCode  = $this->getExceptionTraceCode($throwable);
117
        $logMessage = "\nTrace Code: $traceCode"
118
            . "\nException Message: {$throwable->getMessage()}"
119
            . "\nMessage: $message"
120
            . "\nStack Trace:"
121
            . "\n=================================="
122
            . "\n{$throwable->getTraceAsString()}"
123
            . "\n==================================\n";
124
125
        $this->error($logMessage, $context);
126
    }
127
128
    /**
129
     * Get exception trace code.
130
     *
131
     * @param Throwable $exception The exception
132
     *
133
     * @return string
134
     */
135
    protected function getExceptionTraceCode(Throwable $exception): string
136
    {
137
        return md5($exception::class . $exception->getTraceAsString());
138
    }
139
}
140