ILIAS  release_5-2 Revision v5.2.25-18-g3f80b82851
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...
 
 detectAndCleanUtf8 (&$data)
 Detect invalid UTF-8 string characters and convert to valid UTF-8. 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 ($e)
 
 toJson ($data, $ignoreErrors=false)
 Return the JSON representation of a value. More...
 

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_
int$versionthe logstash format version to use, defaults to 0

Definition at line 59 of file LogstashFormatter.php.

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

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  }

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
}

Implements Monolog\Formatter\FormatterInterface.

Definition at line 74 of file LogstashFormatter.php.

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

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  }
toJson($data, $ignoreErrors=false)
Return the JSON representation of a value.
Write to Excel2007 format
+ Here is the call graph for this function:

◆ formatV0()

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

Definition at line 87 of file LogstashFormatter.php.

References Monolog\Formatter\LogstashFormatter\$applicationName, and array.

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

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) {
108  $message['@type'] = $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  }
Create styles array
The data for the language used.
+ Here is the caller graph for this function:

◆ formatV1()

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

Definition at line 130 of file LogstashFormatter.php.

References Monolog\Formatter\LogstashFormatter\$applicationName, and array.

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

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) {
151  $message['type'] = $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  }
Create styles array
The data for the language used.
+ 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: