|
@@ -145,17 +145,7 @@ class TshieldWebhookController
|
|
|
$encoded = '{"ts":"' . $ts . '","service":"tshield_webhook","request_id":"' . $requestId . '","error":"unable_to_encode_log"}';
|
|
$encoded = '{"ts":"' . $ts . '","service":"tshield_webhook","request_id":"' . $requestId . '","error":"unable_to_encode_log"}';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $written = false;
|
|
|
|
|
- try {
|
|
|
|
|
- $written = (@file_put_contents($this->logFilePath, $encoded . "\n", FILE_APPEND | LOCK_EX) !== false);
|
|
|
|
|
- } catch (\Throwable $e) {
|
|
|
|
|
- $written = false;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (!$written) {
|
|
|
|
|
- $lastError = error_get_last();
|
|
|
|
|
- error_log('[TShieldWebhook] failed_to_write_log file=' . $this->logFilePath . ' error=' . json_encode($lastError));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $this->appendLogLine($encoded);
|
|
|
|
|
|
|
|
error_log('[TShieldWebhook] ' . $encoded);
|
|
error_log('[TShieldWebhook] ' . $encoded);
|
|
|
}
|
|
}
|
|
@@ -185,19 +175,37 @@ class TshieldWebhookController
|
|
|
$encoded = '{"ts":"' . $ts . '","service":"tshield_webhook","event":"received","request_id":"' . $requestId . '","error":"unable_to_encode_log"}';
|
|
$encoded = '{"ts":"' . $ts . '","service":"tshield_webhook","event":"received","request_id":"' . $requestId . '","error":"unable_to_encode_log"}';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $written = false;
|
|
|
|
|
|
|
+ $this->appendLogLine($encoded);
|
|
|
|
|
+
|
|
|
|
|
+ error_log('[TShieldWebhook] ' . $encoded);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function appendLogLine(string $encoded): bool
|
|
|
|
|
+ {
|
|
|
|
|
+ $phpError = null;
|
|
|
|
|
+
|
|
|
|
|
+ $handler = function (int $severity, string $message) use (&$phpError): bool {
|
|
|
|
|
+ $phpError = $message;
|
|
|
|
|
+ return true;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ set_error_handler($handler);
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
- $written = (@file_put_contents($this->logFilePath, $encoded . "\n", FILE_APPEND | LOCK_EX) !== false);
|
|
|
|
|
|
|
+ $result = file_put_contents($this->logFilePath, $encoded . "\n", FILE_APPEND | LOCK_EX);
|
|
|
} catch (\Throwable $e) {
|
|
} catch (\Throwable $e) {
|
|
|
- $written = false;
|
|
|
|
|
|
|
+ $result = false;
|
|
|
|
|
+ $phpError = $phpError ?? ($e->getMessage());
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ restore_error_handler();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (!$written) {
|
|
|
|
|
- $lastError = error_get_last();
|
|
|
|
|
- error_log('[TShieldWebhook] failed_to_write_log file=' . $this->logFilePath . ' error=' . json_encode($lastError));
|
|
|
|
|
|
|
+ if ($result === false) {
|
|
|
|
|
+ error_log('[TShieldWebhook] failed_to_write_log file=' . $this->logFilePath . ' php_error=' . json_encode($phpError));
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- error_log('[TShieldWebhook] ' . $encoded);
|
|
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private function getResponseBodyForLog(ResponseInterface $response): ?string
|
|
private function getResponseBodyForLog(ResponseInterface $response): ?string
|
|
@@ -236,7 +244,38 @@ class TshieldWebhookController
|
|
|
|
|
|
|
|
$dir = dirname($path);
|
|
$dir = dirname($path);
|
|
|
if (!is_dir($dir)) {
|
|
if (!is_dir($dir)) {
|
|
|
- @mkdir($dir, 0775, true);
|
|
|
|
|
|
|
+ if (!@mkdir($dir, 0775, true) && !is_dir($dir)) {
|
|
|
|
|
+ $lastError = error_get_last();
|
|
|
|
|
+ error_log('[TShieldWebhook] failed_to_create_log_dir dir=' . $dir . ' error=' . json_encode($lastError));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (is_dir($dir) && !is_writable($dir)) {
|
|
|
|
|
+ error_log('[TShieldWebhook] log_dir_not_writable dir=' . $dir);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!file_exists($path)) {
|
|
|
|
|
+ $phpError = null;
|
|
|
|
|
+ $handler = function (int $severity, string $message) use (&$phpError): bool {
|
|
|
|
|
+ $phpError = $message;
|
|
|
|
|
+ return true;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ set_error_handler($handler);
|
|
|
|
|
+ try {
|
|
|
|
|
+ $created = (file_put_contents($path, '') !== false);
|
|
|
|
|
+ } catch (\Throwable $e) {
|
|
|
|
|
+ $created = false;
|
|
|
|
|
+ $phpError = $phpError ?? ($e->getMessage());
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ restore_error_handler();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!$created) {
|
|
|
|
|
+ error_log('[TShieldWebhook] failed_to_create_log_file file=' . $path . ' php_error=' . json_encode($phpError));
|
|
|
|
|
+ }
|
|
|
|
|
+ } elseif (!is_writable($path)) {
|
|
|
|
|
+ error_log('[TShieldWebhook] log_file_not_writable file=' . $path);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return $path;
|
|
return $path;
|