Добавлен класс 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\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);
}
}
}
Logger::log($pathLogErrorHtml, 'stop');
Logger::log($pathLogErrorHttp, 'stop');

View File

@ -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);
}
}
}

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 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:47 61220 www.kazeu.kz
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 61277
2024-08-16 10:38:13 61279
2024-08-16 11:59:25 start