From eb4c5da98bd3600bc6a1e1a449c941af2b859dc5 Mon Sep 17 00:00:00 2001 From: Fernando Castillo Date: Thu, 3 Mar 2022 17:34:10 +0100 Subject: [PATCH] Limit the log output --- src/Logger/KatanaLogger.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Logger/KatanaLogger.php b/src/Logger/KatanaLogger.php index c32dc25..219e4cf 100644 --- a/src/Logger/KatanaLogger.php +++ b/src/Logger/KatanaLogger.php @@ -36,6 +36,9 @@ abstract class KatanaLogger self::LOG_NONE => 'NONE', ]; + const MAX_LOG_LENGTH = 65535; + const LOG_TRUNCATE_REPLACEMENT = '... (truncated)'; + /** * @var int */ @@ -87,7 +90,15 @@ protected function log(int $level, string $message) return; } - echo $this->formatMessage($level, $message), "\n"; + $log = $this->formatMessage($level, '{{message}}'); + $logLength = strlen($log) + strlen($message) - 11; + + if ($logLength > self::MAX_LOG_LENGTH) { + $messageExcessLength = self::MAX_LOG_LENGTH - $logLength - strlen(self::LOG_TRUNCATE_REPLACEMENT); + $message = substr($message, 0, $messageExcessLength) . self::LOG_TRUNCATE_REPLACEMENT; + } + + echo str_replace('{{message}}', $message, $log), "\n"; } /**