Добавлен класс Logger

This commit is contained in:
Alexander 2024-08-16 15:00:37 +03:00
parent f7bbcf41a6
commit adc163edfe
6 changed files with 48 additions and 22 deletions

View File

@ -3,6 +3,7 @@ namespace App;
use App\Library\ContingentManager; use App\Library\ContingentManager;
use App\Library\DatabaseConfig; use App\Library\DatabaseConfig;
use App\Library\Logger;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
@ -12,9 +13,15 @@ use App\Library\ContingentParser;
use App\Library\Database; use App\Library\Database;
use GuzzleHttp\Client; 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')); $dbOpendata = new Database(new DatabaseConfig('opendata'));
$dbNiimko = new Database(new DatabaseConfig('niimko')); $dbNiimko = new Database(new DatabaseConfig('niimko'));
exit(0);
$sites = ContingentManager::getInstance()->getSites($dbNiimko); $sites = ContingentManager::getInstance()->getSites($dbNiimko);
$specializations = ContingentManager::getInstance()->getSpecializations($dbNiimko); $specializations = ContingentManager::getInstance()->getSpecializations($dbNiimko);
$sql = 'SELECT DISTINCT org_id FROM sveden_education_contingent'; $sql = 'SELECT DISTINCT org_id FROM sveden_education_contingent';
@ -53,8 +60,8 @@ $start = 600;
for ($i = $start; $i < count($sites); $i++) { for ($i = $start; $i < count($sites); $i++) {
// Нет URL сайта вуза // Нет URL сайта вуза
if (empty($sites[$i]['site'])) { if (empty($sites[$i]['site'])) {
$log = date('Y-m-d H:i:s') . ' ' . $sites[$i]['org_id'] . ' ' . $sites[$i]['site']; $message = $sites[$i]['org_id'] . ' ' . $sites[$i]['site'];
file_put_contents(__DIR__ . '/../error-http.log', $log . PHP_EOL, FILE_APPEND); Logger::log($pathLogErrorHttp, $message);
continue; continue;
} }
// Уже в базе // Уже в базе
@ -112,21 +119,24 @@ for ($i = $start; $i < count($sites); $i++) {
$status = 0; $status = 0;
} finally { } finally {
if ($status != 200) { if ($status != 200) {
$log = date('Y-m-d H:i:s') . ' ' . $sites[$i]['org_id'] . ' ' . $sites[$i]['site']; $message = $sites[$i]['org_id'] . ' ' . $sites[$i]['site'];
file_put_contents(__DIR__ . '/../error-http.log', $log . PHP_EOL, FILE_APPEND); Logger::log($pathLogErrorHttp, $message);
} else if (empty($contingent)) { } else if (empty($contingent)) {
$log = date('Y-m-d H:i:s') . ' ' . $sites[$i]['org_id'] . ' ' . $sites[$i]['site']; $message = $sites[$i]['org_id'] . ' ' . $sites[$i]['site'];
file_put_contents(__DIR__ . '/../error-html.log', $log . PHP_EOL, FILE_APPEND); Logger::log($pathLogErrorHtml, $message);
} else { } else {
$set = ContingentManager::getInstance()->checkContingent($contingent); $set = ContingentManager::getInstance()->checkContingent($contingent);
if ($set) { if ($set) {
// Заносим в базу // Заносим в базу
// ContingentManager::getInstance()->insertContingent($dbOpendata, $contingent); // ContingentManager::getInstance()->insertContingent($dbOpendata, $contingent);
} else { } else {
$log = date('Y-m-d H:i:s') . ' ' . $sites[$i]['org_id'] . ' ' . $sites[$i]['site']; $message = $sites[$i]['org_id'] . ' ' . $sites[$i]['site'];
file_put_contents(__DIR__ . '/../error-html.log', $log . PHP_EOL, FILE_APPEND); Logger::log($pathLogErrorHtml, $message);
} }
unset($contingent); unset($contingent);
} }
} }
} }
Logger::log($pathLogErrorHtml, 'stop');
Logger::log($pathLogErrorHttp, 'stop');

View File

@ -2,11 +2,13 @@
namespace App\Library; namespace App\Library;
use App\Library\DatabaseConfig; use App\Library\DatabaseConfig;
use App\Library\Logger;
use PDOException; use PDOException;
use PDO; use PDO;
class Database class Database
{ {
private PDO $pdo; private PDO $pdo;
private static $logFile = 'database.log';
public function __construct(DatabaseConfig $config) public function __construct(DatabaseConfig $config)
{ {
try { try {
@ -19,18 +21,16 @@ class Database
$password, $password,
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
); );
$log = date('Y-m-d H:i:s') . ' Подлючение к успешно!'; Logger::log(self::$logFile, "Подключение успешно!");
file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND);
} catch (PDOException $e) { } catch (PDOException $e) {
$log = date('Y-m-d H:i:s') . " Ошибка подключения:" . $e->getMessage(); $message = "Ошибка подключения:" . $e->getMessage();
file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND); Logger::log(self::$logFile, $message);
} }
} }
public function __destruct() public function __destruct()
{ {
$log = date('Y-m-d H:i:s') . ' Подлючение прервано!'; Logger::log(self::$logFile, "Подключение прервано!");
file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND);
} }
public function selectQuery(string $sql, array $params = []) : array public function selectQuery(string $sql, array $params = []) : array
@ -44,8 +44,8 @@ class Database
$stmt->execute(); $stmt->execute();
$array = $stmt->fetchAll(PDO::FETCH_ASSOC); $array = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
$log = date('Y-m-d H:i:s') . " Ошибка запроса:" . $e->getMessage(); $message = "Ошибка запроса:" . $e->getMessage();
file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND); Logger::log(self::$logFile, $message);
} finally { } finally {
return $array; return $array;
} }
@ -65,11 +65,10 @@ class Database
$stmt->bindParam(":spec_id".$i+1, $params[$i]['spec_id']); $stmt->bindParam(":spec_id".$i+1, $params[$i]['spec_id']);
} }
$stmt->execute(); $stmt->execute();
$log = date('Y-m-d H:i:s') . " Запрос выполнен успешно!"; Logger::log(self::$logFile, "Запрос выполнен успешно!");
file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND);
} catch (PDOException $e) { } catch (PDOException $e) {
$log = date('Y-m-d H:i:s') . " Ошибка запроса:" . $e->getMessage(); $message = "Ошибка запроса:" . $e->getMessage();
file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND); Logger::log(self::$logFile, $message);
} }
} }
} }

11
app/library/Logger.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace App\Library;
class Logger
{
public static function log($path, $message)
{
$log = date('Y-m-d H:i:s') . ' ' . $message;
file_put_contents($path, $log . PHP_EOL, FILE_APPEND);
}
}

View File

@ -63,3 +63,7 @@
2024-08-16 10:38:13 Запрос выполнен успешно! 2024-08-16 10:38:13 Запрос выполнен успешно!
2024-08-16 10:38:13 Подлючение прервано! 2024-08-16 10:38:13 Подлючение прервано!
2024-08-16 10:38:13 Подлючение прервано! 2024-08-16 10:38:13 Подлючение прервано!
2024-08-16 11:59:25 Подключение успешно!
2024-08-16 11:59:25 Подключение успешно!
2024-08-16 11:59:25 Подключение прервано!
2024-08-16 11:59:25 Подключение прервано!

View File

@ -502,3 +502,4 @@
2024-08-15 15:45:42 60248 http://www.tashiit.uz/index.php/tr/ 2024-08-15 15:45:42 60248 http://www.tashiit.uz/index.php/tr/
2024-08-15 15:45:47 61220 www.kazeu.kz 2024-08-15 15:45:47 61220 www.kazeu.kz
2024-08-15 15:45:54 61248 http://www.cfuv.ru/ 2024-08-15 15:45:54 61248 http://www.cfuv.ru/
2024-08-16 11:59:25 start

View File

@ -843,3 +843,4 @@
2024-08-16 10:38:13 61275 2024-08-16 10:38:13 61275
2024-08-16 10:38:13 61277 2024-08-16 10:38:13 61277
2024-08-16 10:38:13 61279 2024-08-16 10:38:13 61279
2024-08-16 11:59:25 start