ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
LineFormatter.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Monolog\Formatter;
13
15
25{
26 const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
27
28 protected $format;
32
39 public function __construct($format = null, $dateFormat = null, $allowInlineLineBreaks = false, $ignoreEmptyContextAndExtra = false)
40 {
41 $this->format = $format ?: static::SIMPLE_FORMAT;
44 parent::__construct($dateFormat);
45 }
46
47 public function includeStacktraces($include = true)
48 {
49 $this->includeStacktraces = $include;
50 if ($this->includeStacktraces) {
51 $this->allowInlineLineBreaks = true;
52 }
53 }
54
55 public function allowInlineLineBreaks($allow = true)
56 {
57 $this->allowInlineLineBreaks = $allow;
58 }
59
60 public function ignoreEmptyContextAndExtra($ignore = true)
61 {
63 }
64
68 public function format(array $record)
69 {
70 $vars = parent::format($record);
71
73
74 foreach ($vars['extra'] as $var => $val) {
75 if (false !== strpos($output, '%extra.'.$var.'%')) {
76 $output = str_replace('%extra.'.$var.'%', $this->stringify($val), $output);
77 unset($vars['extra'][$var]);
78 }
79 }
80
81
82 foreach ($vars['context'] as $var => $val) {
83 if (false !== strpos($output, '%context.'.$var.'%')) {
84 $output = str_replace('%context.'.$var.'%', $this->stringify($val), $output);
85 unset($vars['context'][$var]);
86 }
87 }
88
89 if ($this->ignoreEmptyContextAndExtra) {
90 if (empty($vars['context'])) {
91 unset($vars['context']);
92 $output = str_replace('%context%', '', $output);
93 }
94
95 if (empty($vars['extra'])) {
96 unset($vars['extra']);
97 $output = str_replace('%extra%', '', $output);
98 }
99 }
100
101 foreach ($vars as $var => $val) {
102 if (false !== strpos($output, '%'.$var.'%')) {
103 $output = str_replace('%'.$var.'%', $this->stringify($val), $output);
104 }
105 }
106
107 // remove leftover %extra.xxx% and %context.xxx% if any
108 if (false !== strpos($output, '%')) {
109 $output = preg_replace('/%(?:extra|context)\..+?%/', '', $output);
110 }
111
112 return $output;
113 }
114
115 public function formatBatch(array $records)
116 {
117 $message = '';
118 foreach ($records as $record) {
119 $message .= $this->format($record);
120 }
121
122 return $message;
123 }
124
125 public function stringify($value)
126 {
127 return $this->replaceNewlines($this->convertToString($value));
128 }
129
130 protected function normalizeException($e)
131 {
132 // TODO 2.0 only check for Throwable
133 if (!$e instanceof \Exception && !$e instanceof \Throwable) {
134 throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.Utils::getClass($e));
135 }
136
137 $previousText = '';
138 if ($previous = $e->getPrevious()) {
139 do {
140 $previousText .= ', '.Utils::getClass($previous).'(code: '.$previous->getCode().'): '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine();
141 } while ($previous = $previous->getPrevious());
142 }
143
144 $str = '[object] ('.Utils::getClass($e).'(code: '.$e->getCode().'): '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')';
145 if ($this->includeStacktraces) {
146 $str .= "\n[stacktrace]\n".$e->getTraceAsString()."\n";
147 }
148
149 return $str;
150 }
151
152 protected function convertToString($data)
153 {
154 if (null === $data || is_bool($data)) {
155 return var_export($data, true);
156 }
157
158 if (is_scalar($data)) {
159 return (string) $data;
160 }
161
162 if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
163 return $this->toJson($data, true);
164 }
165
166 return str_replace('\\/', '/', @json_encode($data));
167 }
168
169 protected function replaceNewlines($str)
170 {
171 if ($this->allowInlineLineBreaks) {
172 if (0 === strpos($str, '{')) {
173 return str_replace(array('\r', '\n'), array("\r", "\n"), $str);
174 }
175
176 return $str;
177 }
178
179 return str_replace(array("\r\n", "\r", "\n"), ' ', $str);
180 }
181}
while(false !==( $line=fgets( $in))) if(! $columns) $ignore
Definition: Utf8Test.php:63
An exception for terminatinating execution or to throw for unit testing.
Formats incoming records into a one-line string.
formatBatch(array $records)
{{Formats a set of log records.mixed The formatted set of records}}
__construct($format=null, $dateFormat=null, $allowInlineLineBreaks=false, $ignoreEmptyContextAndExtra=false)
format(array $record)
{{Formats a log record.mixed The formatted record}}
ignoreEmptyContextAndExtra($ignore=true)
Normalizes incoming records to remove objects/resources so it's easier to dump to various targets.
toJson($data, $ignoreErrors=false)
Return the JSON representation of a value.
static getClass($object)
Definition: Utils.php:19
catch(Exception $e) $message
$records
Definition: simple_test.php:22
$data
Definition: bench.php:6