fix: restore php error_log writing (#4196)

This commit is contained in:
Dag 2024-08-07 18:09:44 +02:00 committed by GitHub
parent 313be4c512
commit c11bc184ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 61 additions and 27 deletions

View file

@ -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();

View file

@ -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 = [])