1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace San4io\RequestLogger; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
6
|
|
|
use Illuminate\Support\ServiceProvider; |
7
|
|
|
use Monolog\Formatter\LogstashFormatter; |
8
|
|
|
use Monolog\Handler\RotatingFileHandler; |
9
|
|
|
use Monolog\Logger; |
10
|
|
|
use San4io\RequestLogger\Logger\ContextFormatters\RequestHeadersFormatter; |
11
|
|
|
use San4io\RequestLogger\Logger\ContextFormatters\RequestIPFormatter; |
12
|
|
|
use San4io\RequestLogger\Logger\ContextFormatters\RequestMethodFormatter; |
13
|
|
|
use San4io\RequestLogger\Logger\ContextFormatters\RequestParamsFormatter; |
14
|
|
|
use San4io\RequestLogger\Logger\ContextFormatters\RequestUriFormatter; |
15
|
|
|
use San4io\RequestLogger\Logger\ContextFormatters\ResponseBenchmarkFormatter; |
16
|
|
|
use San4io\RequestLogger\Logger\ContextFormatters\ResponseContentFormatter; |
17
|
|
|
use San4io\RequestLogger\Logger\LogContextFormatter; |
18
|
|
|
use San4io\RequestLogger\Logger\RequestLogger; |
19
|
|
|
use San4io\RequestLogger\Services\BenchmarkService; |
20
|
|
|
|
21
|
|
|
class RequestLoggerServiceProvider extends ServiceProvider |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Bootstrap the application services. |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function boot() |
29
|
|
|
{ |
30
|
|
|
$this->publishes([ |
31
|
|
|
__DIR__ . '/../../../resources/config/request-logger.php' => config_path('request-logger.php') |
32
|
|
|
]); |
33
|
|
|
$this->mergeConfigFrom( |
34
|
|
|
__DIR__ . '/Config/request-logger.php', 'request-logger' |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Register the application services. |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function register() |
44
|
|
|
{ |
45
|
|
|
$this->registerLogger(); |
46
|
|
|
$this->registerAppBenchmarkFormatter(); |
47
|
|
|
$this->registerLogContextFormatter(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
*/ |
53
|
|
|
protected function registerLogger() |
54
|
|
|
{ |
55
|
|
|
$this->app->bind('app.request.logger', function () { |
56
|
|
|
$logger = new Logger('request-logger'); |
57
|
|
|
|
58
|
|
|
$handler = new RotatingFileHandler( |
59
|
|
|
storage_path(config('request-logger.storage_path')) |
60
|
|
|
); |
61
|
|
|
$handler->setFormatter(new LogstashFormatter( |
62
|
|
|
env('APP_NAME'), |
|
|
|
|
63
|
|
|
null, |
64
|
|
|
null, |
65
|
|
|
null |
66
|
|
|
)); |
67
|
|
|
|
68
|
|
|
$logger->pushHandler($handler); |
69
|
|
|
|
70
|
|
|
return $logger; |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
$this->app->bind(RequestLogger::class, function (Application $app) { |
74
|
|
|
return new RequestLogger( |
75
|
|
|
$app->make('app.request.logger'), |
76
|
|
|
$app->make(LogContextFormatter::class) |
77
|
|
|
); |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* |
83
|
|
|
*/ |
84
|
|
|
protected function registerAppBenchmarkFormatter() |
85
|
|
|
{ |
86
|
|
|
$this->app->singleton('app.services.benchmark.application', function () { |
87
|
|
|
return new BenchmarkService('application'); |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
|
$this->app->bind(ResponseBenchmarkFormatter::class, function (Application $app) { |
91
|
|
|
return new ResponseBenchmarkFormatter( |
92
|
|
|
$app->make('app.services.benchmark.application') |
93
|
|
|
); |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* |
99
|
|
|
*/ |
100
|
|
|
protected function registerLogContextFormatter() |
101
|
|
|
{ |
102
|
|
|
$this->app->bind(LogContextFormatter::class, function (Application $app) { |
103
|
|
|
$contextFormatter = new LogContextFormatter(); |
104
|
|
|
|
105
|
|
|
$contextFormatter->addContextFormatter($app->make(RequestIPFormatter::class)); |
106
|
|
|
$contextFormatter->addContextFormatter($app->make(RequestMethodFormatter::class)); |
107
|
|
|
$contextFormatter->addContextFormatter($app->make(RequestUriFormatter::class)); |
108
|
|
|
$contextFormatter->addContextFormatter($app->make(RequestParamsFormatter::class)); |
109
|
|
|
$contextFormatter->addContextFormatter($app->make(RequestHeadersFormatter::class)); |
110
|
|
|
$contextFormatter->addContextFormatter($app->make(ResponseContentFormatter::class)); |
111
|
|
|
$contextFormatter->addContextFormatter($app->make(ResponseBenchmarkFormatter::class)); |
112
|
|
|
|
113
|
|
|
return $contextFormatter; |
114
|
|
|
}); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|