ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
GelfMessageFormatter.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
15use Gelf\Message;
16
24{
25 const MAX_LENGTH = 32766;
26
30 protected $systemName;
31
35 protected $extraPrefix;
36
40 protected $contextPrefix;
41
45 private $logLevels = array(
46 Logger::DEBUG => 7,
47 Logger::INFO => 6,
48 Logger::NOTICE => 5,
49 Logger::WARNING => 4,
50 Logger::ERROR => 3,
52 Logger::ALERT => 1,
54 );
55
56 public function __construct($systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_')
57 {
58 parent::__construct('U.u');
59
60 $this->systemName = $systemName ?: gethostname();
61
62 $this->extraPrefix = $extraPrefix;
63 $this->contextPrefix = $contextPrefix;
64 }
65
69 public function format(array $record)
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 }
137}
An exception for terminatinating execution or to throw for unit testing.
Serializes a log message to GELF.
$logLevels
Translates Monolog log levels to Graylog2 log priorities.
format(array $record)
{{Formats a log record.mixed The formatted record}}
__construct($systemName=null, $extraPrefix=null, $contextPrefix='ctxt_')
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.
Monolog log channel.
Definition: Logger.php:28
const EMERGENCY
Urgent alert.
Definition: Logger.php:77
const ERROR
Runtime errors.
Definition: Logger.php:57
const CRITICAL
Critical conditions.
Definition: Logger.php:64
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:52
const INFO
Interesting events.
Definition: Logger.php:39
const DEBUG
Detailed debug information.
Definition: Logger.php:32
const NOTICE
Uncommon events.
Definition: Logger.php:44
const ALERT
Action must be taken immediately.
Definition: Logger.php:72