From 7ad3ad7a1079d1386b1363c7ffcce3bc1ed687ca Mon Sep 17 00:00:00 2001 From: Aleksey Filippov Date: Mon, 9 Feb 2026 22:08:22 +0300 Subject: [PATCH] fix error log save --- erp24/services/LogService.php | 43 +++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/erp24/services/LogService.php b/erp24/services/LogService.php index 1afc7d00..b7af1899 100644 --- a/erp24/services/LogService.php +++ b/erp24/services/LogService.php @@ -52,38 +52,48 @@ class LogService } public static function apiErrorLog($jsonString): void { - $input = isset(Yii::$app->request->getRawBody) ? Yii::$app->request->getRawBody() : ""; + $isWeb = Yii::$app instanceof \yii\web\Application; + + $input = ''; + if ($isWeb) { + $rawBody = Yii::$app->request->getRawBody(); + if (!empty($rawBody)) { + $input = $rawBody; + } + } + + $url = $isWeb ? (Yii::$app->request->url ?? '') : 'console'; + $ip = $isWeb ? (Yii::$app->request->remoteIP ?? '') : 'console'; $hash_input = hash('md5', $input); - $h = ApiErrorLog::find()->where(['hash_input' => $hash_input])->andWhere(['url' => Yii::$app->request->url ?? ''])->one(); - if (1) { -// if (!$h) { + + $h = ApiErrorLog::find() + ->where(['hash_input' => $hash_input]) + ->andWhere(['url' => $url]) + ->one(); + + if (!$h) { $apiErrorLog = new ApiErrorLog; - $apiErrorLog->url = Yii::$app->request->url ?? ''; + $apiErrorLog->url = $url; $apiErrorLog->created_at = date('Y-m-d H:i:s'); $apiErrorLog->input = $input; $apiErrorLog->hash_input = $hash_input; $apiErrorLog->payload = $jsonString; - $apiErrorLog->ip = Yii::$app->request->remoteIP ?? ''; + $apiErrorLog->ip = $ip; if (!$apiErrorLog->save()) { Yii::error('Ошибка сохранения логов: ' . json_encode($apiErrorLog->getErrors(), JSON_UNESCAPED_UNICODE)); } - /* if (!self::shouldSendToTelegram($hash_input, $jsonString)) { - return; - }*/ - // Форматирование сообщения об ошибке с условным добавлением строк $errorMessage = "⚠️*Ошибка API Обнаружена*⚠️\n\n"; - // Добавляем строки только если параметры присутствуют - if ($url = $apiErrorLog->url) { + if ($url) { $errorMessage .= "*URL:*\n```" . TelegramService::escapeMarkdownLog($url) . "```\n\n"; } - if ($createdAt = $apiErrorLog->created_at) { - $errorMessage .= "*Created At:*\n```" . "\n" . $createdAt . "```\n\n"; + if ($apiErrorLog->created_at) { + $errorMessage .= "*Created At:*\n```" . "\n" . $apiErrorLog->created_at . "```\n\n"; } - if ($ip = $apiErrorLog->ip) { + if ($ip) { $errorMessage .= "*IP:*\n```" . TelegramService::escapeMarkdownLog($ip) . "```\n\n"; } @@ -93,12 +103,11 @@ class LogService $isDev = TelegramService::isDevEnv(); $disableNotification = false; - // Отправляем сообщение об ошибке в Telegram через TelegramService TelegramService::sendErrorToTelegramMessage($errorMessage, $disableNotification, $isDev); } else { $h->actualed_at = date('Y-m-d H:i:s'); - $h->ip = Yii::$app->request->remoteIP ?? ''; + $h->ip = $ip; $h->save(); } } -- 2.39.5