ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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{
28 protected $systemName;
29
33 protected $extraPrefix;
34
38 protected $contextPrefix;
39
43 private $logLevels = array(
44 Logger::DEBUG => 7,
45 Logger::INFO => 6,
46 Logger::NOTICE => 5,
47 Logger::WARNING => 4,
48 Logger::ERROR => 3,
50 Logger::ALERT => 1,
52 );
53
54 public function __construct($systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_')
55 {
56 parent::__construct('U.u');
57
58 $this->systemName = $systemName ?: gethostname();
59
60 $this->extraPrefix = $extraPrefix;
61 $this->contextPrefix = $contextPrefix;
62 }
63
67 public function format(array $record)
68 {
69 $record = parent::format($record);
70
71 if (!isset($record['datetime'], $record['message'], $record['level'])) {
72 throw new \InvalidArgumentException('The record should at least contain datetime, message and level keys, '.var_export($record, true).' given');
73 }
74
75 $message = new Message();
76 $message
77 ->setTimestamp($record['datetime'])
78 ->setShortMessage((string) $record['message'])
79 ->setHost($this->systemName)
80 ->setLevel($this->logLevels[$record['level']]);
81
82 if (isset($record['channel'])) {
83 $message->setFacility($record['channel']);
84 }
85 if (isset($record['extra']['line'])) {
86 $message->setLine($record['extra']['line']);
87 unset($record['extra']['line']);
88 }
89 if (isset($record['extra']['file'])) {
90 $message->setFile($record['extra']['file']);
91 unset($record['extra']['file']);
92 }
93
94 foreach ($record['extra'] as $key => $val) {
95 $message->setAdditional($this->extraPrefix . $key, is_scalar($val) ? $val : $this->toJson($val));
96 }
97
98 foreach ($record['context'] as $key => $val) {
99 $message->setAdditional($this->contextPrefix . $key, is_scalar($val) ? $val : $this->toJson($val));
100 }
101
102 if (null === $message->getFile() && isset($record['context']['exception']['file'])) {
103 if (preg_match("/^(.+):([0-9]+)$/", $record['context']['exception']['file'], $matches)) {
104 $message->setFile($matches[1]);
105 $message->setLine($matches[2]);
106 }
107 }
108
109 return $message;
110 }
111}
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.
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