1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-04 12:17:35 +02:00

Fix boolean to string convertation. Fix double normalization.

Details bug:
$context = ["false", false]; // formatted as {"0":"false","1":"false"} when need {"0":"false","1":false}
This commit is contained in:
Sergey Rabochiy
2013-11-29 16:09:42 +07:00
parent 6f5b3d2a9c
commit e012bf1b63
2 changed files with 17 additions and 18 deletions

View File

@@ -11,6 +11,8 @@
namespace Monolog\Formatter; namespace Monolog\Formatter;
use Exception;
/** /**
* Formats incoming records into a one-line string * Formats incoming records into a one-line string
* *
@@ -68,33 +70,28 @@ class LineFormatter extends NormalizerFormatter
return $message; return $message;
} }
protected function normalize($data) protected function normalizeException(Exception $e)
{ {
if (is_bool($data) || is_null($data)) {
return var_export($data, true);
}
if ($data instanceof \Exception) {
$previousText = ''; $previousText = '';
if ($previous = $data->getPrevious()) { if ($previous = $e->getPrevious()) {
do { do {
$previousText .= ', '.get_class($previous).': '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine(); $previousText .= ', '.get_class($previous).': '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine();
} while ($previous = $previous->getPrevious()); } while ($previous = $previous->getPrevious());
} }
return '[object] ('.get_class($data).': '.$data->getMessage().' at '.$data->getFile().':'.$data->getLine().$previousText.')'; return '[object] ('.get_class($e).': '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')';
}
return parent::normalize($data);
} }
protected function convertToString($data) protected function convertToString($data)
{ {
if (null === $data || is_scalar($data)) { if (null === $data|| is_bool($data)) {
return var_export($data, true);
}
if (is_scalar($data)) {
return (string) $data; return (string) $data;
} }
$data = $this->normalize($data);
if (version_compare(PHP_VERSION, '5.4.0', '>=')) { if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
return $this->toJson($data, true); return $this->toJson($data, true);
} }

View File

@@ -42,9 +42,11 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
'context' => array( 'context' => array(
'foo' => 'bar', 'foo' => 'bar',
'baz' => 'qux', 'baz' => 'qux',
'bool' => false,
'null' => null,
) )
)); ));
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foo {"foo":"bar","baz":"qux"} []'."\n", $message); $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foo {"foo":"bar","baz":"qux","bool":false,"null":null} []'."\n", $message);
} }
public function testDefFormatExtras() public function testDefFormatExtras()