Skip to content

Commit

Permalink
Syslog: URL mit loggen, Darstellung optimiert (#5598)
Browse files Browse the repository at this point in the history
Co-authored-by: tbaddade <thomas@redaxo.org>
  • Loading branch information
gharlan and tbaddade committed Feb 21, 2023
1 parent ccabcd4 commit fb45427
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 39 deletions.
Binary file modified .github/tests-visual/system_log--dark.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/tests-visual/system_log.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 0 additions & 5 deletions .tools/psalm/baseline.xml
Expand Up @@ -5345,11 +5345,6 @@
<code>isset($logFile)</code>
</TypeDoesNotContainNull>
</file>
<file src="redaxo/src/core/pages/system.log.redaxo.php">
<ArgumentTypeCoercion>
<code>$data[3] ?? 1</code>
</ArgumentTypeCoercion>
</file>
<file src="redaxo/src/core/pages/system.report.html.php">
<MixedArgument>
<code>$value</code>
Expand Down
2 changes: 1 addition & 1 deletion redaxo/src/addons/debug/lib/extensions/logger_debug.php
Expand Up @@ -7,7 +7,7 @@
*/
class rex_logger_debug extends rex_logger
{
public function log($level, $message, array $context = [], $file = null, $line = null)
public function log($level, $message, array $context = [], $file = null, $line = null, ?string $url = null)
{
$levelType = is_int($level) ? self::getLogLevel($level) : $level;

Expand Down
1 change: 1 addition & 0 deletions redaxo/src/core/lang/de_de.lang
Expand Up @@ -124,6 +124,7 @@ syslog_type = Typ
syslog_message = Nachricht
syslog_file = Datei
syslog_line = Zeile
syslog_url = URL
syslog_title = Logdatei "{0}"
syslog_phperrors = PHP Errorlog
syslog_slowqueries = DB Slow-Query log
Expand Down
25 changes: 23 additions & 2 deletions redaxo/src/core/lib/error_handler.php
Expand Up @@ -61,7 +61,7 @@ public static function handleException($exception)
}

try {
rex_logger::logException($exception);
rex_logger::logException($exception, self::getUrl());

// in case exceptions happen early - before symfony-console doRun()
if ('cli' === PHP_SAPI) {
Expand Down Expand Up @@ -324,7 +324,7 @@ public static function handleError($errno, $errstr, $errfile, $errline)
}
}

rex_logger::logError($errno, $errstr, $errfile, $errline);
rex_logger::logError($errno, $errstr, $errfile, $errline, self::getUrl());

return true;
}
Expand Down Expand Up @@ -368,6 +368,27 @@ public static function getErrorType($errno)
};
}

private static function getUrl(): ?string
{
if ('cli' === PHP_SAPI) {
return null;
}

try {
$request = rex::getRequest();
} catch (rex_exception) {
return null;
}

// Backend URLs use `/` inside page param, and we try to use them unencoded
// so for consistency we unencode them here too
$uri = preg_replace_callback('@(?<=\?page=)[\w/]+@', static function (array $match) {
return str_replace('%2F', '/', $match[0]);
}, $request->getRequestUri(), 1);

return $request->getSchemeAndHttpHost().$uri;
}

/**
* @param Throwable|Exception $exception
*
Expand Down
21 changes: 12 additions & 9 deletions redaxo/src/core/lib/util/logger.php
Expand Up @@ -33,13 +33,13 @@ public static function getPath()
* @param Throwable|Exception $exception The Exception to log
* @return void
*/
public static function logException($exception)
public static function logException($exception, ?string $url = null)
{
if ($exception instanceof ErrorException) {
self::logError($exception->getSeverity(), $exception->getMessage(), $exception->getFile(), $exception->getLine());
self::logError($exception->getSeverity(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), $url);
} else {
$logger = self::factory();
$logger->log($exception::class, $exception->getMessage(), [], $exception->getFile(), $exception->getLine());
$logger->log($exception::class, $exception->getMessage(), [], $exception->getFile(), $exception->getLine(), $url);
}
}

Expand All @@ -54,7 +54,7 @@ public static function logException($exception)
* @throws InvalidArgumentException
* @return void
*/
public static function logError($errno, $errstr, $errfile, $errline)
public static function logError($errno, $errstr, $errfile, $errline, ?string $url = null)
{
if (!is_int($errno)) {
throw new InvalidArgumentException('Expecting $errno to be integer, but ' . gettype($errno) . ' given!');
Expand All @@ -70,7 +70,7 @@ public static function logError($errno, $errstr, $errfile, $errline)
}

$logger = self::factory();
$logger->log(rex_error_handler::getErrorType($errno), $errstr, [], $errfile, $errline);
$logger->log(rex_error_handler::getErrorType($errno), $errstr, [], $errfile, $errline, $url);
}

/**
Expand All @@ -83,7 +83,7 @@ public static function logError($errno, $errstr, $errfile, $errline)
*
* @throws InvalidArgumentException
*/
public function log($level, $message, array $context = [], $file = null, $line = null)
public function log($level, $message, array $context = [], $file = null, $line = null, ?string $url = null)
{
if ($factoryClass = static::getExplicitFactoryClass()) {
$factoryClass::log($level, $message, $context, $file, $line);
Expand All @@ -109,9 +109,12 @@ public function log($level, $message, array $context = [], $file = null, $line =
}

$logData = [$level, $message];
if ($file && $line) {
$logData[] = rex_path::relative($file);
$logData[] = $line;
if ($file && $line || $url) {
$logData[] = $file ? rex_path::relative($file) : '';
$logData[] = $line ?? '';
if ($url) {
$logData[] = $url;
}
}
self::$file->add($logData);

Expand Down
58 changes: 36 additions & 22 deletions redaxo/src/core/pages/system.log.redaxo.php
Expand Up @@ -25,26 +25,20 @@
exit;
}

$message = '';
$content = '';

if ('' != $success) {
$message .= rex_view::success($success);
echo rex_view::success($success);
}

if ('' != $error) {
$message .= rex_view::error($error);
echo rex_view::error($error);
}

$content .= '
$content = '
<table class="table table-hover">
<thead>
<tr>
<th>' . rex_i18n::msg('syslog_timestamp') . '</th>
<th>' . rex_i18n::msg('syslog_type') . '</th>
<th>' . rex_i18n::msg('syslog_message') . '</th>
<th>' . rex_i18n::msg('syslog_file') . '</th>
<th class="rex-table-number">' . rex_i18n::msg('syslog_line') . '</th>
</tr>
</thead>
<tbody>';
Expand All @@ -56,26 +50,47 @@
/** @var rex_log_entry $entry */
$data = $entry->getData();

$class = strtolower($data[0]);
$class = in_array($class, ['notice', 'warning', 'success', 'info', 'debug'], true) ? $class : 'error';
$type = rex_type::string($data[0]);
$message = rex_type::string($data[1]);
$file = $data[2] ?? null;
$line = $data[3] ?? null;
$url = $data[4] ?? null;

$class = match (strtolower($type)) {
'debug' => 'default',
'info', 'notice', 'deprecated' => 'info',
'warning' => 'warning',
default => 'danger',
};

$path = '';
if (isset($data[2])) {
$path = rex_escape($data[2]);
if ($file) {
$path = rex_escape($file.(null === $line ? '' : ':'.$line));

$fullPath = str_starts_with($data[2], 'rex://') ? $data[2] : rex_path::base($data[2]);
if ($url = $editor->getUrl($fullPath, $data[3] ?? 1)) {
$fullPath = str_starts_with($file, 'rex://') ? $file : rex_path::base($file);
if ($url = $editor->getUrl($fullPath, (int) ($line ?? 1))) {
$path = '<a href="'.$url.'">'.$path.'</a>';
}
$path = '<small class="rex-word-break"><span class="label label-default">'.rex_i18n::msg('syslog_file').':</span> '.$path.'</small><br>';
}
if ($url) {
$url = rex_escape($url);
$url = '<small class="rex-word-break"><span class="label label-default">'.rex_i18n::msg('syslog_url').':</span> <a href="'.$url.'">'.$url.'</a></small>';
} else {
$url = '';
}

$content .= '
<tr class="rex-state-' . $class . '">
<td data-title="' . rex_i18n::msg('syslog_timestamp') . '" class="rex-table-tabular-nums">' . rex_formatter::intlDateTime($entry->getTimestamp(), [IntlDateFormatter::SHORT, IntlDateFormatter::MEDIUM]) . '</td>
<td data-title="' . rex_i18n::msg('syslog_type') . '"><div class="rex-word-break">' . rex_escape($data[0]) . '</div></td>
<td data-title="' . rex_i18n::msg('syslog_message') . '"><div class="rex-word-break">' . nl2br(rex_escape($data[1])) . '</div></td>
<td data-title="' . rex_i18n::msg('syslog_file') . '"><div class="rex-word-break">' . $path . '</div></td>
<td class="rex-table-number" data-title="' . rex_i18n::msg('syslog_line') . '">' . (isset($data[3]) ? rex_escape($data[3]) : '') . '</td>
<tr>
<td data-title="' . rex_i18n::msg('syslog_timestamp') . '" class="rex-table-tabular-nums rex-table-date">
<small>' . rex_formatter::intlDateTime($entry->getTimestamp(), [IntlDateFormatter::SHORT, IntlDateFormatter::MEDIUM]) . '</small><br>
<span class="label label-'.$class.'">' . rex_escape($type) . '</span>
</td>
<td data-title="' . rex_i18n::msg('syslog_message') . '">
<div class="rex-word-break"><b style="font-weight: 500">' . nl2br(rex_escape($message)) . '</b></div>
'.$path.'
'.$url.'
</td>
</tr>';
}

Expand Down Expand Up @@ -119,5 +134,4 @@
' . $content . '
</form>';

echo $message;
echo $content;

0 comments on commit fb45427

Please sign in to comment.