Passed
Push — master ( 976aad...1511ac )
by Aleksandr
01:47
created

RequestLoggerServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
B registerLogger() 0 24 1
A register() 0 5 1
A boot() 0 7 1
A registerAppBenchmarkFormatter() 0 9 1
A registerLogContextFormatter() 0 13 1
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')
0 ignored issues
show
Bug introduced by
The function config_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
            __DIR__ . '/../../../resources/config/request-logger.php' => /** @scrutinizer ignore-call */ config_path('request-logger.php')
Loading history...
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'))
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
                storage_path(/** @scrutinizer ignore-call */ config('request-logger.storage_path'))
Loading history...
Bug introduced by
The function storage_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
                /** @scrutinizer ignore-call */ 
59
                storage_path(config('request-logger.storage_path'))
Loading history...
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