diff --git a/app/support/BaseController.php b/app/support/BaseController.php index 92db6c5..68b6d27 100644 --- a/app/support/BaseController.php +++ b/app/support/BaseController.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace app\support; +use support\Log; use support\Response; use Webman\Http\Request as WebmanRequest; @@ -93,6 +94,20 @@ abstract class BaseController $statusCode = $header['statusCode'] ?? 200; unset($header['statusCode']); + if ($code !== 1) { + $req = $this->request; + $method = $req ? $req->method() : ''; + $path = $req ? trim($req->path(), '/') : ''; + $ip = $req ? strval($req->getRealIp()) : ''; + $msgForLog = $msg; + if (function_exists('mb_strlen') && function_exists('mb_substr') && mb_strlen($msgForLog) > 2000) { + $msgForLog = mb_substr($msgForLog, 0, 2000) . '...'; + } elseif (strlen($msgForLog) > 2000) { + $msgForLog = substr($msgForLog, 0, 2000) . '...'; + } + Log::error('[API] ' . $method . ' /' . $path . ' | business_code=' . strval($code) . ' http=' . strval($statusCode) . ' ip=' . $ip . ' msg=' . $msgForLog); + } + $headers = array_merge(['Content-Type' => 'application/json'], $header); $options = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES; if (defined('JSON_INVALID_UTF8_SUBSTITUTE')) {