ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
LogstashFormatter.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 
23 {
24  const V0 = 0;
25  const V1 = 1;
26 
30  protected $systemName;
31 
35  protected $applicationName;
36 
40  protected $extraPrefix;
41 
45  protected $contextPrefix;
46 
50  protected $version;
51 
58  public function __construct($applicationName, $systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_', $version = self::V0)
59  {
60  // logstash requires a ISO 8601 format date with optional millisecond precision.
61  parent::__construct('Y-m-d\TH:i:s.uP');
62 
63  $this->systemName = $systemName ?: gethostname();
64  $this->applicationName = $applicationName;
65  $this->extraPrefix = $extraPrefix;
66  $this->contextPrefix = $contextPrefix;
67  $this->version = $version;
68  }
69 
73  public function format(array $record)
74  {
75  $record = parent::format($record);
76 
77  if ($this->version === self::V1) {
78  $message = $this->formatV1($record);
79  } else {
80  $message = $this->formatV0($record);
81  }
82 
83  return $this->toJson($message) . "\n";
84  }
85 
86  protected function formatV0(array $record)
87  {
88  if (empty($record['datetime'])) {
89  $record['datetime'] = gmdate('c');
90  }
91  $message = array(
92  '@timestamp' => $record['datetime'],
93  '@source' => $this->systemName,
94  '@fields' => array()
95  );
96  if (isset($record['message'])) {
97  $message['@message'] = $record['message'];
98  }
99  if (isset($record['channel'])) {
100  $message['@tags'] = array($record['channel']);
101  $message['@fields']['channel'] = $record['channel'];
102  }
103  if (isset($record['level'])) {
104  $message['@fields']['level'] = $record['level'];
105  }
106  if ($this->applicationName) {
107  $message['@type'] = $this->applicationName;
108  }
109  if (isset($record['extra']['server'])) {
110  $message['@source_host'] = $record['extra']['server'];
111  }
112  if (isset($record['extra']['url'])) {
113  $message['@source_path'] = $record['extra']['url'];
114  }
115  if (!empty($record['extra'])) {
116  foreach ($record['extra'] as $key => $val) {
117  $message['@fields'][$this->extraPrefix . $key] = $val;
118  }
119  }
120  if (!empty($record['context'])) {
121  foreach ($record['context'] as $key => $val) {
122  $message['@fields'][$this->contextPrefix . $key] = $val;
123  }
124  }
125 
126  return $message;
127  }
128 
129  protected function formatV1(array $record)
130  {
131  if (empty($record['datetime'])) {
132  $record['datetime'] = gmdate('c');
133  }
134  $message = array(
135  '@timestamp' => $record['datetime'],
136  '@version' => 1,
137  'host' => $this->systemName,
138  );
139  if (isset($record['message'])) {
140  $message['message'] = $record['message'];
141  }
142  if (isset($record['channel'])) {
143  $message['type'] = $record['channel'];
144  $message['channel'] = $record['channel'];
145  }
146  if (isset($record['level_name'])) {
147  $message['level'] = $record['level_name'];
148  }
149  if ($this->applicationName) {
150  $message['type'] = $this->applicationName;
151  }
152  if (!empty($record['extra'])) {
153  foreach ($record['extra'] as $key => $val) {
154  $message[$this->extraPrefix . $key] = $val;
155  }
156  }
157  if (!empty($record['context'])) {
158  foreach ($record['context'] as $key => $val) {
159  $message[$this->contextPrefix . $key] = $val;
160  }
161  }
162 
163  return $message;
164  }
165 }
Normalizes incoming records to remove objects/resources so it&#39;s easier to dump to various targets...
__construct($applicationName, $systemName=null, $extraPrefix=null, $contextPrefix='ctxt_', $version=self::V0)
format(array $record)
{Formats a log record.A record to format mixed The formatted record}
Serializes a log message to Logstash Event Format.