ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 
12 namespace Monolog\Formatter;
13 
14 use Exception;
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 
72  $output = $this->format;
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  if ($this->ignoreEmptyContextAndExtra) {
82  if (empty($vars['context'])) {
83  unset($vars['context']);
84  $output = str_replace('%context%', '', $output);
85  }
86 
87  if (empty($vars['extra'])) {
88  unset($vars['extra']);
89  $output = str_replace('%extra%', '', $output);
90  }
91  }
92 
93  foreach ($vars as $var => $val) {
94  if (false !== strpos($output, '%'.$var.'%')) {
95  $output = str_replace('%'.$var.'%', $this->stringify($val), $output);
96  }
97  }
98 
99  return $output;
100  }
101 
102  public function formatBatch(array $records)
103  {
104  $message = '';
105  foreach ($records as $record) {
106  $message .= $this->format($record);
107  }
108 
109  return $message;
110  }
111 
112  public function stringify($value)
113  {
114  return $this->replaceNewlines($this->convertToString($value));
115  }
116 
117  protected function normalizeException(Exception $e)
118  {
119  $previousText = '';
120  if ($previous = $e->getPrevious()) {
121  do {
122  $previousText .= ', '.get_class($previous).'(code: '.$previous->getCode().'): '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine();
123  } while ($previous = $previous->getPrevious());
124  }
125 
126  $str = '[object] ('.get_class($e).'(code: '.$e->getCode().'): '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')';
127  if ($this->includeStacktraces) {
128  $str .= "\n[stacktrace]\n".$e->getTraceAsString();
129  }
130 
131  return $str;
132  }
133 
134  protected function convertToString($data)
135  {
136  if (null === $data || is_bool($data)) {
137  return var_export($data, true);
138  }
139 
140  if (is_scalar($data)) {
141  return (string) $data;
142  }
143 
144  if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
145  return $this->toJson($data, true);
146  }
147 
148  return str_replace('\\/', '/', @json_encode($data));
149  }
150 
151  protected function replaceNewlines($str)
152  {
153  if ($this->allowInlineLineBreaks) {
154  return $str;
155  }
156 
157  return str_replace(array("\r\n", "\r", "\n"), ' ', $str);
158  }
159 }
while(false !==( $line=fgets( $in))) if(! $columns) $ignore
Definition: Utf8Test.php:64
$records
Definition: simple_test.php:17
ignoreEmptyContextAndExtra($ignore=true)
__construct($format=null, $dateFormat=null, $allowInlineLineBreaks=false, $ignoreEmptyContextAndExtra=false)
$data
Normalizes incoming records to remove objects/resources so it&#39;s easier to dump to various targets...
format(array $record)
{Formats a log record.A record to format mixed The formatted record}
Formats incoming records into a one-line string.
formatBatch(array $records)
Formats a set of log records.