| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
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 |
|
|
|
12 |
namespace Monolog\Formatter;
|
|
|
13 |
|
|
|
14 |
use Monolog\Logger;
|
|
|
15 |
use Gelf\Message;
|
|
|
16 |
use Monolog\Utils;
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Serializes a log message to GELF
|
|
|
20 |
* @see http://docs.graylog.org/en/latest/pages/gelf.html
|
|
|
21 |
*
|
|
|
22 |
* @author Matt Lehner <mlehner@gmail.com>
|
|
|
23 |
*
|
|
|
24 |
* @phpstan-import-type Level from \Monolog\Logger
|
|
|
25 |
*/
|
|
|
26 |
class GelfMessageFormatter extends NormalizerFormatter
|
|
|
27 |
{
|
|
|
28 |
protected const DEFAULT_MAX_LENGTH = 32766;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* @var string the name of the system for the Gelf log message
|
|
|
32 |
*/
|
|
|
33 |
protected $systemName;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @var string a prefix for 'extra' fields from the Monolog record (optional)
|
|
|
37 |
*/
|
|
|
38 |
protected $extraPrefix;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* @var string a prefix for 'context' fields from the Monolog record (optional)
|
|
|
42 |
*/
|
|
|
43 |
protected $contextPrefix;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* @var int max length per field
|
|
|
47 |
*/
|
|
|
48 |
protected $maxLength;
|
|
|
49 |
|
|
|
50 |
/**
|
| 621 |
lars |
51 |
* @var int
|
|
|
52 |
*/
|
|
|
53 |
private $gelfVersion = 2;
|
|
|
54 |
|
|
|
55 |
/**
|
| 148 |
lars |
56 |
* Translates Monolog log levels to Graylog2 log priorities.
|
|
|
57 |
*
|
|
|
58 |
* @var array<int, int>
|
|
|
59 |
*
|
|
|
60 |
* @phpstan-var array<Level, int>
|
|
|
61 |
*/
|
|
|
62 |
private $logLevels = [
|
|
|
63 |
Logger::DEBUG => 7,
|
|
|
64 |
Logger::INFO => 6,
|
|
|
65 |
Logger::NOTICE => 5,
|
|
|
66 |
Logger::WARNING => 4,
|
|
|
67 |
Logger::ERROR => 3,
|
|
|
68 |
Logger::CRITICAL => 2,
|
|
|
69 |
Logger::ALERT => 1,
|
|
|
70 |
Logger::EMERGENCY => 0,
|
|
|
71 |
];
|
|
|
72 |
|
|
|
73 |
public function __construct(?string $systemName = null, ?string $extraPrefix = null, string $contextPrefix = 'ctxt_', ?int $maxLength = null)
|
|
|
74 |
{
|
|
|
75 |
if (!class_exists(Message::class)) {
|
|
|
76 |
throw new \RuntimeException('Composer package graylog2/gelf-php is required to use Monolog\'s GelfMessageFormatter');
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
parent::__construct('U.u');
|
|
|
80 |
|
|
|
81 |
$this->systemName = (is_null($systemName) || $systemName === '') ? (string) gethostname() : $systemName;
|
|
|
82 |
|
|
|
83 |
$this->extraPrefix = is_null($extraPrefix) ? '' : $extraPrefix;
|
|
|
84 |
$this->contextPrefix = $contextPrefix;
|
|
|
85 |
$this->maxLength = is_null($maxLength) ? self::DEFAULT_MAX_LENGTH : $maxLength;
|
| 621 |
lars |
86 |
|
|
|
87 |
if (method_exists(Message::class, 'setFacility')) {
|
|
|
88 |
$this->gelfVersion = 1;
|
|
|
89 |
}
|
| 148 |
lars |
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* {@inheritDoc}
|
|
|
94 |
*/
|
|
|
95 |
public function format(array $record): Message
|
|
|
96 |
{
|
|
|
97 |
$context = $extra = [];
|
|
|
98 |
if (isset($record['context'])) {
|
|
|
99 |
/** @var mixed[] $context */
|
|
|
100 |
$context = parent::normalize($record['context']);
|
|
|
101 |
}
|
|
|
102 |
if (isset($record['extra'])) {
|
|
|
103 |
/** @var mixed[] $extra */
|
|
|
104 |
$extra = parent::normalize($record['extra']);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
if (!isset($record['datetime'], $record['message'], $record['level'])) {
|
|
|
108 |
throw new \InvalidArgumentException('The record should at least contain datetime, message and level keys, '.var_export($record, true).' given');
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
$message = new Message();
|
|
|
112 |
$message
|
|
|
113 |
->setTimestamp($record['datetime'])
|
|
|
114 |
->setShortMessage((string) $record['message'])
|
|
|
115 |
->setHost($this->systemName)
|
|
|
116 |
->setLevel($this->logLevels[$record['level']]);
|
|
|
117 |
|
|
|
118 |
// message length + system name length + 200 for padding / metadata
|
|
|
119 |
$len = 200 + strlen((string) $record['message']) + strlen($this->systemName);
|
|
|
120 |
|
|
|
121 |
if ($len > $this->maxLength) {
|
|
|
122 |
$message->setShortMessage(Utils::substr($record['message'], 0, $this->maxLength));
|
|
|
123 |
}
|
|
|
124 |
|
| 621 |
lars |
125 |
if ($this->gelfVersion === 1) {
|
|
|
126 |
if (isset($record['channel'])) {
|
|
|
127 |
$message->setFacility($record['channel']);
|
|
|
128 |
}
|
|
|
129 |
if (isset($extra['line'])) {
|
|
|
130 |
$message->setLine($extra['line']);
|
|
|
131 |
unset($extra['line']);
|
|
|
132 |
}
|
|
|
133 |
if (isset($extra['file'])) {
|
|
|
134 |
$message->setFile($extra['file']);
|
|
|
135 |
unset($extra['file']);
|
|
|
136 |
}
|
|
|
137 |
} else {
|
|
|
138 |
$message->setAdditional('facility', $record['channel']);
|
| 148 |
lars |
139 |
}
|
|
|
140 |
|
|
|
141 |
foreach ($extra as $key => $val) {
|
|
|
142 |
$val = is_scalar($val) || null === $val ? $val : $this->toJson($val);
|
|
|
143 |
$len = strlen($this->extraPrefix . $key . $val);
|
|
|
144 |
if ($len > $this->maxLength) {
|
|
|
145 |
$message->setAdditional($this->extraPrefix . $key, Utils::substr((string) $val, 0, $this->maxLength));
|
|
|
146 |
|
|
|
147 |
continue;
|
|
|
148 |
}
|
|
|
149 |
$message->setAdditional($this->extraPrefix . $key, $val);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
foreach ($context as $key => $val) {
|
|
|
153 |
$val = is_scalar($val) || null === $val ? $val : $this->toJson($val);
|
|
|
154 |
$len = strlen($this->contextPrefix . $key . $val);
|
|
|
155 |
if ($len > $this->maxLength) {
|
|
|
156 |
$message->setAdditional($this->contextPrefix . $key, Utils::substr((string) $val, 0, $this->maxLength));
|
|
|
157 |
|
|
|
158 |
continue;
|
|
|
159 |
}
|
|
|
160 |
$message->setAdditional($this->contextPrefix . $key, $val);
|
|
|
161 |
}
|
|
|
162 |
|
| 621 |
lars |
163 |
if ($this->gelfVersion === 1) {
|
|
|
164 |
/** @phpstan-ignore-next-line */
|
|
|
165 |
if (null === $message->getFile() && isset($context['exception']['file'])) {
|
|
|
166 |
if (preg_match("/^(.+):([0-9]+)$/", $context['exception']['file'], $matches)) {
|
|
|
167 |
$message->setFile($matches[1]);
|
|
|
168 |
$message->setLine($matches[2]);
|
|
|
169 |
}
|
| 148 |
lars |
170 |
}
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
return $message;
|
|
|
174 |
}
|
|
|
175 |
}
|