1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Db\Events; |
13
|
|
|
|
14
|
|
|
use Phalcon\Db\Adapter\AbstractAdapter; |
15
|
|
|
use Phalcon\Events\EventInterface; |
16
|
|
|
use Phalcon\Mvc\ModelInterface; |
17
|
|
|
use Zemit\Di\Injectable; |
18
|
|
|
use Zemit\Models\Interfaces\SessionInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @todo review |
22
|
|
|
*/ |
23
|
|
|
class Logger extends Injectable |
24
|
|
|
{ |
25
|
|
|
public bool $inProgress = false; |
26
|
|
|
|
27
|
4 |
|
public function beforeQuery(EventInterface $event, AbstractAdapter $connection): void |
28
|
|
|
{ |
29
|
4 |
|
if ($this->config->path('logger.enable') || $this->config->path('app.logger')) { |
30
|
|
|
if ($this->config->path('logger.logDatabaseQuery')) { |
31
|
|
|
if (!$this->inProgress) { |
32
|
|
|
|
33
|
|
|
// deactivate logger |
34
|
|
|
$this->inProgress = true; |
35
|
|
|
$sessionId = $this->identity->getSession()?->getId(); |
36
|
|
|
$userId = $this->identity->getUserId() ?: null; |
37
|
|
|
$userAsId = $this->identity->getUserAsId() ?: null; |
38
|
|
|
|
39
|
|
|
$log = json_encode([ |
40
|
|
|
'type' => 'query', |
41
|
|
|
'sessionId' => $sessionId, |
42
|
|
|
'userId' => $userId, |
43
|
|
|
'userAsId' => $userAsId, |
44
|
|
|
'event' => [ |
45
|
|
|
'type' => $event->getType(), |
46
|
|
|
'data' => $event->getData(), |
47
|
|
|
], |
48
|
|
|
'meta' => [ |
49
|
|
|
// 'identity' => $this->identity->getIdentity(), |
50
|
|
|
'sqlStatement' => $connection->getSQLStatement(), |
51
|
|
|
'sqlVariables' => $connection->getSQLVariables(), |
52
|
|
|
], |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
$this->logger->info($log); |
56
|
|
|
|
57
|
|
|
// reactivate logger |
58
|
|
|
$this->inProgress = false; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|