ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Monolog\Formatter\GelfMessageFormatter Class Reference

Serializes a log message to GELF. More...

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

Public Member Functions

 __construct ($systemName=null, $extraPrefix=null, $contextPrefix='ctxt_')
 
 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 MAX_LENGTH = 32766
 
- Data Fields inherited from Monolog\Formatter\NormalizerFormatter
const SIMPLE_DATE = "Y-m-d H:i:s"
 

Protected Attributes

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

Private Attributes

 $logLevels
 Translates Monolog log levels to Graylog2 log priorities. More...
 

Additional Inherited Members

- Protected Member Functions inherited from Monolog\Formatter\NormalizerFormatter
 normalize ($data)
 
 normalizeException ($e)
 
 toJson ($data, $ignoreErrors=false)
 Return the JSON representation of a value. More...
 

Detailed Description

Serializes a log message to GELF.

See also
http://www.graylog2.org/about/gelf
Author
Matt Lehner mlehn.nosp@m.er@g.nosp@m.mail..nosp@m.com

Definition at line 23 of file GelfMessageFormatter.php.

Constructor & Destructor Documentation

◆ __construct()

Monolog\Formatter\GelfMessageFormatter::__construct (   $systemName = null,
  $extraPrefix = null,
  $contextPrefix = 'ctxt_' 
)

Member Function Documentation

◆ format()

Monolog\Formatter\GelfMessageFormatter::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 69 of file GelfMessageFormatter.php.

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

70  {
71  $record = parent::format($record);
72 
73  if (!isset($record['datetime'], $record['message'], $record['level'])) {
74  throw new \InvalidArgumentException('The record should at least contain datetime, message and level keys, '.var_export($record, true).' given');
75  }
76 
77  $message = new Message();
78  $message
79  ->setTimestamp($record['datetime'])
80  ->setShortMessage((string) $record['message'])
81  ->setHost($this->systemName)
82  ->setLevel($this->logLevels[$record['level']]);
83 
84  // start count with message length + system name length + 200 for padding / metadata
85  $len = 200 + strlen((string) $record['message']) + strlen($this->systemName);
86 
87  if ($len > self::MAX_LENGTH) {
88  $message->setShortMessage(substr($record['message'], 0, self::MAX_LENGTH - 200));
89 
90  return $message;
91  }
92 
93  if (isset($record['channel'])) {
94  $message->setFacility($record['channel']);
95  $len += strlen($record['channel']);
96  }
97  if (isset($record['extra']['line'])) {
98  $message->setLine($record['extra']['line']);
99  $len += 10;
100  unset($record['extra']['line']);
101  }
102  if (isset($record['extra']['file'])) {
103  $message->setFile($record['extra']['file']);
104  $len += strlen($record['extra']['file']);
105  unset($record['extra']['file']);
106  }
107 
108  foreach ($record['extra'] as $key => $val) {
109  $val = is_scalar($val) || null === $val ? $val : $this->toJson($val);
110  $len += strlen($this->extraPrefix . $key . $val);
111  if ($len > self::MAX_LENGTH) {
112  $message->setAdditional($this->extraPrefix . $key, substr($val, 0, self::MAX_LENGTH - $len));
113  break;
114  }
115  $message->setAdditional($this->extraPrefix . $key, $val);
116  }
117 
118  foreach ($record['context'] as $key => $val) {
119  $val = is_scalar($val) || null === $val ? $val : $this->toJson($val);
120  $len += strlen($this->contextPrefix . $key . $val);
121  if ($len > self::MAX_LENGTH) {
122  $message->setAdditional($this->contextPrefix . $key, substr($val, 0, self::MAX_LENGTH - $len));
123  break;
124  }
125  $message->setAdditional($this->contextPrefix . $key, $val);
126  }
127 
128  if (null === $message->getFile() && isset($record['context']['exception']['file'])) {
129  if (preg_match("/^(.+):([0-9]+)$/", $record['context']['exception']['file'], $matches)) {
130  $message->setFile($matches[1]);
131  $message->setLine($matches[2]);
132  }
133  }
134 
135  return $message;
136  }
toJson($data, $ignoreErrors=false)
Return the JSON representation of a value.
Write to Excel2007 format
+ Here is the call graph for this function:

Field Documentation

◆ $contextPrefix

Monolog\Formatter\GelfMessageFormatter::$contextPrefix
protected

◆ $extraPrefix

Monolog\Formatter\GelfMessageFormatter::$extraPrefix
protected

◆ $logLevels

Monolog\Formatter\GelfMessageFormatter::$logLevels
private
Initial value:

Translates Monolog log levels to Graylog2 log priorities.

Definition at line 45 of file GelfMessageFormatter.php.

◆ $systemName

Monolog\Formatter\GelfMessageFormatter::$systemName
protected

◆ MAX_LENGTH

const Monolog\Formatter\GelfMessageFormatter::MAX_LENGTH = 32766

Definition at line 25 of file GelfMessageFormatter.php.


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