Completed
Push — devel ( d3ae64...2aa639 )
by Alexey
02:44
created

Logger::getTimeRange()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
c 0
b 0
f 0
rs 5.7377
cc 8
eloc 15
nc 8
nop 1
1
<?php namespace Bardex\Elastic\Listener;
2
3
use Bardex\Elastic\IListener;
4
use Psr\Log\LoggerInterface;
5
6
class Logger implements IListener
7
{
8
    /**
9
     * @var LoggerInterface $logger
10
     */
11
    protected $logger;
12
13
    /**
14
     * @var bool логировать все запросы
15
     */
16
    protected $logAllQueries = true;
17
18
    /**
19
     * @var bool логировать запросы с ошибками
20
     */
21
    protected $logErrorQueries = true;
22
23
    /**
24
     * @var bool логировать медленные запросы
25
     */
26
    protected $logSlowQueries = true;
27
28
    /**
29
     * @var int лимит времени выполнения запроса после которого он считается медленным (мс)
30
     */
31
    protected $slowQueryLimitMs = 1000;
32
33
    /**
34
     * Logger constructor.
35
     * @param LoggerInterface $logger
36
     * @param bool $logAllQueries
37
     * @param bool $logErrorQueries
38
     * @param bool $logSlowQueries
39
     * @param int $slowQueryLimitMs
40
     */
41
    public function __construct(
42
        LoggerInterface $logger,
43
        $logAllQueries = true,
44
        $logErrorQueries = true,
45
        $logSlowQueries = true,
46
        $slowQueryLimitMs = 1000
47
    ) {
48
        $this->logger = $logger;
49
        $this->logAllQueries = $logAllQueries;
50
        $this->logErrorQueries = $logErrorQueries;
51
        $this->logSlowQueries = $logSlowQueries;
52
        $this->slowQueryLimitMs = $slowQueryLimitMs;
53
    }
54
55
    /**
56
     * @param LoggerInterface $logger
57
     * @return Logger
58
     */
59
    public function setLogger(LoggerInterface $logger)
60
    {
61
        $this->logger = $logger;
62
        return $this;
63
    }
64
65
    /**
66
     * @param bool $logAllQueries
67
     * @return Logger
68
     */
69
    public function setLogAllQueries($logAllQueries)
70
    {
71
        $this->logAllQueries = $logAllQueries;
72
        return $this;
73
    }
74
75
    /**
76
     * @param bool $logErrorQueries
77
     * @return Logger
78
     */
79
    public function setLogErrorQueries($logErrorQueries)
80
    {
81
        $this->logErrorQueries = $logErrorQueries;
82
        return $this;
83
    }
84
85
    /**
86
     * @param bool $logSlowQueries
87
     * @return Logger
88
     */
89
    public function setLogSlowQueries($logSlowQueries)
90
    {
91
        $this->logSlowQueries = $logSlowQueries;
92
        return $this;
93
    }
94
95
    /**
96
     * @param int $slowQueryLimitMs
97
     * @return Logger
98
     */
99
    public function setSlowQueryLimitMs($slowQueryLimitMs)
100
    {
101
        $this->slowQueryLimitMs = $slowQueryLimitMs;
102
        return $this;
103
    }
104
105
106
    public function onSuccess(array $query, array $response, $time)
107
    {
108
        if ($this->logAllQueries || ($this->logSlowQueries && $time > $this->slowQueryLimitMs)) {
109
            $index = $this->getIndexName($query);
110
            $context = [
111
                'query' => json_encode($query),
112
                'time' => $time,
113
                'time_range' => $this->getTimeRange($time),
114
                'index' => $index,
115
                'found_rows' => $response['hits']['total'],
116
                'fetched_rows' => count($response['hits']['hits'])
117
            ];
118
119
            if ($this->logAllQueries) {
120
                $this->logger->debug("Elastic query (index: $index, time: $time ms)", $context);
121
            }
122
123
            if ($this->logSlowQueries && $time > $this->slowQueryLimitMs) {
124
                $this->logger->warning("Slow elastic query (index: $index, time: $time ms)", $context);
125
            }
126
        }
127
    }
128
129
    public function onError(array $query, \Exception $e)
130
    {
131
        if ($this->logErrorQueries) {
132
            $index = $this->getIndexName($query);
133
            $context = [
134
                'query' => json_encode($query),
135
                'index' => $index,
136
                'error' => $e->getMessage(),
137
            ];
138
            $this->logger->error("Error elastic query (index: $index)", $context);
139
        }
140
    }
141
142
    protected function getIndexName(array $query)
143
    {
144
        $index = isset($query['index']) ? $query['index'] : '(undefined index)';
145
        $type = isset($query['type']) ? $query['type'] : '(undefined type)';
146
        return "$index/$type";
147
    }
148
149
    protected function getTimeRange($time)
150
    {
151
        if ($time <= 10) {
152
            return '0-10 ms';
153
        }
154
        if ($time <= 30) {
155
            return '10-30 ms';
156
        }
157
        if ($time <= 50) {
158
            return '30-50 ms';
159
        }
160
        if ($time <= 100) {
161
            return '50-100 ms';
162
        }
163
        if ($time <= 500) {
164
            return '100-500 ms';
165
        }
166
        if ($time <= 1000) {
167
            return '500-1000 ms';
168
        }
169
        if ($time > 1000) {
170
            return '> 1000 ms';
171
        }
172
    }
173
}
174