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