|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
6
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
7
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
8
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
9
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
10
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
11
|
|
|
* THE SOFTWARE. |
|
12
|
|
|
* |
|
13
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
14
|
|
|
* and is licensed under the MIT license. |
|
15
|
|
|
* |
|
16
|
|
|
* Copyright (c) 2015-2020 Yuuki Takezawa |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Ytake\LaravelAspect\Interceptor; |
|
21
|
|
|
|
|
22
|
|
|
use Psr\Log\LoggerInterface; |
|
23
|
|
|
use Ray\Aop\MethodInvocation; |
|
24
|
|
|
use Ytake\LaravelAspect\Annotation\LoggableAnnotate; |
|
25
|
|
|
|
|
26
|
|
|
use function sprintf; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class AbstractLogger |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract class AbstractLogger |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
protected $format = "%s:%s.%s"; |
|
35
|
|
|
|
|
36
|
|
|
/** @var LoggerInterface */ |
|
37
|
|
|
protected static $logger; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param LoggableAnnotate $annotation |
|
41
|
|
|
* @param MethodInvocation $invocation |
|
42
|
|
|
* @return array |
|
43
|
|
|
* @throws \ReflectionException |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function logFormatter( |
|
46
|
|
|
LoggableAnnotate $annotation, |
|
47
|
|
|
MethodInvocation $invocation |
|
48
|
|
|
): array { |
|
49
|
|
|
$context = []; |
|
50
|
|
|
$arguments = $invocation->getArguments(); |
|
51
|
|
|
foreach ($invocation->getMethod()->getParameters() as $parameter) { |
|
52
|
|
|
$context['args'][$parameter->name] = isset($arguments[$parameter->getPosition()]) ? |
|
53
|
|
|
$arguments[$parameter->getPosition()] : |
|
54
|
|
|
($parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return [ |
|
58
|
|
|
'level' => $annotation->value, |
|
59
|
|
|
'message' => sprintf( |
|
60
|
|
|
$this->format, |
|
61
|
|
|
$annotation->name, |
|
62
|
|
|
$invocation->getMethod()->class, |
|
63
|
|
|
$invocation->getMethod()->name |
|
64
|
|
|
), |
|
65
|
|
|
'context' => $context, |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param LoggerInterface $logger |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setLogger(LoggerInterface $logger): void |
|
73
|
|
|
{ |
|
74
|
|
|
static::$logger = $logger; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|