|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* micrometa |
|
5
|
|
|
* |
|
6
|
|
|
* @category Jkphl |
|
7
|
|
|
* @package Jkphl\Micrometa |
|
8
|
|
|
* @subpackage Jkphl\Micrometa\Infrastructure |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/*********************************************************************************** |
|
15
|
|
|
* The MIT License (MIT) |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
|
18
|
|
|
* |
|
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
21
|
|
|
* the Software without restriction, including without limitation the rights to |
|
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
24
|
|
|
* subject to the following conditions: |
|
25
|
|
|
* |
|
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
27
|
|
|
* copies or substantial portions of the Software. |
|
28
|
|
|
* |
|
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
35
|
|
|
***********************************************************************************/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Jkphl\Micrometa\Infrastructure\Logger; |
|
38
|
|
|
|
|
39
|
|
|
use Jkphl\Micrometa\Ports\Exceptions\RuntimeException; |
|
40
|
|
|
use Monolog\Handler\NullHandler; |
|
41
|
|
|
use Monolog\Logger; |
|
42
|
|
|
use Monolog\ResettableInterface; |
|
43
|
|
|
use Psr\Log\LoggerInterface; |
|
44
|
|
|
use Psr\Log\LoggerTrait; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Exception logger |
|
48
|
|
|
* |
|
49
|
|
|
* @package Jkphl\Micrometa |
|
50
|
|
|
* @subpackage Jkphl\Micrometa\Infrastructure |
|
51
|
|
|
*/ |
|
52
|
|
|
final class ExceptionLogger implements LoggerInterface, ResettableInterface |
|
53
|
|
|
{ |
|
54
|
|
|
use LoggerTrait; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Exception threshold |
|
58
|
|
|
* |
|
59
|
|
|
* @var int |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $threshold; |
|
62
|
|
|
|
|
63
|
|
|
private $decoratedLogger; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Constructor |
|
67
|
|
|
*/ |
|
68
|
9 |
|
public function __construct($threshold = Logger::ERROR) |
|
69
|
|
|
{ |
|
70
|
9 |
|
$this->threshold = $threshold; |
|
71
|
9 |
|
$this->decoratedLogger = new Logger('exception', [new NullHandler()]); |
|
72
|
9 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Throws an exception for all messages with error level or higher |
|
76
|
|
|
* |
|
77
|
|
|
* @param mixed $level The log level |
|
78
|
|
|
* @param string $message The log message |
|
79
|
|
|
* @param array $context The log context |
|
80
|
|
|
* |
|
81
|
|
|
* @throws \Exception Exception that occured |
|
82
|
|
|
* @throws \RuntimeException Log message as exception |
|
83
|
|
|
*/ |
|
84
|
18 |
|
public function log($level, $message, array $context = []) |
|
85
|
|
|
{ |
|
86
|
18 |
|
$level = Logger::toMonologLevel($level); |
|
87
|
|
|
|
|
88
|
18 |
|
if ($this->isTriggered($level)) { |
|
89
|
3 |
|
throw $this->getContextException($context) ?: new RuntimeException($message, $level); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
16 |
|
$this->decoratedLogger->addRecord($level, $message, $context); |
|
93
|
16 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Return whether an exception should be triggered |
|
97
|
|
|
* |
|
98
|
|
|
* @param int $level Log level |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool Exception should be triggered |
|
101
|
|
|
*/ |
|
102
|
18 |
|
protected function isTriggered($level) |
|
103
|
|
|
{ |
|
104
|
18 |
|
return $this->threshold && ($level >= $this->threshold); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Return the context exception (if any) |
|
109
|
|
|
* |
|
110
|
|
|
* @param array $context Context |
|
111
|
|
|
* |
|
112
|
|
|
* @return \Exception|null Context exception |
|
113
|
|
|
*/ |
|
114
|
3 |
|
protected function getContextException(array $context) |
|
115
|
|
|
{ |
|
116
|
3 |
|
return (isset($context['exception']) && ($context['exception'] instanceof \Exception)) ? |
|
117
|
3 |
|
$context['exception'] : null; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function reset() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->decoratedLogger->reset(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|