Completed
Pull Request — master (#992)
by David
05:34
created

ElasticaLogger   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 58.97%
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 150
ccs 23
cts 39
cp 0.5897
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getNbQueries() 0 4 1
A getQueries() 0 4 1
A debug() 0 4 1
A logQuery() 0 18 3
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 log() 0 4 1
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 11
    public function __construct(LoggerInterface $logger = null, $debug = false)
39
    {
40 11
        $this->logger = $logger;
41 11
        $this->debug = $debug;
42 11
    }
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 7
    public function logQuery($path, $method, $data, $time, $connection = array(), $query = array())
55
    {
56 7
        if ($this->debug) {
57 6
            $this->queries[] = array(
58 6
                'path' => $path,
59 6
                'method' => $method,
60 6
                'data' => $data,
61 6
                'executionMS' => $time,
62 6
                'connection' => $connection,
63 6
                'queryString' => $query,
64
            );
65
        }
66
67 7
        if (null !== $this->logger) {
68 4
            $message = sprintf("%s (%s) %0.2f ms", $path, $method, $time * 1000);
69 4
            $this->logger->info($message, (array) $data);
70
        }
71 7
    }
72
73
    /**
74
     * Returns the number of queries that have been logged.
75
     *
76
     * @return integer The number of queries logged
77
     */
78 3
    public function getNbQueries()
79
    {
80 3
        return count($this->queries);
81
    }
82
83
    /**
84
     * Returns a human-readable array of queries logged.
85
     *
86
     * @return array An array of queries
87
     */
88 1
    public function getQueries()
89
    {
90 1
        return $this->queries;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function emergency($message, array $context = array())
97
    {
98
        return $this->logger->emergency($message, $context);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function alert($message, array $context = array())
105
    {
106
        return $this->logger->alert($message, $context);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function critical($message, array $context = array())
113
    {
114
        return $this->logger->critical($message, $context);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function error($message, array $context = array())
121
    {
122
        return $this->logger->error($message, $context);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function warning($message, array $context = array())
129
    {
130
        return $this->logger->warning($message, $context);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function notice($message, array $context = array())
137
    {
138
        return $this->logger->notice($message, $context);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function info($message, array $context = array())
145
    {
146
        return $this->logger->info($message, $context);
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 4
    public function debug($message, array $context = array())
153
    {
154 4
        return $this->logger->debug($message, $context);
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function log($level, $message, array $context = array())
161
    {
162
        return $this->logger->log($level, $message, $context);
163
    }
164
}
165