}
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";
}
$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();
}
}