summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChristophe Coevoet <stof@notk.org>2011-06-01 00:10:38 +0200
committerChristophe Coevoet <stof@notk.org>2011-06-01 00:10:38 +0200
commitc1675c59d8dbf50e16fa776917030eb03efa6206 (patch)
tree543030f90425adaf723e4433ef2aba203759c79e /src
parenta8d4fc4a34c6abc9928735d56aa5653b5d0834d7 (diff)
downloadmonolog-c1675c59d8dbf50e16fa776917030eb03efa6206.zip
monolog-c1675c59d8dbf50e16fa776917030eb03efa6206.tar.gz
monolog-c1675c59d8dbf50e16fa776917030eb03efa6206.tar.bz2
Added the support of a logging context
Diffstat (limited to 'src')
-rw-r--r--src/Monolog/Formatter/LineFormatter.php12
-rw-r--r--src/Monolog/Formatter/WildfireFormatter.php2
-rw-r--r--src/Monolog/Handler/AbstractProcessingHandler.php2
-rw-r--r--src/Monolog/Handler/FirePHPHandler.php2
-rw-r--r--src/Monolog/Handler/MailHandler.php6
-rw-r--r--src/Monolog/Handler/StreamHandler.php2
-rw-r--r--src/Monolog/Handler/SyslogHandler.php2
-rw-r--r--src/Monolog/Logger.php48
8 files changed, 46 insertions, 30 deletions
diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php
index 63fc119..69f04f9 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 1f3b664..100db44 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 d334448..7f0d6dc 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 fe9d4ff..e1042bd 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 c9a9e0e..9e9ab56 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 b631d08..3268ae4 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 dee2ecc..ebdfe5c 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 49d1dfd..7c8525a 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);
}