]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
fix error log save origin/feature_filippov_20260209_log_save_fix
authorAleksey Filippov <Aleksey.Filippov@erp-flowers.ru>
Mon, 9 Feb 2026 19:08:22 +0000 (22:08 +0300)
committerAleksey Filippov <Aleksey.Filippov@erp-flowers.ru>
Mon, 9 Feb 2026 19:08:22 +0000 (22:08 +0300)
erp24/services/LogService.php

index 1afc7d002b598e0dfbc63293321f44c5b0e178f7..b7af1899fa3e7c5b6aa1454e41aec265ca116ee2 100644 (file)
@@ -52,38 +52,48 @@ class LogService
     }
 
     public static function apiErrorLog($jsonString): void {
-        $input = isset(Yii::$app->request->getRawBody) ? Yii::$app->request->getRawBody() : "<no input>";
+        $isWeb = Yii::$app instanceof \yii\web\Application;
+
+        $input = '<no input>';
+        if ($isWeb) {
+            $rawBody = Yii::$app->request->getRawBody();
+            if (!empty($rawBody)) {
+                $input = $rawBody;
+            }
+        }
+
+        $url = $isWeb ? (Yii::$app->request->url ?? '<no url>') : 'console';
+        $ip = $isWeb ? (Yii::$app->request->remoteIP ?? '<no ip>') : 'console';
         $hash_input = hash('md5', $input);
-        $h = ApiErrorLog::find()->where(['hash_input' => $hash_input])->andWhere(['url' => Yii::$app->request->url ?? '<no 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 ?? '<no 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 ?? '<no ip>';
+            $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 ?? '<no ip>';
+            $h->ip = $ip;
             $h->save();
         }
     }