Completed
Pull Request — master (#1427)
by Mikołaj
14:10
created

ElasticaLogger::getQueries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Logger;
13
14
use Psr\Log\AbstractLogger;
15
use Psr\Log\LoggerInterface;
16
17
/**
18
 * Logger for the Elastica.
19
 *
20
 * The {@link logQuery()} method is configured as the logger callable in the
21
 * service container.
22
 *
23
 * @author Gordon Franke <[email protected]>
24
 */
25
class ElasticaLogger extends AbstractLogger
26
{
27
    /**
28
     * @var LoggerInterface
29
     */
30
    protected $logger;
31
32
    /**
33
     * @var array
34
     */
35
    protected $queries = [];
36
37
    /**
38
     * @var bool
39
     */
40
    protected $debug;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param LoggerInterface|null $logger The Symfony logger
46
     * @param bool                 $debug
47
     */
48 31
    public function __construct(LoggerInterface $logger = null, $debug = false)
49
    {
50 31
        $this->logger = $logger;
51 31
        $this->debug = $debug;
52 31
    }
53
54
    /**
55
     * Logs a query.
56
     *
57
     * @param string $path       Path to call
58
     * @param string $method     Rest method to use (GET, POST, DELETE, PUT)
59
     * @param array|string $data Arguments
60
     * @param float  $queryTime  Execution time (in seconds)
61
     * @param array  $connection Host, port, transport, and headers of the query
62
     * @param array  $query      Arguments
63
     * @param int    $engineTime
64
     * @param int    $itemCount
65
     */
66 17
    public function logQuery($path, $method, $data, $queryTime, $connection = [], $query = [], $engineTime = 0, $itemCount = 0)
67
    {
68 17
        $executionMS = $queryTime * 1000;
69
70 17
        if ($this->debug) {
71 15
            $e = new \Exception();
72 15
            if (is_string($data)) {
73 6
                $jsonStrings = explode("\n", $data);
74 6
                $data = [];
75 6
                foreach ($jsonStrings as $json) {
76 6
                    if ($json != '') {
77 6
                        $data[] = json_decode($json, true);
78
                    }
79
                }
80
            } else {
81 11
                $data = [$data];
82
            }
83
84 15
            $this->queries[] = [
85 15
                'path' => $path,
86 15
                'method' => $method,
87 15
                'data' => $data,
88 15
                'executionMS' => $executionMS,
89 15
                'engineMS' => $engineTime,
90 15
                'connection' => $connection,
91 15
                'queryString' => $query,
92 15
                'itemCount' => $itemCount,
93 15
                'backtrace' => $e->getTraceAsString(),
94
            ];
95
        }
96
97 17
        if (null !== $this->logger) {
98 12
            $message = sprintf('%s (%s) %0.2f ms', $path, $method, $executionMS);
99 12
            $this->logger->info($message, (array) $data);
100
        }
101 17
    }
102
103
    /**
104
     * Returns the number of queries that have been logged.
105
     *
106
     * @return int The number of queries logged
107
     */
108 5
    public function getNbQueries()
109
    {
110 5
        return count($this->queries);
111
    }
112
113
    /**
114
     * Returns a human-readable array of queries logged.
115
     *
116
     * @return array An array of queries
117
     */
118 5
    public function getQueries()
119
    {
120 5
        return $this->queries;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 18
    public function log($level, $message, array $context = [])
127
    {
128 18
        return $this->logger->log($level, $message, $context);
129
    }
130
131
    public function reset()
132
    {
133
        $this->queries = [];
134
    }
135
}
136