Completed
Pull Request — master (#1015)
by Kévin
09:16 queued 06:42
created

ElasticaLogger   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 152
ccs 43
cts 43
cp 1
rs 10
c 1
b 0
f 1

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getNbQueries() 0 4 1
A getQueries() 0 4 1
A emergency() 0 4 1
A alert() 0 4 1
A critical() 0 4 1
A error() 0 4 1
A warning() 0 4 1
A notice() 0 4 1
A info() 0 4 1
A debug() 0 4 1
A log() 0 4 1
A __construct() 0 5 1
A logQuery() 0 20 3
1
<?php
2
3
namespace FOS\ElasticaBundle\Logger;
4
5
use Psr\Log\LoggerInterface;
6
7
/**
8
 * Logger for the Elastica.
9
 *
10
 * The {@link logQuery()} method is configured as the logger callable in the
11
 * service container.
12
 *
13
 * @author Gordon Franke <[email protected]>
14
 */
15
class ElasticaLogger implements LoggerInterface
16
{
17
    /**
18
     * @var LoggerInterface
19
     */
20
    protected $logger;
21
22
    /**
23
     * @var array
24
     */
25
    protected $queries = array();
26
27
    /**
28
     * @var boolean
29
     */
30
    protected $debug;
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param LoggerInterface|null $logger The Symfony logger
36
     * @param boolean              $debug
37
     */
38 26
    public function __construct(LoggerInterface $logger = null, $debug = false)
39
    {
40 26
        $this->logger = $logger;
41 26
        $this->debug = $debug;
42 26
    }
43
44
    /**
45
     * Logs a query.
46
     *
47
     * @param string $path       Path to call
48
     * @param string $method     Rest method to use (GET, POST, DELETE, PUT)
49
     * @param array  $data       Arguments
50
     * @param float  $time       Execution time
51
     * @param array  $connection Host, port, transport, and headers of the query
52
     * @param array  $query      Arguments
53
     */
54 12
    public function logQuery($path, $method, $data, $time, $connection = array(), $query = array(), $engineTime = 0, $itemCount = 0)
55
    {
56 12
        if ($this->debug) {
57 10
            $this->queries[] = array(
58 10
                'path' => $path,
59 10
                'method' => $method,
60 10
                'data' => $data,
61 10
                'executionMS' => $time,
62 10
                'engineMS' => $engineTime,
63 10
                'connection' => $connection,
64 10
                'queryString' => $query,
65 10
                'itemCount' => $itemCount,
66
            );
67 10
        }
68
69 12
        if (null !== $this->logger) {
70 9
            $message = sprintf("%s (%s) %0.2f ms", $path, $method, $time * 1000);
71 9
            $this->logger->info($message, (array) $data);
72 9
        }
73 12
    }
74
75
    /**
76
     * Returns the number of queries that have been logged.
77
     *
78
     * @return integer The number of queries logged
79
     */
80 3
    public function getNbQueries()
81
    {
82 3
        return count($this->queries);
83
    }
84
85
    /**
86
     * Returns a human-readable array of queries logged.
87
     *
88
     * @return array An array of queries
89
     */
90 1
    public function getQueries()
91
    {
92 1
        return $this->queries;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 1
    public function emergency($message, array $context = array())
99
    {
100 1
        return $this->logger->emergency($message, $context);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1
    public function alert($message, array $context = array())
107
    {
108 1
        return $this->logger->alert($message, $context);
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 1
    public function critical($message, array $context = array())
115
    {
116 1
        return $this->logger->critical($message, $context);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 1
    public function error($message, array $context = array())
123
    {
124 1
        return $this->logger->error($message, $context);
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 1
    public function warning($message, array $context = array())
131
    {
132 1
        return $this->logger->warning($message, $context);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 1
    public function notice($message, array $context = array())
139
    {
140 1
        return $this->logger->notice($message, $context);
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 1
    public function info($message, array $context = array())
147
    {
148 1
        return $this->logger->info($message, $context);
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 9
    public function debug($message, array $context = array())
155
    {
156 9
        return $this->logger->debug($message, $context);
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 1
    public function log($level, $message, array $context = array())
163
    {
164 1
        return $this->logger->log($level, $message, $context);
165
    }
166
}
167