From adc163edfee7d392ae8aff12aaf6421c8eb21e9e Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 16 Aug 2024 15:00:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20Logger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app.php | 30 ++++++++++++++++++++---------- app/library/Database.php | 23 +++++++++++------------ app/library/Logger.php | 11 +++++++++++ app/database.log => database.log | 4 ++++ error-html.log | 1 + error-http.log | 1 + 6 files changed, 48 insertions(+), 22 deletions(-) create mode 100644 app/library/Logger.php rename app/database.log => database.log (94%) diff --git a/app/app.php b/app/app.php index e575c2b..c91f0e6 100644 --- a/app/app.php +++ b/app/app.php @@ -3,6 +3,7 @@ namespace App; use App\Library\ContingentManager; use App\Library\DatabaseConfig; +use App\Library\Logger; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\RequestException; @@ -12,9 +13,15 @@ use App\Library\ContingentParser; use App\Library\Database; use GuzzleHttp\Client; +$pathLogErrorHtml = 'error-html.log'; +$pathLogErrorHttp = 'error-http.log'; + +Logger::log($pathLogErrorHtml, 'start'); +Logger::log($pathLogErrorHttp, 'start'); + $dbOpendata = new Database(new DatabaseConfig('opendata')); $dbNiimko = new Database(new DatabaseConfig('niimko')); - +exit(0); $sites = ContingentManager::getInstance()->getSites($dbNiimko); $specializations = ContingentManager::getInstance()->getSpecializations($dbNiimko); $sql = 'SELECT DISTINCT org_id FROM sveden_education_contingent'; @@ -53,8 +60,8 @@ $start = 600; for ($i = $start; $i < count($sites); $i++) { // Нет URL сайта вуза if (empty($sites[$i]['site'])) { - $log = date('Y-m-d H:i:s') . ' ' . $sites[$i]['org_id'] . ' ' . $sites[$i]['site']; - file_put_contents(__DIR__ . '/../error-http.log', $log . PHP_EOL, FILE_APPEND); + $message = $sites[$i]['org_id'] . ' ' . $sites[$i]['site']; + Logger::log($pathLogErrorHttp, $message); continue; } // Уже в базе @@ -112,21 +119,24 @@ for ($i = $start; $i < count($sites); $i++) { $status = 0; } finally { if ($status != 200) { - $log = date('Y-m-d H:i:s') . ' ' . $sites[$i]['org_id'] . ' ' . $sites[$i]['site']; - file_put_contents(__DIR__ . '/../error-http.log', $log . PHP_EOL, FILE_APPEND); + $message = $sites[$i]['org_id'] . ' ' . $sites[$i]['site']; + Logger::log($pathLogErrorHttp, $message); } else if (empty($contingent)) { - $log = date('Y-m-d H:i:s') . ' ' . $sites[$i]['org_id'] . ' ' . $sites[$i]['site']; - file_put_contents(__DIR__ . '/../error-html.log', $log . PHP_EOL, FILE_APPEND); + $message = $sites[$i]['org_id'] . ' ' . $sites[$i]['site']; + Logger::log($pathLogErrorHtml, $message); } else { $set = ContingentManager::getInstance()->checkContingent($contingent); if ($set) { // Заносим в базу // ContingentManager::getInstance()->insertContingent($dbOpendata, $contingent); } else { - $log = date('Y-m-d H:i:s') . ' ' . $sites[$i]['org_id'] . ' ' . $sites[$i]['site']; - file_put_contents(__DIR__ . '/../error-html.log', $log . PHP_EOL, FILE_APPEND); + $message = $sites[$i]['org_id'] . ' ' . $sites[$i]['site']; + Logger::log($pathLogErrorHtml, $message); } unset($contingent); } } -} \ No newline at end of file +} + +Logger::log($pathLogErrorHtml, 'stop'); +Logger::log($pathLogErrorHttp, 'stop'); \ No newline at end of file diff --git a/app/library/Database.php b/app/library/Database.php index 6f72f16..aff7eab 100644 --- a/app/library/Database.php +++ b/app/library/Database.php @@ -2,11 +2,13 @@ namespace App\Library; use App\Library\DatabaseConfig; +use App\Library\Logger; use PDOException; use PDO; class Database { private PDO $pdo; + private static $logFile = 'database.log'; public function __construct(DatabaseConfig $config) { try { @@ -19,18 +21,16 @@ class Database $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] ); - $log = date('Y-m-d H:i:s') . ' Подлючение к успешно!'; - file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND); + Logger::log(self::$logFile, "Подключение успешно!"); } catch (PDOException $e) { - $log = date('Y-m-d H:i:s') . " Ошибка подключения:" . $e->getMessage(); - file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND); + $message = "Ошибка подключения:" . $e->getMessage(); + Logger::log(self::$logFile, $message); } } public function __destruct() { - $log = date('Y-m-d H:i:s') . ' Подлючение прервано!'; - file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND); + Logger::log(self::$logFile, "Подключение прервано!"); } public function selectQuery(string $sql, array $params = []) : array @@ -44,8 +44,8 @@ class Database $stmt->execute(); $array = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { - $log = date('Y-m-d H:i:s') . " Ошибка запроса:" . $e->getMessage(); - file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND); + $message = "Ошибка запроса:" . $e->getMessage(); + Logger::log(self::$logFile, $message); } finally { return $array; } @@ -65,11 +65,10 @@ class Database $stmt->bindParam(":spec_id".$i+1, $params[$i]['spec_id']); } $stmt->execute(); - $log = date('Y-m-d H:i:s') . " Запрос выполнен успешно!"; - file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND); + Logger::log(self::$logFile, "Запрос выполнен успешно!"); } catch (PDOException $e) { - $log = date('Y-m-d H:i:s') . " Ошибка запроса:" . $e->getMessage(); - file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND); + $message = "Ошибка запроса:" . $e->getMessage(); + Logger::log(self::$logFile, $message); } } } \ No newline at end of file diff --git a/app/library/Logger.php b/app/library/Logger.php new file mode 100644 index 0000000..c4ea404 --- /dev/null +++ b/app/library/Logger.php @@ -0,0 +1,11 @@ +