1
0
mirror of https://github.com/dg/dibi.git synced 2025-07-31 19:30:30 +02:00

FileLogger: Add option to log errors only (#351)

This commit is contained in:
Enrico Dias
2020-02-09 13:12:55 -03:00
committed by David Grudl
parent 2f857c28d6
commit 8c761eac5c
3 changed files with 13 additions and 3 deletions

View File

@@ -20,6 +20,7 @@ $dibi = new Dibi\Connection([
// enable query logging to this file
'profiler' => [
'file' => 'log/log.sql',
'errorsOnly' => false,
],
]);

View File

@@ -51,6 +51,7 @@ class Connection implements IConnection
* - profiler (array)
* - run (bool) => enable profiler?
* - file => file to log
* - errorsOnly (bool) => log only errors
* - substitutes (array) => map of driver specific substitutes (under development)
* - onConnect (array) => list of SQL queries to execute (by Connection::query()) after connection is established
* @throws Exception
@@ -70,7 +71,8 @@ class Connection implements IConnection
// profiler
if (isset($config['profiler']['file']) && (!isset($config['profiler']['run']) || $config['profiler']['run'])) {
$filter = $config['profiler']['filter'] ?? Event::QUERY;
$this->onEvent[] = [new Loggers\FileLogger($config['profiler']['file'], $filter), 'logEvent'];
$errorsOnly = $config['profiler']['errorsOnly'] ?? false;
$this->onEvent[] = [new Loggers\FileLogger($config['profiler']['file'], $filter, $errorsOnly), 'logEvent'];
}
$this->substitutes = new HashMap(function (string $expr) { return ":$expr:"; });

View File

@@ -25,11 +25,15 @@ class FileLogger
/** @var int */
public $filter;
/** @var bool */
private $errorsOnly;
public function __construct(string $file, int $filter = null)
public function __construct(string $file, int $filter = null, bool $errorsOnly = false)
{
$this->file = $file;
$this->filter = $filter ?: Dibi\Event::QUERY;
$this->errorsOnly = $errorsOnly;
}
@@ -38,7 +42,10 @@ class FileLogger
*/
public function logEvent(Dibi\Event $event): void
{
if (($event->type & $this->filter) === 0) {
if (
(($event->type & $this->filter) === 0)
|| ($this->errorsOnly === true && !$event->result instanceof \Exception)
) {
return;
}