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