PluginProbe
ManageWP Worker / 4.9.35
ManageWP Worker v4.9.35
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / Monolog / Formatter / LegacyGelfMessageFormatter.php

LegacyGelfMessageFormatter.php in ManageWP Worker 4.9.35, at src/Monolog/Formatter/LegacyGelfMessageFormatter.php

113 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <[email protected]>
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 /**
13 * Serializes a log message to GELF
14 *
15 * @see http://www.graylog2.org/about/gelf
16 *
17 * @author Matt Lehner <[email protected]>
18 */
19 class Monolog_Formatter_LegacyGelfMessageFormatter extends Monolog_Formatter_NormalizerFormatter
20 {
21 /**
22 * @var string the name of the system for the Gelf log message
23 */
24 protected $systemName;
25
26 /**
27 * @var string a prefix for 'extra' fields from the Monolog record (optional)
28 */
29 protected $extraPrefix;
30
31 /**
32 * @var string a prefix for 'context' fields from the Monolog record (optional)
33 */
34 protected $contextPrefix;
35
36 /**
37 * Translates Monolog log levels to Graylog2 log priorities.
38 */
39 private $logLevels = array(
40 Monolog_Logger::DEBUG => 7,
41 Monolog_Logger::INFO => 6,
42 Monolog_Logger::NOTICE => 5,
43 Monolog_Logger::WARNING => 4,
44 Monolog_Logger::ERROR => 3,
45 Monolog_Logger::CRITICAL => 2,
46 Monolog_Logger::ALERT => 1,
47 Monolog_Logger::EMERGENCY => 0,
48 );
49
50 public function __construct($systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_')
51 {
52 parent::__construct('U.u');
53
54 $this->systemName = $systemName ? $systemName : php_uname('n');
55
56 $this->extraPrefix = $extraPrefix;
57 $this->contextPrefix = $contextPrefix;
58 }
59
60 /**
61 * {@inheritdoc}
62 */
63 public function format(array $record)
64 {
65 $record = parent::format($record);
66 $message = new Gelf_Message();
67 $message
68 ->setTimestamp($record['datetime'])
69 ->setShortMessage((string) $record['message'])
70 ->setFacility($record['channel'])
71 ->setHost($this->systemName)
72 ->setLine(isset($record['extra']['line']) ? $record['extra']['line'] : null)
73 ->setFile(isset($record['extra']['file']) ? $record['extra']['file'] : null)
74 ->setLevel($this->logLevels[$record['level']]);
75
76 // Do not duplicate these values in the additional fields
77 unset($record['extra']['line']);
78 unset($record['extra']['file']);
79
80 if (isset($record['extra']['memory_usage'])) {
81 $record['extra']['mem_usage'] = $record['extra']['memory_usage'];
82 unset($record['extra']['memory_usage']);
83 }
84
85 if (isset($record['extra']['memory_real_usage'])) {
86 $record['extra']['mem_real_usage'] = $record['extra']['memory_real_usage'];
87 unset($record['extra']['memory_real_usage']);
88 }
89
90 if (isset($record['extra']['memory_peak_usage'])) {
91 $record['extra']['mem_peak_usage'] = $record['extra']['memory_peak_usage'];
92 unset($record['extra']['memory_peak_usage']);
93 }
94
95 foreach ($record['extra'] as $key => $val) {
96 $message->setAdditional($this->extraPrefix.$key, is_scalar($val) ? $val : $this->toJson($val));
97 }
98
99 foreach ($record['context'] as $key => $val) {
100 $message->setAdditional($this->contextPrefix.$key, is_scalar($val) ? $val : $this->toJson($val));
101 }
102
103 if (null === $message->getFile() && isset($record['context']['exception'])) {
104 if (preg_match("/^(.+):([0-9]+)$/", $record['context']['exception']['file'], $matches)) {
105 $message->setFile($matches[1]);
106 $message->setLine($matches[2]);
107 }
108 }
109
110 return $message;
111 }
112 }
113