ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Monolog\Formatter\LogstashFormatter Class Reference

Serializes a log message to Logstash Event Format. More...

+ Inheritance diagram for Monolog\Formatter\LogstashFormatter:
+ Collaboration diagram for Monolog\Formatter\LogstashFormatter:

Public Member Functions

 __construct ($applicationName, $systemName=null, $extraPrefix=null, $contextPrefix='ctxt_', $version=self::V0)
 
 format (array $record)
 {{Formats a log record.
Parameters
array$recordA record to format
Returns
mixed The formatted record
}} More...
 
- Public Member Functions inherited from Monolog\Formatter\NormalizerFormatter
 __construct ($dateFormat=null)
 
 format (array $record)
 {Formats a log record.
Parameters
array$recordA record to format
Returns
mixed The formatted record
} More...
 
 formatBatch (array $records)
 {Formats a set of log records.
Parameters
array$recordsA set of records to format
Returns
mixed The formatted set of records
} More...
 
 format (array $record)
 Formats a log record. More...
 
 formatBatch (array $records)
 Formats a set of log records. More...
 

Data Fields

const V0 = 0
 
const V1 = 1
 
- Data Fields inherited from Monolog\Formatter\NormalizerFormatter
const SIMPLE_DATE = "Y-m-d H:i:s"
 

Protected Member Functions

 formatV0 (array $record)
 
 formatV1 (array $record)
 
- Protected Member Functions inherited from Monolog\Formatter\NormalizerFormatter
 normalize ($data)
 
 normalizeException (Exception $e)
 
 toJson ($data, $ignoreErrors=false)
 

Protected Attributes

 $systemName
 
 $applicationName
 
 $extraPrefix
 
 $contextPrefix
 
 $version
 
- Protected Attributes inherited from Monolog\Formatter\NormalizerFormatter
 $dateFormat
 

Detailed Description

Constructor & Destructor Documentation

◆ __construct()

Monolog\Formatter\LogstashFormatter::__construct (   $applicationName,
  $systemName = null,
  $extraPrefix = null,
  $contextPrefix = 'ctxt_',
  $version = self::V0 
)
Parameters
string$applicationNamethe application that sends the data, used as the "type" field of logstash
string$systemNamethe system/machine name, used as the "source" field of logstash, defaults to the hostname of the machine
string$extraPrefixprefix for extra keys inside logstash "fields"
string$contextPrefixprefix for context keys inside logstash "fields", defaults to ctxt_

Definition at line 58 of file LogstashFormatter.php.

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 }

References Monolog\Formatter\LogstashFormatter\$applicationName, Monolog\Formatter\LogstashFormatter\$contextPrefix, Monolog\Formatter\LogstashFormatter\$extraPrefix, Monolog\Formatter\LogstashFormatter\$systemName, and Monolog\Formatter\LogstashFormatter\$version.

Member Function Documentation

◆ format()

Monolog\Formatter\LogstashFormatter::format ( array  $record)

{{Formats a log record.

Parameters
array$recordA record to format
Returns
mixed The formatted record
}}

Reimplemented from Monolog\Formatter\NormalizerFormatter.

Definition at line 73 of file LogstashFormatter.php.

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 }

References Monolog\Formatter\LogstashFormatter\formatV0(), Monolog\Formatter\LogstashFormatter\formatV1(), and Monolog\Formatter\NormalizerFormatter\toJson().

+ Here is the call graph for this function:

◆ formatV0()

Monolog\Formatter\LogstashFormatter::formatV0 ( array  $record)
protected

Definition at line 86 of file LogstashFormatter.php.

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 }

References Monolog\Formatter\LogstashFormatter\$applicationName.

Referenced by Monolog\Formatter\LogstashFormatter\format().

+ Here is the caller graph for this function:

◆ formatV1()

Monolog\Formatter\LogstashFormatter::formatV1 ( array  $record)
protected

Definition at line 129 of file LogstashFormatter.php.

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 }

References Monolog\Formatter\LogstashFormatter\$applicationName.

Referenced by Monolog\Formatter\LogstashFormatter\format().

+ Here is the caller graph for this function:

Field Documentation

◆ $applicationName

Monolog\Formatter\LogstashFormatter::$applicationName
protected

◆ $contextPrefix

Monolog\Formatter\LogstashFormatter::$contextPrefix
protected

◆ $extraPrefix

Monolog\Formatter\LogstashFormatter::$extraPrefix
protected

◆ $systemName

Monolog\Formatter\LogstashFormatter::$systemName
protected

◆ $version

Monolog\Formatter\LogstashFormatter::$version
protected

◆ V0

const Monolog\Formatter\LogstashFormatter::V0 = 0

Definition at line 24 of file LogstashFormatter.php.

◆ V1


The documentation for this class was generated from the following file: