4 * This file is part of the Monolog package.
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Monolog\Formatter;
15 * Formats incoming records into a one-line string
17 * This is especially useful for logging to files
19 * @author Jordi Boggiano <j.boggiano@seld.be>
20 * @author Christophe Coevoet <stof@notk.org>
22 class LineFormatter extends NormalizerFormatter
24 const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
27 protected $allowInlineLineBreaks;
28 protected $ignoreEmptyContextAndExtra;
29 protected $includeStacktraces;
32 * @param string $format The format of the message
33 * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
34 * @param bool $allowInlineLineBreaks Whether to allow inline line breaks in log entries
35 * @param bool $ignoreEmptyContextAndExtra
37 public function __construct($format = null, $dateFormat = null, $allowInlineLineBreaks = false, $ignoreEmptyContextAndExtra = false)
39 $this->format = $format ?: static::SIMPLE_FORMAT;
40 $this->allowInlineLineBreaks = $allowInlineLineBreaks;
41 $this->ignoreEmptyContextAndExtra = $ignoreEmptyContextAndExtra;
42 parent::__construct($dateFormat);
45 public function includeStacktraces($include = true)
47 $this->includeStacktraces = $include;
48 if ($this->includeStacktraces) {
49 $this->allowInlineLineBreaks = true;
53 public function allowInlineLineBreaks($allow = true)
55 $this->allowInlineLineBreaks = $allow;
58 public function ignoreEmptyContextAndExtra($ignore = true)
60 $this->ignoreEmptyContextAndExtra = $ignore;
66 public function format(array $record)
68 $vars = parent::format($record);
70 $output = $this->format;
72 foreach ($vars['extra'] as $var => $val) {
73 if (false !== strpos($output, '%extra.'.$var.'%')) {
74 $output = str_replace('%extra.'.$var.'%', $this->stringify($val), $output);
75 unset($vars['extra'][$var]);
80 foreach ($vars['context'] as $var => $val) {
81 if (false !== strpos($output, '%context.'.$var.'%')) {
82 $output = str_replace('%context.'.$var.'%', $this->stringify($val), $output);
83 unset($vars['context'][$var]);
87 if ($this->ignoreEmptyContextAndExtra) {
88 if (empty($vars['context'])) {
89 unset($vars['context']);
90 $output = str_replace('%context%', '', $output);
93 if (empty($vars['extra'])) {
94 unset($vars['extra']);
95 $output = str_replace('%extra%', '', $output);
99 foreach ($vars as $var => $val) {
100 if (false !== strpos($output, '%'.$var.'%')) {
101 $output = str_replace('%'.$var.'%', $this->stringify($val), $output);
105 // remove leftover %extra.xxx% and %context.xxx% if any
106 if (false !== strpos($output, '%')) {
107 $output = preg_replace('/%(?:extra|context)\..+?%/', '', $output);
113 public function formatBatch(array $records)
116 foreach ($records as $record) {
117 $message .= $this->format($record);
123 public function stringify($value)
125 return $this->replaceNewlines($this->convertToString($value));
128 protected function normalizeException($e)
130 // TODO 2.0 only check for Throwable
131 if (!$e instanceof \Exception && !$e instanceof \Throwable) {
132 throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e));
136 if ($previous = $e->getPrevious()) {
138 $previousText .= ', '.get_class($previous).'(code: '.$previous->getCode().'): '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine();
139 } while ($previous = $previous->getPrevious());
142 $str = '[object] ('.get_class($e).'(code: '.$e->getCode().'): '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')';
143 if ($this->includeStacktraces) {
144 $str .= "\n[stacktrace]\n".$e->getTraceAsString()."\n";
150 protected function convertToString($data)
152 if (null === $data || is_bool($data)) {
153 return var_export($data, true);
156 if (is_scalar($data)) {
157 return (string) $data;
160 if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
161 return $this->toJson($data, true);
164 return str_replace('\\/', '/', @json_encode($data));
167 protected function replaceNewlines($str)
169 if ($this->allowInlineLineBreaks) {
170 if (0 === strpos($str, '{')) {
171 return str_replace(array('\r', '\n'), array("\r", "\n"), $str);
177 return str_replace(array("\r\n", "\r", "\n"), ' ', $str);