1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: christopherfuchs |
6
|
|
|
* Date: 14.02.18 |
7
|
|
|
* Time: 15:45 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Ytake\LaravelFluent; |
11
|
|
|
|
12
|
|
|
use Fluent\Logger\FluentLogger; |
13
|
|
|
use Illuminate\Foundation\Application; |
14
|
|
|
use Monolog\Formatter\FormatterInterface; |
15
|
|
|
use Monolog\Formatter\LineFormatter; |
16
|
|
|
use Monolog\Handler\HandlerInterface; |
17
|
|
|
use Monolog\Logger; |
18
|
|
|
|
19
|
|
|
class CreateFluentLogger |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Application |
23
|
|
|
*/ |
24
|
|
|
private $app; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The Log levels. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $levels = [ |
32
|
|
|
'debug' => Logger::DEBUG, |
33
|
|
|
'info' => Logger::INFO, |
34
|
|
|
'notice' => Logger::NOTICE, |
35
|
|
|
'warning' => Logger::WARNING, |
36
|
|
|
'error' => Logger::ERROR, |
37
|
|
|
'critical' => Logger::CRITICAL, |
38
|
|
|
'alert' => Logger::ALERT, |
39
|
|
|
'emergency' => Logger::EMERGENCY, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* CreateFluentLogger constructor. |
44
|
|
|
* |
45
|
|
|
* @param Application $app |
46
|
|
|
*/ |
47
|
|
|
public function __construct(Application $app) |
48
|
|
|
{ |
49
|
|
|
$this->app = $app; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create a custom Monolog instance. |
54
|
|
|
* |
55
|
|
|
* @param array $config |
56
|
|
|
* |
57
|
|
|
* @return \Monolog\Logger |
58
|
|
|
*/ |
59
|
|
|
public function __invoke(array $config) : Logger |
60
|
|
|
{ |
61
|
|
|
$configure = $this->app['config']->get('fluent'); |
62
|
|
|
|
63
|
|
|
if ($configure['packer'] !== null) { |
64
|
|
|
if (class_exists($configure['packer'])) { |
65
|
|
|
$packer = $this->app->make($configure['packer']); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$fluentLogger = new FluentLogger( |
70
|
|
|
$configure['host'] ?? FluentLogger::DEFAULT_ADDRESS, |
71
|
|
|
(int) $configure['port'] ?? FluentLogger::DEFAULT_LISTEN_PORT, |
72
|
|
|
$configure['options'] ?? [], |
73
|
|
|
$packer ?? null |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$logger = new Logger($config["channel"], [ |
77
|
|
|
$this->prepareHandler(new FluentHandler( |
78
|
|
|
$fluentLogger, |
79
|
|
|
$configure['tagFormat'] ?? null, |
80
|
|
|
$this->level($this->app['config']['app.log_level'] |
81
|
|
|
))), |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
return $logger; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Prepare the handler for usage by Monolog. |
89
|
|
|
* |
90
|
|
|
* @param \Monolog\Handler\HandlerInterface $handler |
91
|
|
|
* |
92
|
|
|
* @return \Monolog\Handler\HandlerInterface |
93
|
|
|
*/ |
94
|
|
|
protected function prepareHandler(HandlerInterface $handler): HandlerInterface |
95
|
|
|
{ |
96
|
|
|
return $handler->setFormatter($this->formatter()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get a Monolog formatter instance. |
101
|
|
|
* |
102
|
|
|
* @return \Monolog\Formatter\FormatterInterface |
103
|
|
|
*/ |
104
|
|
|
protected function formatter() : FormatterInterface |
105
|
|
|
{ |
106
|
|
|
return tap(new LineFormatter(null, null, true, true), function ($formatter) { |
107
|
|
|
$formatter->includeStacktraces(); |
108
|
|
|
}); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Parse the string level into a Monolog constant. |
113
|
|
|
* |
114
|
|
|
* @param array $config |
|
|
|
|
115
|
|
|
* |
116
|
|
|
* @throws \InvalidArgumentException |
117
|
|
|
* |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
|
|
protected function level(string $level) : int |
121
|
|
|
{ |
122
|
|
|
$level = $level ?? 'debug'; |
123
|
|
|
|
124
|
|
|
if (isset($this->levels[$level])) { |
125
|
|
|
return $this->levels[$level]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
throw new \InvalidArgumentException('Invalid log level.'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.