Completed
Push — master ( 6a369a...985e7a )
by Karel
25s queued 10s
created

ElasticaLogger::getQueries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
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 33
    public function __construct(?LoggerInterface $logger = null, bool $debug = false)
43
    {
44 33
        $this->logger = $logger;
45 33
        $this->debug = $debug;
46 33
    }
47
48
    /**
49
     * Logs a query.
50
     *
51
     * @param string       $path       Path to call
52
     * @param string       $method     Rest method to use (GET, POST, DELETE, PUT)
53
     * @param array|string $data       Arguments
54
     * @param float        $queryTime  Execution time (in seconds)
55
     * @param array        $connection Host, port, transport, and headers of the query
56
     * @param array        $query      Arguments
57
     * @param int          $engineTime
58
     */
59 19
    public function logQuery(string $path, string $method, $data, $queryTime, $connection = [], $query = [], $engineTime = 0, int $itemCount = 0)
60
    {
61 19
        $executionMS = $queryTime * 1000;
62
63 19
        if ($this->debug) {
64 17
            $e = new \Exception();
65 17
            if (is_string($data)) {
66 6
                $jsonStrings = explode("\n", $data);
67 6
                $data = [];
68 6
                foreach ($jsonStrings as $json) {
69 6
                    if ('' != $json) {
70 6
                        $data[] = json_decode($json, true);
71
                    }
72
                }
73
            } else {
74 13
                $data = [$data];
75
            }
76
77 17
            $this->queries[] = [
78 17
                'path' => $path,
79 17
                'method' => $method,
80 17
                'data' => $data,
81 17
                'executionMS' => $executionMS,
82 17
                'engineMS' => $engineTime,
83 17
                'connection' => $connection,
84 17
                'queryString' => $query,
85 17
                'itemCount' => $itemCount,
86 17
                'backtrace' => $e->getTraceAsString(),
87
            ];
88
        }
89
90 19
        if (null !== $this->logger) {
91 14
            $message = sprintf('%s (%s) %0.2f ms', $path, $method, $executionMS);
92 14
            $this->logger->info($message, (array) $data);
93
        }
94 19
    }
95
96
    /**
97
     * Returns the number of queries that have been logged.
98
     */
99 5
    public function getNbQueries(): int
100
    {
101 5
        return count($this->queries);
102
    }
103
104
    /**
105
     * Returns a human-readable array of queries logged.
106
     */
107 5
    public function getQueries(): array
108
    {
109 5
        return $this->queries;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 20
    public function log($level, $message, array $context = [])
116
    {
117 20
        $this->logger->log($level, $message, $context);
118 20
    }
119
120
    public function reset()
121
    {
122
        $this->queries = [];
123
    }
124
}
125