mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-20 16:16:37 +02:00
Merge pull request #22 from stof/formatting_refactoring
Refactoring of FormatterInterface format() must now return the formatted message and not the complete record.
This commit is contained in:
@@ -22,7 +22,15 @@ interface FormatterInterface
|
||||
* Formats a log record.
|
||||
*
|
||||
* @param array $record A record to format
|
||||
* @return array The record with a formatted message
|
||||
* @return string The formatted message
|
||||
*/
|
||||
function format(array $record);
|
||||
|
||||
/**
|
||||
* Formats a set of log records.
|
||||
*
|
||||
* @param array $record A record to format
|
||||
* @return string The formatted batch message
|
||||
*/
|
||||
function formatBatch(array $records);
|
||||
}
|
||||
|
@@ -27,8 +27,14 @@ class JsonFormatter implements FormatterInterface
|
||||
*/
|
||||
public function format(array $record)
|
||||
{
|
||||
$record['message'] = json_encode($record);
|
||||
return json_encode($record);
|
||||
}
|
||||
|
||||
return $record;
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function formatBatch(array $records)
|
||||
{
|
||||
return json_encode($records);
|
||||
}
|
||||
}
|
||||
|
@@ -62,8 +62,17 @@ class LineFormatter implements FormatterInterface
|
||||
foreach ($vars['extra'] as $var => $val) {
|
||||
$output = str_replace('%extra.'.$var.'%', $val, $output);
|
||||
}
|
||||
$record['message'] = $output;
|
||||
|
||||
return $record;
|
||||
return $output;
|
||||
}
|
||||
|
||||
public function formatBatch(array $records)
|
||||
{
|
||||
$message = '';
|
||||
foreach ($records as $record) {
|
||||
$message .= $this->format($record);
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
|
@@ -18,7 +18,7 @@ use Monolog\Logger;
|
||||
*
|
||||
* @author Eric Clemmons (@ericclemmons) <eric@uxdriven.com>
|
||||
*/
|
||||
class WildfireFormatter extends LineFormatter implements FormatterInterface
|
||||
class WildfireFormatter extends LineFormatter
|
||||
{
|
||||
/**
|
||||
* Similar to LineFormatter::SIMPLE_FORMAT, except without the "[%datetime%]"
|
||||
@@ -43,7 +43,7 @@ class WildfireFormatter extends LineFormatter implements FormatterInterface
|
||||
public function format(array $record)
|
||||
{
|
||||
// Format record according with LineFormatter
|
||||
$formatted = parent::format($record);
|
||||
$message = parent::format($record);
|
||||
|
||||
// Create JSON object describing the appearance of the message in the console
|
||||
$json = json_encode(array(
|
||||
@@ -52,17 +52,19 @@ class WildfireFormatter extends LineFormatter implements FormatterInterface
|
||||
'File' => '',
|
||||
'Line' => '',
|
||||
),
|
||||
$formatted['message'],
|
||||
$message,
|
||||
));
|
||||
|
||||
// The message itself is a serialization of the above JSON object + it's length
|
||||
$formatted['message'] = sprintf(
|
||||
return sprintf(
|
||||
'%s|%s|',
|
||||
strlen($json),
|
||||
$json
|
||||
);
|
||||
|
||||
return $formatted;
|
||||
}
|
||||
|
||||
public function formatBatch(array $records)
|
||||
{
|
||||
throw new \BadMethodCallException('Batch formatting does not make sense for the WildfireFormatter');
|
||||
}
|
||||
}
|
@@ -27,6 +27,9 @@ abstract class AbstractHandler implements HandlerInterface
|
||||
protected $level = Logger::DEBUG;
|
||||
protected $bubble = false;
|
||||
|
||||
/**
|
||||
* @var FormatterInterface
|
||||
*/
|
||||
protected $formatter;
|
||||
protected $processors = array();
|
||||
|
||||
@@ -57,16 +60,12 @@ abstract class AbstractHandler implements HandlerInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->processors) {
|
||||
foreach ($this->processors as $processor) {
|
||||
$record = call_user_func($processor, $record);
|
||||
}
|
||||
}
|
||||
$record = $this->processRecord($record);
|
||||
|
||||
if (!$this->formatter) {
|
||||
$this->formatter = $this->getDefaultFormatter();
|
||||
}
|
||||
$record = $this->formatter->format($record);
|
||||
$record['message'] = $this->formatter->format($record);
|
||||
|
||||
$this->write($record);
|
||||
|
||||
@@ -188,4 +187,21 @@ abstract class AbstractHandler implements HandlerInterface
|
||||
{
|
||||
return new LineFormatter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a record.
|
||||
*
|
||||
* @param array $record
|
||||
* @return array
|
||||
*/
|
||||
protected function processRecord(array $record)
|
||||
{
|
||||
if ($this->processors) {
|
||||
foreach ($this->processors as $processor) {
|
||||
$record = call_user_func($processor, $record);
|
||||
}
|
||||
}
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user