From c11bc184caec2537f80161ca4d792fa238ddeceb Mon Sep 17 00:00:00 2001 From: Dag Date: Wed, 7 Aug 2024 18:09:44 +0200 Subject: [PATCH] fix: restore php error_log writing (#4196) --- index.php | 5 +-- lib/logger.php | 83 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 61 insertions(+), 27 deletions(-) diff --git a/index.php b/index.php index ccb2316f..d29900f7 100644 --- a/index.php +++ b/index.php @@ -61,11 +61,12 @@ register_shutdown_function(function () use ($logger) { }); $cacheFactory = new CacheFactory($logger); + if (Debug::isEnabled()) { - $logger->addHandler(new StreamHandler('php://stderr', Logger::DEBUG)); + $logger->addHandler(new ErrorLogHandler(Logger::DEBUG)); $cache = $cacheFactory->create('array'); } else { - $logger->addHandler(new StreamHandler('php://stderr', Logger::INFO)); + $logger->addHandler(new ErrorLogHandler(Logger::INFO)); $cache = $cacheFactory->create(); } $httpClient = new CurlHttpClient(); diff --git a/lib/logger.php b/lib/logger.php index 916e65ed..3ebe3b0a 100644 --- a/lib/logger.php +++ b/lib/logger.php @@ -68,6 +68,16 @@ final class SimpleLogger implements Logger private function log(int $level, string $message, array $context = []): void { + $ignoredMessages = [ + 'Format name invalid', + 'Unknown format given', + 'Unable to find channel', + ]; + foreach ($ignoredMessages as $ignoredMessage) { + if (str_starts_with($message, $ignoredMessage)) { + return; + } + } foreach ($this->handlers as $handler) { $handler([ 'name' => $this->name, @@ -83,7 +93,7 @@ final class SimpleLogger implements Logger final class StreamHandler { - private $stream; + private string $stream; private int $level; public function __construct(string $stream, int $level = Logger::DEBUG) @@ -108,28 +118,6 @@ final class StreamHandler $record['context']['line'] = $e->getLine(); $record['context']['url'] = get_current_url(); $record['context']['trace'] = trace_to_call_points(trace_from_exception($e)); - - $ignoredExceptions = [ - 'You must specify a format', - 'Format name invalid', - 'Unknown format given', - 'Bridge name invalid', - 'Invalid action', - 'twitter: No results for this query', - // telegram - 'Unable to find channel. The channel is non-existing or non-public', - // fb - 'This group is not public! RSS-Bridge only supports public groups!', - 'You must be logged in to view this page', - 'Unable to get the page id. You should consider getting the ID by hand', - // tiktok 404 - 'https://www.tiktok.com/@', - ]; - foreach ($ignoredExceptions as $ignoredException) { - if (str_starts_with($e->getMessage(), $ignoredException)) { - return; - } - } } $context = ''; if ($record['context']) { @@ -145,15 +133,60 @@ final class StreamHandler $record['created_at']->format('Y-m-d H:i:s'), $record['name'], $record['level_name'], - // Should probably sanitize message for output context $record['message'], $context ); - $bytes = file_put_contents($this->stream, $text, FILE_APPEND | LOCK_EX); } } +final class ErrorLogHandler +{ + private int $level; + + public function __construct(int $level = Logger::DEBUG) + { + $this->level = $level; + } + + public function __invoke(array $record) + { + if ($record['level'] < $this->level) { + return; + } + if (isset($record['context']['e'])) { + /** @var \Throwable $e */ + $e = $record['context']['e']; + unset($record['context']['e']); + $record['context']['type'] = get_class($e); + $record['context']['code'] = $e->getCode(); + $record['context']['message'] = sanitize_root($e->getMessage()); + $record['context']['file'] = sanitize_root($e->getFile()); + $record['context']['line'] = $e->getLine(); + $record['context']['url'] = get_current_url(); + $record['context']['trace'] = trace_to_call_points(trace_from_exception($e)); + } + $context = ''; + if ($record['context']) { + try { + $context = Json::encode($record['context']); + } catch (\JsonException $e) { + $record['context']['message'] = null; + $context = Json::encode($record['context']); + } + } + $text = sprintf( + "[%s] %s.%s %s %s\n", + $record['created_at']->format('Y-m-d H:i:s'), + $record['name'], + $record['level_name'], + $record['message'], + $context + ); + error_log($text); + } +} + final class NullLogger implements Logger { public function debug(string $message, array $context = [])