PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / formatters / class-genericformatter.php

class-genericformatter.php in DecaLog 4.4.0, at includes/formatters/class-genericformatter.php

162 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2 /**
3 * Generic formatter for Monolog
4 *
5 * Handles all features of generic formatter for Monolog.
6 *
7 * @package Formatters
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 2.0.0
10 */
11
12 namespace Decalog\Formatter;
13
14 use Decalog\Plugin\Feature\ClassTypes;
15 use Decalog\Plugin\Feature\EventTypes;
16 use Decalog\System\Http;
17 use DLMonolog\Formatter\FormatterInterface;
18
19 /**
20 * Define the Monolog generic formatter.
21 *
22 * Handles all features of generic formatter for Monolog.
23 *
24 * @package Formatters
25 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
26 * @since 2.0.0
27 */
28 class GenericFormatter implements FormatterInterface {
29
30 /**
31 * Formats a log record.
32 *
33 * @param array $record A record to format.
34 * @return string The formatted record.
35 * @since 2.0.0
36 */
37 public function format( array $record ): string {
38 $message = [];
39 $values = [];
40 $values['timestamp'] = date( 'Y-m-d H:i:s' );
41 $values['level'] = 'unknown';
42 $values['channel'] = 'unknown';
43 $values['class'] = 'unknown';
44 $values['component'] = 'Unknown';
45 $values['version'] = 'N/A';
46 $values['code'] = '0';
47 $values['message'] = '';
48 $values['site_id'] = '0';
49 $values['site_name'] = 'Unknown';
50 $values['user_id'] = '0';
51 $values['user_name'] = 'Unknown';
52 $values['remote_ip'] = '127.0.0.1';
53 $values['url'] = '-';
54 $values['verb'] = 'unknown';
55 $values['server'] = 'unknown';
56 $values['referrer'] = '';
57 $values['file'] = 'unknown';
58 $values['line'] = '0';
59 $values['classname'] = 'unknown';
60 $values['function'] = 'unknown';
61 if ( array_key_exists( 'level', $record ) ) {
62 if ( array_key_exists( $record['level'], EventTypes::$level_names ) ) {
63 $values['level'] = strtolower( EventTypes::$level_names[ $record['level'] ] );
64 }
65 }
66 if ( array_key_exists( 'channel', $record ) ) {
67 $values['channel'] = strtolower( $record['channel'] );
68 }
69 if ( array_key_exists( 'message', $record ) ) {
70 $values['message'] = substr( $record['message'], 0, 65000 );
71 }
72 // Context formatting.
73 if ( array_key_exists( 'context', $record ) ) {
74 $context = $record['context'];
75 if ( array_key_exists( 'class', $context ) ) {
76 if ( in_array( $context['class'], ClassTypes::$classes, true ) ) {
77 $values['class'] = strtolower( $context['class'] );
78 }
79 }
80 if ( array_key_exists( 'component', $context ) ) {
81 $values['component'] = substr( $context['component'], 0, 26 );
82 }
83 if ( array_key_exists( 'version', $context ) ) {
84 $values['version'] = substr( $context['version'], 0, 13 );
85 }
86 if ( array_key_exists( 'code', $context ) ) {
87 $values['code'] = (int) $context['code'];
88 }
89 }
90 // Extra formatting.
91 if ( array_key_exists( 'extra', $record ) ) {
92 $extra = $record['extra'];
93 if ( array_key_exists( 'siteid', $extra ) ) {
94 $values['site_id'] = (int) $extra['siteid'];
95 }
96 if ( array_key_exists( 'sitename', $extra ) && is_string( $extra['sitename'] ) ) {
97 $values['site_name'] = substr( $extra['sitename'], 0, 250 );
98 }
99 if ( array_key_exists( 'userid', $extra ) && is_scalar( $extra['userid'] ) ) {
100 $values['user_id'] = substr( (string) $extra['userid'], 0, 66 );
101 }
102 if ( array_key_exists( 'username', $extra ) && is_string( $extra['username'] ) ) {
103 $values['user_name'] = substr( $extra['username'], 0, 250 );
104 }
105 if ( array_key_exists( 'ip', $extra ) && is_string( $extra['ip'] ) ) {
106 $values['remote_ip'] = substr( $extra['ip'], 0, 66 );
107 }
108 if ( array_key_exists( 'url', $extra ) && is_string( $extra['url'] ) ) {
109 $values['url'] = substr( $extra['url'], 0, 2083 );
110 }
111 if ( array_key_exists( 'http_method', $extra ) && is_string( $extra['http_method'] ) ) {
112 if ( in_array( strtolower( $extra['http_method'] ), Http::$verbs, true ) ) {
113 $values['verb'] = strtolower( $extra['http_method'] );
114 }
115 }
116 if ( array_key_exists( 'server', $extra ) && is_string( $extra['server'] ) ) {
117 $values['server'] = substr( $extra['server'], 0, 250 );
118 }
119 if ( array_key_exists( 'referrer', $extra ) && $extra['referrer'] && is_string( $extra['referrer'] ) ) {
120 $values['referrer'] = substr( $extra['referrer'], 0, 250 );
121 }
122 if ( array_key_exists( 'ua', $extra ) && $extra['ua'] && is_string( $extra['ua'] ) ) {
123 $values['user_agent'] = substr( $extra['ua'], 0, 1024 );
124 }
125 if ( array_key_exists( 'file', $extra ) && $extra['file'] && is_string( $extra['file'] ) ) {
126 $values['file'] = substr( $extra['file'], 0, 250 );
127 }
128 if ( array_key_exists( 'line', $extra ) && $extra['line'] ) {
129 $values['line'] = (int) $extra['line'];
130 }
131 if ( array_key_exists( 'class', $extra ) && $extra['class'] && is_string( $extra['class'] ) ) {
132 $values['classname'] = substr( $extra['class'], 0, 100 );
133 }
134 if ( array_key_exists( 'function', $extra ) && $extra['function'] && is_string( $extra['function'] ) ) {
135 $values['function'] = substr( $extra['function'], 0, 100 );
136 }
137 }
138 $message[] = $values;
139 // phpcs:ignore
140 return serialize( $message );
141 }
142 /**
143 * Formats a set of log records.
144 *
145 * @param array $records A set of records to format.
146 * @return string The formatted set of records.
147 * @since 2.0.0
148 */
149 public function formatBatch( array $records ): string {
150 $messages = [];
151 foreach ( $records as $record ) {
152 // phpcs:ignore
153 $a = unserialize( $this->format( $record ) );
154 if ( 1 === count( $a ) ) {
155 $messages[] = $a[0];
156 }
157 }
158 // phpcs:ignore
159 return serialize( $messages );
160 }
161 }
162