Skip to content
Extraits de code Groupes Projets
Valider 79c3920f rédigé par Matthieu Napoli's avatar Matthieu Napoli
Parcourir les fichiers

#6622 Logger refactoring: simplified the formatters by removing the ErrorTextFormatter

parent 45a61af6
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -6,7 +6,6 @@ use Piwik\Log; ...@@ -6,7 +6,6 @@ use Piwik\Log;
use Piwik\Log\Backend\StdErrBackend; use Piwik\Log\Backend\StdErrBackend;
use Piwik\Log\Formatter\AddRequestIdFormatter; use Piwik\Log\Formatter\AddRequestIdFormatter;
use Piwik\Log\Formatter\ErrorHtmlFormatter; use Piwik\Log\Formatter\ErrorHtmlFormatter;
use Piwik\Log\Formatter\ErrorTextFormatter;
use Piwik\Log\Formatter\ExceptionHtmlFormatter; use Piwik\Log\Formatter\ExceptionHtmlFormatter;
use Piwik\Log\Formatter\ExceptionTextFormatter; use Piwik\Log\Formatter\ExceptionTextFormatter;
use Piwik\Log\Formatter\HtmlPreFormatter; use Piwik\Log\Formatter\HtmlPreFormatter;
...@@ -130,16 +129,11 @@ return array( ...@@ -130,16 +129,11 @@ return array(
return $logPath; return $logPath;
}), }),
'log.formatter.text' => DI\factory(function (ContainerInterface $c) { 'log.formatter.text' => DI\factory(function (ContainerInterface $c) {
// Chain of responsibility pattern
$lineFormatter = new LineMessageFormatter($c->get('log.format'));
$exceptionFormatter = new ExceptionTextFormatter(); $exceptionFormatter = new ExceptionTextFormatter();
$exceptionFormatter->setNext($lineFormatter); $exceptionFormatter->setNext(
new LineMessageFormatter($c->get('log.format'))
$errorFormatter = new ErrorTextFormatter(); );
$errorFormatter->setNext($exceptionFormatter); return $exceptionFormatter;
return $errorFormatter;
}), }),
'log.formatter.html' => DI\factory(function (ContainerInterface $c) { 'log.formatter.html' => DI\factory(function (ContainerInterface $c) {
// Chain of responsibility pattern // Chain of responsibility pattern
......
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Log\Formatter;
use Piwik\Error;
use Piwik\Log;
/**
* Formats a log message containing a Piwik\Error object into a textual string.
*/
class ErrorTextFormatter extends Formatter
{
public function format(array $record)
{
if (! $this->contextContainsError($record)) {
return $this->next($record);
}
/** @var \ErrorException $exception */
$exception = $record['context']['exception'];
$trace = Log::$debugBacktraceForTests ?: $exception->getTraceAsString();
$record['message'] = $exception->getFile() . '(' . $exception->getLine() . '): ' . Error::getErrNoString($exception->getSeverity())
. ' - ' . $exception->getMessage() . "\n" . $trace;
// Remove the exception so that it's not formatted again by another formatter
unset($record['context']['exception']);
return $this->next($record);
}
private function contextContainsError($record)
{
return isset($record['context']['exception'])
&& $record['context']['exception'] instanceof \ErrorException;
}
}
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
namespace Piwik\Log\Formatter; namespace Piwik\Log\Formatter;
use Piwik\ExceptionHandler; use Piwik\Error;
use Piwik\Log; use Piwik\Log;
/** /**
...@@ -25,10 +25,14 @@ class ExceptionTextFormatter extends Formatter ...@@ -25,10 +25,14 @@ class ExceptionTextFormatter extends Formatter
/** @var \Exception $exception */ /** @var \Exception $exception */
$exception = $record['context']['exception']; $exception = $record['context']['exception'];
$record['message'] = sprintf("%s(%d): %s\n%s", $exception->getFile(), $exception->getLine(), $exception->getMessage(), $record['message'] = sprintf(
Log::$debugBacktraceForTests ?: $exception->getTraceAsString()); "%s(%d): %s\n%s",
$exception->getFile(),
$exception->getLine(),
$this->getMessage($exception),
$this->getStackTrace($exception)
);
// Remove the exception so that it's not formatted again by another formatter
unset($record['context']['exception']); unset($record['context']['exception']);
return $this->next($record); return $this->next($record);
...@@ -39,4 +43,18 @@ class ExceptionTextFormatter extends Formatter ...@@ -39,4 +43,18 @@ class ExceptionTextFormatter extends Formatter
return isset($record['context']['exception']) return isset($record['context']['exception'])
&& $record['context']['exception'] instanceof \Exception; && $record['context']['exception'] instanceof \Exception;
} }
private function getMessage(\Exception $exception)
{
if ($exception instanceof \ErrorException) {
return Error::getErrNoString($exception->getSeverity()) . ' - ' . $exception->getMessage();
}
return $exception->getMessage();
}
private function getStackTrace(\Exception $exception)
{
return Log::$debugBacktraceForTests ?: $exception->getTraceAsString();
}
} }
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter