1 | <?php |
||
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) |
|
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() |
|
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() |
|
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()) |
|
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 |