Skip to content

Commit

Permalink
fix: restore php error_log writing (RSS-Bridge#4196)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan authored and NotsoanoNimus committed Aug 8, 2024
1 parent 7a0ea28 commit cbcd3ef
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
5 changes: 3 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@
});

$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();
Expand Down
83 changes: 58 additions & 25 deletions lib/logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public function error(string $message, array $context = []): void

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,
Expand All @@ -83,7 +93,7 @@ private function log(int $level, string $message, array $context = []): void

final class StreamHandler
{
private $stream;
private string $stream;
private int $level;

public function __construct(string $stream, int $level = Logger::DEBUG)
Expand All @@ -108,28 +118,6 @@ public function __invoke(array $record)
$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']) {
Expand All @@ -145,15 +133,60 @@ public function __invoke(array $record)
$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 = [])
Expand Down

0 comments on commit cbcd3ef

Please sign in to comment.