ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
23{
24 const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
25
26 protected $format;
30
37 public function __construct($format = null, $dateFormat = null, $allowInlineLineBreaks = false, $ignoreEmptyContextAndExtra = false)
38 {
39 $this->format = $format ?: static::SIMPLE_FORMAT;
42 parent::__construct($dateFormat);
43 }
44
45 public function includeStacktraces($include = true)
46 {
47 $this->includeStacktraces = $include;
48 if ($this->includeStacktraces) {
49 $this->allowInlineLineBreaks = true;
50 }
51 }
52
53 public function allowInlineLineBreaks($allow = true)
54 {
55 $this->allowInlineLineBreaks = $allow;
56 }
57
58 public function ignoreEmptyContextAndExtra($ignore = true)
59 {
61 }
62
66 public function format(array $record)
67 {
68 $vars = parent::format($record);
69
71
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]);
76 }
77 }
78
79 foreach ($vars['context'] as $var => $val) {
80 if (false !== strpos($output, '%context.'.$var.'%')) {
81 $output = str_replace('%context.'.$var.'%', $this->stringify($val), $output);
82 unset($vars['context'][$var]);
83 }
84 }
85
86 if ($this->ignoreEmptyContextAndExtra) {
87 if (empty($vars['context'])) {
88 unset($vars['context']);
89 $output = str_replace('%context%', '', $output);
90 }
91
92 if (empty($vars['extra'])) {
93 unset($vars['extra']);
94 $output = str_replace('%extra%', '', $output);
95 }
96 }
97
98 foreach ($vars as $var => $val) {
99 if (false !== strpos($output, '%'.$var.'%')) {
100 $output = str_replace('%'.$var.'%', $this->stringify($val), $output);
101 }
102 }
103
104 return $output;
105 }
106
107 public function formatBatch(array $records)
108 {
109 $message = '';
110 foreach ($records as $record) {
111 $message .= $this->format($record);
112 }
113
114 return $message;
115 }
116
117 public function stringify($value)
118 {
119 return $this->replaceNewlines($this->convertToString($value));
120 }
121
122 protected function normalizeException($e)
123 {
124 // TODO 2.0 only check for Throwable
125 if (!$e instanceof \Exception && !$e instanceof \Throwable) {
126 throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e));
127 }
128
129 $previousText = '';
130 if ($previous = $e->getPrevious()) {
131 do {
132 $previousText .= ', '.get_class($previous).'(code: '.$previous->getCode().'): '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine();
133 } while ($previous = $previous->getPrevious());
134 }
135
136 $str = '[object] ('.get_class($e).'(code: '.$e->getCode().'): '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')';
137 if ($this->includeStacktraces) {
138 $str .= "\n[stacktrace]\n".$e->getTraceAsString();
139 }
140
141 return $str;
142 }
143
144 protected function convertToString($data)
145 {
146 if (null === $data || is_bool($data)) {
147 return var_export($data, true);
148 }
149
150 if (is_scalar($data)) {
151 return (string) $data;
152 }
153
154 if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
155 return $this->toJson($data, true);
156 }
157
158 return str_replace('\\/', '/', @json_encode($data));
159 }
160
161 protected function replaceNewlines($str)
162 {
163 if ($this->allowInlineLineBreaks) {
164 return $str;
165 }
166
167 return str_replace(array("\r\n", "\r", "\n"), ' ', $str);
168 }
169}
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.
if(!is_dir( $entity_dir)) exit("Fatal Error ([A-Za-z0-9]+)\s+" &#(? foreach( $entity_files as $file) $output
catch(Exception $e) $message
$records
Definition: simple_test.php:22