PluginProbe
ManageWP Worker / 4.9.25
ManageWP Worker v4.9.25
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 / NormalizerFormatter.php

NormalizerFormatter.php in ManageWP Worker 4.9.25, at src/Monolog/Formatter/NormalizerFormatter.php

142 lines 3.7 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 <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 /**
13 * Normalizes incoming records to remove objects/resources so it's easier to dump to various targets
14 *
15 * @author Jordi Boggiano <j.boggiano@seld.be>
16 */
17 class Monolog_Formatter_NormalizerFormatter implements Monolog_Formatter_FormatterInterface
18 {
19 const SIMPLE_DATE = "Y-m-d H:i:s";
20
21 protected $dateFormat;
22
23 /**
24 * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
25 */
26 public function __construct($dateFormat = null)
27 {
28 $this->dateFormat = $dateFormat ? $dateFormat : self::SIMPLE_DATE;
29 }
30
31 /**
32 * {@inheritdoc}
33 */
34 public function format(array $record)
35 {
36 return $this->normalize($record);
37 }
38
39 /**
40 * {@inheritdoc}
41 */
42 public function formatBatch(array $records)
43 {
44 foreach ($records as $key => $record) {
45 $records[$key] = $this->format($record);
46 }
47
48 return $records;
49 }
50
51 protected function normalize($data)
52 {
53 if (null === $data || is_scalar($data)) {
54 return $data;
55 }
56
57 if (is_array($data) || $data instanceof Traversable) {
58 $normalized = array();
59
60 $count = 1;
61 foreach ($data as $key => $value) {
62 if ($count++ >= 1000) {
63 $normalized['...'] = 'Over 1000 items, aborting normalization';
64 break;
65 }
66 $normalized[$key] = $this->normalize($value);
67 }
68
69 return $normalized;
70 }
71
72 if ($data instanceof DateTime) {
73 return $data->format($this->dateFormat);
74 }
75
76 if (is_object($data)) {
77 if ($data instanceof Exception) {
78 return $this->normalizeException($data);
79 }
80
81 return sprintf("[object] (%s: %s)", get_class($data), $this->toJson($data, true));
82 }
83
84 if (is_resource($data)) {
85 return '[resource]';
86 }
87
88 return '[unknown('.gettype($data).')]';
89 }
90
91 protected function normalizeException(Exception $e)
92 {
93 $data = array(
94 'class' => get_class($e),
95 'message' => $e->getMessage(),
96 'file' => $e->getFile().':'.$e->getLine(),
97 );
98
99 $trace = $e->getTrace();
100 array_shift($trace);
101 foreach ($trace as $frame) {
102 if (isset($frame['file'])) {
103 $data['trace'][] = $frame['file'].':'.$frame['line'];
104 } else {
105 $data['trace'][] = json_encode($frame);
106 }
107 }
108
109 $previous = null;
110
111 if (is_callable(array($e, 'getPrevious'))) {
112 $previous = $e->getPrevious();
113 } elseif (is_callable($e, 'getPreviousException')) {
114 $previous = $e->getPreviousException();
115 }
116
117 if ($previous) {
118 $data['previous'] = $this->normalizeException($previous);
119 }
120
121 return $data;
122 }
123
124 protected function toJson($data, $ignoreErrors = false)
125 {
126 // suppress json_encode errors since it's twitchy with some inputs
127 if ($ignoreErrors) {
128 if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
129 return @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
130 }
131
132 return @json_encode($data);
133 }
134
135 if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
136 return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
137 }
138
139 return json_encode($data);
140 }
141 }
142