Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log Service : When sql syntax error occurs, log sql in error level. #2443

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/Factory/BaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,15 @@ protected function parseComparisonOperator($variable)
* @param array $params Array of parameters passed by reference
* @param bool $useRegex flag to match against a regex pattern
*/
public function nameFilter($tableName, $tableColumn, $terms, &$body, &$params, $useRegex = false, $logicalOperator = 'OR')
{
public function nameFilter(
$tableName,
$tableColumn,
$terms,
&$body,
&$params,
$useRegex = false,
$logicalOperator = 'OR'
) {
$i = 0;

$tableAndColumn = $tableName . '.' . $tableColumn;
Expand All @@ -377,7 +384,7 @@ public function nameFilter($tableName, $tableColumn, $terms, &$body, &$params, $
$searchName = trim($searchName);

// Discard any incompatible
if ($searchName === '-' || empty($searchName)) {
if (empty(ltrim($searchName, '-')) || empty($searchName)) {
continue;
}

Expand Down
11 changes: 7 additions & 4 deletions lib/Service/LogService.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* Copyright (C) 2023 Xibo Signage Ltd
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - https://xibosignage.com
*
Expand Down Expand Up @@ -147,14 +147,17 @@ public function audit($entity, $entityId, $message, $object)
/**
* @inheritdoc
*/
public function sql($sql, $params)
public function sql($sql, $params, $logAsError = false)
{
if (strtolower($this->mode) == 'test') {
if (strtolower($this->mode) == 'test' || $logAsError) {
$paramSql = '';
foreach ($params as $key => $param) {
$paramSql .= 'SET @' . $key . '=\'' . $param . '\';' . PHP_EOL;
}
$this->log->debug($paramSql . str_replace(':', '@', $sql));

($logAsError)
? $this->log->error($paramSql . str_replace(':', '@', $sql))
: $this->log->debug($paramSql . str_replace(':', '@', $sql));
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/Service/LogServiceInterface.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* Copyright (C) 2021 Xibo Signage Ltd
/*
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - http://www.xibo.org.uk
* Xibo - Digital Signage - https://xibosignage.com
*
* This file is part of Xibo.
*
Expand All @@ -23,7 +23,6 @@

namespace Xibo\Service;

use Monolog\Logger;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -77,9 +76,10 @@ public function audit($entity, $entityId, $message, $object);
/**
* @param $sql
* @param $params
* @param bool $logAsError
* @return mixed
*/
public function sql($sql, $params);
public function sql($sql, $params, $logAsError = false);

/**
* @param string
Expand Down
11 changes: 8 additions & 3 deletions lib/Storage/PdoStorageService.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* Copyright (C) 2023 Xibo Signage Ltd
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - https://xibosignage.com
*
Expand Down Expand Up @@ -334,13 +334,18 @@ public function select($sql, $params, $connection = 'default', $reconnect = fals
}
return $records;
} catch (\PDOException $PDOException) {
$errorCode = $PDOException->errorInfo[1] ?? $PDOException->getCode();

// syntax error, log the sql and params in error level.
if ($errorCode == 1064 && $this->log != null) {
$this->log->sql($sql, $params, true);
}

// Throw if we're not expected to reconnect.
if (!$reconnect) {
throw $PDOException;
}

$errorCode = $PDOException->errorInfo[1] ?? $PDOException->getCode();

if ($errorCode != 2006) {
throw $PDOException;
} else {
Expand Down
Loading