From a8d4fc4a34c6abc9928735d56aa5653b5d0834d7 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Tue, 31 May 2011 16:38:48 +0200 Subject: [PATCH 1/4] Tweaked the formatting of complex data in the LineFormatter --- src/Monolog/Formatter/FormatterInterface.php | 6 +-- src/Monolog/Formatter/LineFormatter.php | 42 +++++++++++++------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/Monolog/Formatter/FormatterInterface.php b/src/Monolog/Formatter/FormatterInterface.php index f3222638..77891de8 100644 --- a/src/Monolog/Formatter/FormatterInterface.php +++ b/src/Monolog/Formatter/FormatterInterface.php @@ -22,15 +22,15 @@ interface FormatterInterface * Formats a log record. * * @param array $record A record to format - * @return string The formatted message + * @return mixed The formatted record */ function format(array $record); /** * Formats a set of log records. * - * @param array $record A record to format - * @return string The formatted batch message + * @param array $records A set of records to format + * @return mixed The formatted set of records */ function formatBatch(array $records); } diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index 7c6c3dcd..63fc119c 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -19,6 +19,7 @@ use Monolog\Logger; * This is especially useful for logging to files * * @author Jordi Boggiano + * @author Christophe Coevoet */ class LineFormatter implements FormatterInterface { @@ -47,20 +48,12 @@ class LineFormatter implements FormatterInterface $vars['datetime'] = $vars['datetime']->format($this->dateFormat); $output = $this->format; - foreach ($vars as $var => $val) { - if (is_array($val)) { - $strval = array(); - foreach ($val as $subvar => $subval) { - $strval[] = $subvar.': '.$this->convertToString($subval); - } - $replacement = $strval ? $var.'('.implode(', ', $strval).')' : ''; - $output = str_replace('%'.$var.'%', $replacement, $output); - } else { - $output = str_replace('%'.$var.'%', $this->convertToString($val), $output); - } - } foreach ($vars['extra'] as $var => $val) { $output = str_replace('%extra.'.$var.'%', $this->convertToString($val), $output); + unset($vars['extra'][$var]); + } + foreach ($vars as $var => $val) { + $output = str_replace('%'.$var.'%', $this->convertToString($val), $output); } return $output; @@ -76,12 +69,31 @@ class LineFormatter implements FormatterInterface return $message; } - private function convertToString($data) + protected function convertToString($data) { - if (is_scalar($data) || (is_object($data) && method_exists($data, '__toString'))) { + if (null === $data || is_scalar($data)) { return (string) $data; } - return serialize($data); + return json_encode($this->normalize($data)); + } + + protected function normalize($data) + { + if (null === $data || is_scalar($data)) { + return $data; + } + + if (is_array($data) || $data instanceof \Traversable) { + $normalized = array(); + + foreach ($data as $key => $value) { + $normalized[$key] = $this->normalize($value); + } + + return $normalized; + } + + return sprintf("[object] (%s: %s)", get_class($data), json_decode($data)); } } From c1675c59d8dbf50e16fa776917030eb03efa6206 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Wed, 1 Jun 2011 00:10:38 +0200 Subject: [PATCH 2/4] Added the support of a logging context --- src/Monolog/Formatter/LineFormatter.php | 12 +++-- src/Monolog/Formatter/WildfireFormatter.php | 2 +- .../Handler/AbstractProcessingHandler.php | 2 +- src/Monolog/Handler/FirePHPHandler.php | 2 +- src/Monolog/Handler/MailHandler.php | 6 +-- src/Monolog/Handler/StreamHandler.php | 2 +- src/Monolog/Handler/SyslogHandler.php | 2 +- src/Monolog/Logger.php | 48 ++++++++++++------- tests/Monolog/Formatter/JsonFormatterTest.php | 1 + tests/Monolog/Formatter/LineFormatterTest.php | 20 +++++--- .../Formatter/WildfireFormatterTest.php | 24 +++------- tests/Monolog/Handler/FirePHPHandlerTest.php | 12 ++--- tests/Monolog/TestCase.php | 1 + 13 files changed, 74 insertions(+), 60 deletions(-) diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index 63fc119c..69f04f98 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -23,7 +23,7 @@ use Monolog\Logger; */ class LineFormatter implements FormatterInterface { - const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %extra%\n"; + const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"; const SIMPLE_DATE = "Y-m-d H:i:s"; protected $format; @@ -49,8 +49,10 @@ class LineFormatter implements FormatterInterface $output = $this->format; foreach ($vars['extra'] as $var => $val) { - $output = str_replace('%extra.'.$var.'%', $this->convertToString($val), $output); - unset($vars['extra'][$var]); + if (false !== strpos($output, '%extra.'.$var.'%')) { + $output = str_replace('%extra.'.$var.'%', $this->convertToString($val), $output); + unset($vars['extra'][$var]); + } } foreach ($vars as $var => $val) { $output = str_replace('%'.$var.'%', $this->convertToString($val), $output); @@ -75,7 +77,7 @@ class LineFormatter implements FormatterInterface return (string) $data; } - return json_encode($this->normalize($data)); + return stripslashes(json_encode($this->normalize($data))); } protected function normalize($data) @@ -94,6 +96,6 @@ class LineFormatter implements FormatterInterface return $normalized; } - return sprintf("[object] (%s: %s)", get_class($data), json_decode($data)); + return sprintf("[object] (%s: %s)", get_class($data), json_encode($data)); } } diff --git a/src/Monolog/Formatter/WildfireFormatter.php b/src/Monolog/Formatter/WildfireFormatter.php index 1f3b6647..100db44a 100644 --- a/src/Monolog/Formatter/WildfireFormatter.php +++ b/src/Monolog/Formatter/WildfireFormatter.php @@ -23,7 +23,7 @@ class WildfireFormatter extends LineFormatter /** * Similar to LineFormatter::SIMPLE_FORMAT, except without the "[%datetime%]" */ - const SIMPLE_FORMAT = "%channel%: %message% %extra%"; + const SIMPLE_FORMAT = "%channel%: %message% %context% %extra%"; /** * Translates Monolog log levels to Wildfire levels. diff --git a/src/Monolog/Handler/AbstractProcessingHandler.php b/src/Monolog/Handler/AbstractProcessingHandler.php index d3344480..7f0d6dc9 100644 --- a/src/Monolog/Handler/AbstractProcessingHandler.php +++ b/src/Monolog/Handler/AbstractProcessingHandler.php @@ -36,7 +36,7 @@ abstract class AbstractProcessingHandler extends AbstractHandler $record = $this->processRecord($record); - $record['message'] = $this->getFormatter()->format($record); + $record['formatted'] = $this->getFormatter()->format($record); $this->write($record); diff --git a/src/Monolog/Handler/FirePHPHandler.php b/src/Monolog/Handler/FirePHPHandler.php index fe9d4ff3..e1042bd9 100644 --- a/src/Monolog/Handler/FirePHPHandler.php +++ b/src/Monolog/Handler/FirePHPHandler.php @@ -79,7 +79,7 @@ class FirePHPHandler extends AbstractProcessingHandler // but we're not taking advantage of that (yet), so we're using "1" for simplicity's sake. return $this->createHeader( array(1, 1, 1, self::$messageIndex++), - $record['message'] + $record['formatted'] ); } diff --git a/src/Monolog/Handler/MailHandler.php b/src/Monolog/Handler/MailHandler.php index c9a9e0e5..9e9ab563 100644 --- a/src/Monolog/Handler/MailHandler.php +++ b/src/Monolog/Handler/MailHandler.php @@ -33,7 +33,7 @@ abstract class MailHandler extends AbstractProcessingHandler } if (!empty($messages)) { - $this->send($this->getFormatter()->formatBatch($messages)); + $this->send((string) $this->getFormatter()->formatBatch($messages)); } } @@ -49,8 +49,6 @@ abstract class MailHandler extends AbstractProcessingHandler */ protected function write(array $record) { - $content = $record['message']; - - $this->send($content); + $this->send((string) $record['formatted']); } } \ No newline at end of file diff --git a/src/Monolog/Handler/StreamHandler.php b/src/Monolog/Handler/StreamHandler.php index b631d080..3268ae48 100644 --- a/src/Monolog/Handler/StreamHandler.php +++ b/src/Monolog/Handler/StreamHandler.php @@ -67,6 +67,6 @@ class StreamHandler extends AbstractProcessingHandler throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened; it may be invalid or not writable.', $this->url)); } } - fwrite($this->stream, (string) $record['message']); + fwrite($this->stream, (string) $record['formatted']); } } diff --git a/src/Monolog/Handler/SyslogHandler.php b/src/Monolog/Handler/SyslogHandler.php index dee2eccd..ebdfe5c2 100644 --- a/src/Monolog/Handler/SyslogHandler.php +++ b/src/Monolog/Handler/SyslogHandler.php @@ -104,6 +104,6 @@ class SyslogHandler extends AbstractProcessingHandler */ protected function write(array $record) { - syslog($this->logLevels[$record['level']], (string) $record['message']); + syslog($this->logLevels[$record['level']], (string) $record['formatted']); } } diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 49d1dfd4..7c8525ac 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -137,15 +137,17 @@ class Logger * * @param integer $level The logging level * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function addRecord($level, $message) + public function addRecord($level, $message, array $context = array()) { if (!$this->handlers) { $this->pushHandler(new StreamHandler('php://stderr', self::DEBUG)); } $record = array( - 'message' => $message, + 'message' => (string) $message, + 'context' => $context, 'level' => $level, 'level_name' => self::getLevelName($level), 'channel' => $this->name, @@ -180,9 +182,10 @@ class Logger * Adds a log record at the DEBUG level. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function addDebug($message) + public function addDebug($message, array $context = array()) { return $this->addRecord(self::DEBUG, $message); } @@ -191,9 +194,10 @@ class Logger * Adds a log record at the INFO level. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function addInfo($message) + public function addInfo($message, array $context = array()) { return $this->addRecord(self::INFO, $message); } @@ -202,9 +206,10 @@ class Logger * Adds a log record at the WARNING level. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function addWarning($message) + public function addWarning($message, array $context = array()) { return $this->addRecord(self::WARNING, $message); } @@ -213,9 +218,10 @@ class Logger * Adds a log record at the ERROR level. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function addError($message) + public function addError($message, array $context = array()) { return $this->addRecord(self::ERROR, $message); } @@ -224,9 +230,10 @@ class Logger * Adds a log record at the CRITICAL level. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function addCritical($message) + public function addCritical($message, array $context = array()) { return $this->addRecord(self::CRITICAL, $message); } @@ -235,9 +242,10 @@ class Logger * Adds a log record at the ALERT level. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function addAlert($message) + public function addAlert($message, array $context = array()) { return $this->addRecord(self::ALERT, $message); } @@ -261,9 +269,10 @@ class Logger * This method allows to have an easy ZF compatibility. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function debug($message) + public function debug($message, array $context = array()) { return $this->addRecord(self::DEBUG, $message); } @@ -274,9 +283,10 @@ class Logger * This method allows to have an easy ZF compatibility. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function info($message) + public function info($message, array $context = array()) { return $this->addRecord(self::INFO, $message); } @@ -287,9 +297,10 @@ class Logger * This method allows to have an easy ZF compatibility. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function notice($message) + public function notice($message, array $context = array()) { return $this->addRecord(self::INFO, $message); } @@ -300,9 +311,10 @@ class Logger * This method allows to have an easy ZF compatibility. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function warn($message) + public function warn($message, array $context = array()) { return $this->addRecord(self::WARNING, $message); } @@ -313,9 +325,10 @@ class Logger * This method allows to have an easy ZF compatibility. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function err($message) + public function err($message, array $context = array()) { return $this->addRecord(self::ERROR, $message); } @@ -326,9 +339,10 @@ class Logger * This method allows to have an easy ZF compatibility. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function crit($message) + public function crit($message, array $context = array()) { return $this->addRecord(self::CRITICAL, $message); } @@ -339,9 +353,10 @@ class Logger * This method allows to have an easy ZF compatibility. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function alert($message) + public function alert($message, array $context = array()) { return $this->addRecord(self::ALERT, $message); } @@ -352,9 +367,10 @@ class Logger * This method allows to have an easy ZF compatibility. * * @param string $message The log message + * @param array $context The log context * @return Boolean Whether the record has been processed */ - public function emerg($message) + public function emerg($message, array $context = array()) { return $this->addRecord(self::ALERT, $message); } diff --git a/tests/Monolog/Formatter/JsonFormatterTest.php b/tests/Monolog/Formatter/JsonFormatterTest.php index ae0e29e2..2e26811a 100644 --- a/tests/Monolog/Formatter/JsonFormatterTest.php +++ b/tests/Monolog/Formatter/JsonFormatterTest.php @@ -21,6 +21,7 @@ class JsonFormatterTest extends \PHPUnit_Framework_TestCase $message = $formatter->format($msg = array( 'level_name' => 'WARNING', 'channel' => 'log', + 'context' => array(), 'message' => array('foo'), 'datetime' => new \DateTime, 'extra' => array(), diff --git a/tests/Monolog/Formatter/LineFormatterTest.php b/tests/Monolog/Formatter/LineFormatterTest.php index a33b1cd8..01f7ac6a 100644 --- a/tests/Monolog/Formatter/LineFormatterTest.php +++ b/tests/Monolog/Formatter/LineFormatterTest.php @@ -21,27 +21,29 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase $message = $formatter->format(array( 'level_name' => 'WARNING', 'channel' => 'log', + 'context' => array(), 'message' => 'foo', 'datetime' => new \DateTime, 'extra' => array(), )); - $this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo '."\n", $message); + $this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message); } - public function testDefFormatWithArray() + public function testDefFormatWithArrayContext() { $formatter = new LineFormatter(null, 'Y-m-d'); $message = $formatter->format(array( 'level_name' => 'ERROR', 'channel' => 'meh', + 'message' => 'foo', 'datetime' => new \DateTime, 'extra' => array(), - 'message' => array( + 'context' => array( 'foo' => 'bar', 'baz' => 'qux', ) )); - $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: message(foo: bar, baz: qux) '."\n", $message); + $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foo {"foo":"bar","baz":"qux"} []'."\n", $message); } public function testDefFormatExtras() @@ -50,11 +52,12 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase $message = $formatter->format(array( 'level_name' => 'ERROR', 'channel' => 'meh', + 'context' => array(), 'datetime' => new \DateTime, 'extra' => array('ip' => '127.0.0.1'), 'message' => 'log', )); - $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log extra(ip: 127.0.0.1)'."\n", $message); + $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] {"ip":"127.0.0.1"}'."\n", $message); } public function testDefFormatWithObject() @@ -63,11 +66,12 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase $message = $formatter->format(array( 'level_name' => 'ERROR', 'channel' => 'meh', + 'context' => array(), 'datetime' => new \DateTime, 'extra' => array('foo' => new TestFoo, 'bar' => new TestBar, 'baz' => array()), 'message' => 'foobar', )); - $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar extra(foo: O:25:"Monolog\\Formatter\\TestFoo":1:{s:3:"foo";s:3:"foo";}, bar: bar, baz: a:0:{})'."\n", $message); + $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":"[object] (Monolog\\Formatter\\TestFoo: {"foo":"foo"})","bar":"[object] (Monolog\\Formatter\\TestBar: {})","baz":[]}'."\n", $message); } public function testBatchFormat() @@ -78,6 +82,7 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase 'level_name' => 'CRITICAL', 'channel' => 'test', 'message' => 'bar', + 'context' => array(), 'datetime' => new \DateTime, 'extra' => array(), ), @@ -85,11 +90,12 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase 'level_name' => 'WARNING', 'channel' => 'log', 'message' => 'foo', + 'context' => array(), 'datetime' => new \DateTime, 'extra' => array(), ), )); - $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar '."\n".'['.date('Y-m-d').'] log.WARNING: foo '."\n", $message); + $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message); } } diff --git a/tests/Monolog/Formatter/WildfireFormatterTest.php b/tests/Monolog/Formatter/WildfireFormatterTest.php index 06bcea3c..aabd3248 100644 --- a/tests/Monolog/Formatter/WildfireFormatterTest.php +++ b/tests/Monolog/Formatter/WildfireFormatterTest.php @@ -15,34 +15,24 @@ use Monolog\Logger; class WildfireFormatterTest extends \PHPUnit_Framework_TestCase { - /** - * @dataProvider recordProvider - */ - public function testDefaultFormatIsLineFormatterWithoutNewLine($record) + public function testDefaultFormatIsLineFormatterWithoutNewLine() { $wildfire = new WildfireFormatter(); - - $message = $wildfire->format($record); - - $this->assertEquals( - '70|[{"Type":"ERROR","File":"","Line":""},"meh: log extra(ip: 127.0.0.1)"]|', - $message - ); - } - - public function recordProvider() - { $record = array( 'level' => Logger::ERROR, 'level_name' => 'ERROR', 'channel' => 'meh', + 'context' => array(), 'datetime' => new \DateTime, 'extra' => array('ip' => '127.0.0.1'), 'message' => 'log', ); - return array( - array($record), + $message = $wildfire->format($record); + + $this->assertEquals( + '75|[{"Type":"ERROR","File":"","Line":""},"meh: log [] {\\"ip\\":\\"127.0.0.1\\"}"]|', + $message ); } } diff --git a/tests/Monolog/Handler/FirePHPHandlerTest.php b/tests/Monolog/Handler/FirePHPHandlerTest.php index 309a8f45..1da9279e 100644 --- a/tests/Monolog/Handler/FirePHPHandlerTest.php +++ b/tests/Monolog/Handler/FirePHPHandlerTest.php @@ -31,8 +31,8 @@ class FirePHPHandlerTest extends TestCase 'X-Wf-Protocol-1' => 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2', 'X-Wf-1-Structure-1' => 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1', 'X-Wf-1-Plugin-1' => 'http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3', - 'X-Wf-1-1-1-1' => '50|[{"Type":"LOG","File":"","Line":""},"test: test "]|', - 'X-Wf-1-1-1-2' => '51|[{"Type":"WARN","File":"","Line":""},"test: test "]|', + 'X-Wf-1-1-1-1' => '55|[{"Type":"LOG","File":"","Line":""},"test: test [] []"]|', + 'X-Wf-1-1-1-2' => '56|[{"Type":"WARN","File":"","Line":""},"test: test [] []"]|', ); $this->assertEquals($expected, $handler->getHeaders()); @@ -52,13 +52,13 @@ class FirePHPHandlerTest extends TestCase 'X-Wf-Protocol-1' => 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2', 'X-Wf-1-Structure-1' => 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1', 'X-Wf-1-Plugin-1' => 'http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3', - 'X-Wf-1-1-1-1' => '50|[{"Type":"LOG","File":"","Line":""},"test: test "]|', - 'X-Wf-1-1-1-2' => '51|[{"Type":"WARN","File":"","Line":""},"test: test "]|', + 'X-Wf-1-1-1-1' => '55|[{"Type":"LOG","File":"","Line":""},"test: test [] []"]|', + 'X-Wf-1-1-1-2' => '56|[{"Type":"WARN","File":"","Line":""},"test: test [] []"]|', ); $expected2 = array( - 'X-Wf-1-1-1-3' => '50|[{"Type":"LOG","File":"","Line":""},"test: test "]|', - 'X-Wf-1-1-1-4' => '51|[{"Type":"WARN","File":"","Line":""},"test: test "]|', + 'X-Wf-1-1-1-3' => '55|[{"Type":"LOG","File":"","Line":""},"test: test [] []"]|', + 'X-Wf-1-1-1-4' => '56|[{"Type":"WARN","File":"","Line":""},"test: test [] []"]|', ); $this->assertEquals($expected, $handler->getHeaders()); diff --git a/tests/Monolog/TestCase.php b/tests/Monolog/TestCase.php index 8f4b1caf..ecf655c2 100644 --- a/tests/Monolog/TestCase.php +++ b/tests/Monolog/TestCase.php @@ -20,6 +20,7 @@ class TestCase extends \PHPUnit_Framework_TestCase { return array( 'message' => $message, + 'context' => array(), 'level' => $level, 'level_name' => Logger::getLevelName($level), 'channel' => 'test', From e9f3e0c2d70ea39a2883599561f4c9160690f78f Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Wed, 1 Jun 2011 00:43:09 +0200 Subject: [PATCH 3/4] Moved the channel as label on the FirePHP message for a better formatting --- src/Monolog/Formatter/WildfireFormatter.php | 3 ++- tests/Monolog/Formatter/WildfireFormatterTest.php | 2 +- tests/Monolog/Handler/FirePHPHandlerTest.php | 12 ++++++------ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Monolog/Formatter/WildfireFormatter.php b/src/Monolog/Formatter/WildfireFormatter.php index 100db44a..85f57949 100644 --- a/src/Monolog/Formatter/WildfireFormatter.php +++ b/src/Monolog/Formatter/WildfireFormatter.php @@ -23,7 +23,7 @@ class WildfireFormatter extends LineFormatter /** * Similar to LineFormatter::SIMPLE_FORMAT, except without the "[%datetime%]" */ - const SIMPLE_FORMAT = "%channel%: %message% %context% %extra%"; + const SIMPLE_FORMAT = "%message% %context% %extra%"; /** * Translates Monolog log levels to Wildfire levels. @@ -51,6 +51,7 @@ class WildfireFormatter extends LineFormatter 'Type' => $this->logLevels[$record['level']], 'File' => '', 'Line' => '', + 'Label' => $record['channel'], ), $message, )); diff --git a/tests/Monolog/Formatter/WildfireFormatterTest.php b/tests/Monolog/Formatter/WildfireFormatterTest.php index aabd3248..9a0309ea 100644 --- a/tests/Monolog/Formatter/WildfireFormatterTest.php +++ b/tests/Monolog/Formatter/WildfireFormatterTest.php @@ -31,7 +31,7 @@ class WildfireFormatterTest extends \PHPUnit_Framework_TestCase $message = $wildfire->format($record); $this->assertEquals( - '75|[{"Type":"ERROR","File":"","Line":""},"meh: log [] {\\"ip\\":\\"127.0.0.1\\"}"]|', + '84|[{"Type":"ERROR","File":"","Line":"","Label":"meh"},"log [] {\\"ip\\":\\"127.0.0.1\\"}"]|', $message ); } diff --git a/tests/Monolog/Handler/FirePHPHandlerTest.php b/tests/Monolog/Handler/FirePHPHandlerTest.php index 1da9279e..dca97497 100644 --- a/tests/Monolog/Handler/FirePHPHandlerTest.php +++ b/tests/Monolog/Handler/FirePHPHandlerTest.php @@ -31,8 +31,8 @@ class FirePHPHandlerTest extends TestCase 'X-Wf-Protocol-1' => 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2', 'X-Wf-1-Structure-1' => 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1', 'X-Wf-1-Plugin-1' => 'http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3', - 'X-Wf-1-1-1-1' => '55|[{"Type":"LOG","File":"","Line":""},"test: test [] []"]|', - 'X-Wf-1-1-1-2' => '56|[{"Type":"WARN","File":"","Line":""},"test: test [] []"]|', + 'X-Wf-1-1-1-1' => '64|[{"Type":"LOG","File":"","Line":"","Label":"test"},"test [] []"]|', + 'X-Wf-1-1-1-2' => '65|[{"Type":"WARN","File":"","Line":"","Label":"test"},"test [] []"]|', ); $this->assertEquals($expected, $handler->getHeaders()); @@ -52,13 +52,13 @@ class FirePHPHandlerTest extends TestCase 'X-Wf-Protocol-1' => 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2', 'X-Wf-1-Structure-1' => 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1', 'X-Wf-1-Plugin-1' => 'http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3', - 'X-Wf-1-1-1-1' => '55|[{"Type":"LOG","File":"","Line":""},"test: test [] []"]|', - 'X-Wf-1-1-1-2' => '56|[{"Type":"WARN","File":"","Line":""},"test: test [] []"]|', + 'X-Wf-1-1-1-1' => '64|[{"Type":"LOG","File":"","Line":"","Label":"test"},"test [] []"]|', + 'X-Wf-1-1-1-2' => '65|[{"Type":"WARN","File":"","Line":"","Label":"test"},"test [] []"]|', ); $expected2 = array( - 'X-Wf-1-1-1-3' => '55|[{"Type":"LOG","File":"","Line":""},"test: test [] []"]|', - 'X-Wf-1-1-1-4' => '56|[{"Type":"WARN","File":"","Line":""},"test: test [] []"]|', + 'X-Wf-1-1-1-3' => '64|[{"Type":"LOG","File":"","Line":"","Label":"test"},"test [] []"]|', + 'X-Wf-1-1-1-4' => '65|[{"Type":"WARN","File":"","Line":"","Label":"test"},"test [] []"]|', ); $this->assertEquals($expected, $handler->getHeaders()); From 0d728feb7c53eb4151c8c2228e81fe59cd859d6c Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Wed, 1 Jun 2011 01:05:23 +0200 Subject: [PATCH 4/4] Fixed forwarding of the context in logging calls --- src/Monolog/Logger.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 7c8525ac..fd8ee8bd 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -187,7 +187,7 @@ class Logger */ public function addDebug($message, array $context = array()) { - return $this->addRecord(self::DEBUG, $message); + return $this->addRecord(self::DEBUG, $message, $context); } /** @@ -199,7 +199,7 @@ class Logger */ public function addInfo($message, array $context = array()) { - return $this->addRecord(self::INFO, $message); + return $this->addRecord(self::INFO, $message, $context); } /** @@ -211,7 +211,7 @@ class Logger */ public function addWarning($message, array $context = array()) { - return $this->addRecord(self::WARNING, $message); + return $this->addRecord(self::WARNING, $message, $context); } /** @@ -223,7 +223,7 @@ class Logger */ public function addError($message, array $context = array()) { - return $this->addRecord(self::ERROR, $message); + return $this->addRecord(self::ERROR, $message, $context); } /** @@ -235,7 +235,7 @@ class Logger */ public function addCritical($message, array $context = array()) { - return $this->addRecord(self::CRITICAL, $message); + return $this->addRecord(self::CRITICAL, $message, $context); } /** @@ -247,7 +247,7 @@ class Logger */ public function addAlert($message, array $context = array()) { - return $this->addRecord(self::ALERT, $message); + return $this->addRecord(self::ALERT, $message, $context); } /** @@ -274,7 +274,7 @@ class Logger */ public function debug($message, array $context = array()) { - return $this->addRecord(self::DEBUG, $message); + return $this->addRecord(self::DEBUG, $message, $context); } /** @@ -288,7 +288,7 @@ class Logger */ public function info($message, array $context = array()) { - return $this->addRecord(self::INFO, $message); + return $this->addRecord(self::INFO, $message, $context); } /** @@ -302,7 +302,7 @@ class Logger */ public function notice($message, array $context = array()) { - return $this->addRecord(self::INFO, $message); + return $this->addRecord(self::INFO, $message, $context); } /** @@ -316,7 +316,7 @@ class Logger */ public function warn($message, array $context = array()) { - return $this->addRecord(self::WARNING, $message); + return $this->addRecord(self::WARNING, $message, $context); } /** @@ -330,7 +330,7 @@ class Logger */ public function err($message, array $context = array()) { - return $this->addRecord(self::ERROR, $message); + return $this->addRecord(self::ERROR, $message, $context); } /** @@ -344,7 +344,7 @@ class Logger */ public function crit($message, array $context = array()) { - return $this->addRecord(self::CRITICAL, $message); + return $this->addRecord(self::CRITICAL, $message, $context); } /** @@ -358,7 +358,7 @@ class Logger */ public function alert($message, array $context = array()) { - return $this->addRecord(self::ALERT, $message); + return $this->addRecord(self::ALERT, $message, $context); } /** @@ -372,6 +372,6 @@ class Logger */ public function emerg($message, array $context = array()) { - return $this->addRecord(self::ALERT, $message); + return $this->addRecord(self::ALERT, $message, $context); } }