-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdebug.php
More file actions
101 lines (92 loc) · 2.29 KB
/
debug.php
File metadata and controls
101 lines (92 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* Log class
* 方便印出 Error Log
*
* @package J7\WpUtils
*/
namespace J7\WpUtils\Classes;
if (!class_exists('J7\WpUtils\Classes\ErrorLog')) {
/**
* Log class
*/
abstract class ErrorLog {
/**
* Log a message.
*
* @param mixed $message The message to log.
* @param string $level The log level.
* @param string $context The context of the message.
*
* @return void
*/
protected static function log( $message, string $level = 'info', ?string $context = '' ): void {
$emoji = match ( $level ) {
'info' => '📘 ',
'error' => '❌ ',
'debug' => '🐛 ',
default => ' ',
};
$formatted_message = sprintf(
'[%1$s%2$s] %3$s %4$s',
$emoji,
strtoupper( $level ),
$context,
self::stringify( $message )
);
if ( defined( 'ABSPATH' ) ) {
$default_path = \ABSPATH . 'wp-content';
$default_file_name = 'debug.log';
$log_in_file = file_put_contents( "{$default_path}/{$default_file_name}", '[' . gmdate( 'Y-m-d H:i:s' ) . ' UTC]' . $formatted_message . PHP_EOL, FILE_APPEND );
} else {
// Write the log message using error_log()
error_log( $formatted_message );
}
}
/**
* Stringify the message.
*
* @param mixed $message The message to stringify.
*
* @return string The stringified message.
*/
public static function stringify( $message ): string {
ob_start();
var_dump($message);
return ob_get_clean();
}
/**
* Log a info message.
*
* @param mixed $message The message to log.
* @param string $context The context of the message.
*
* @return void
*/
public static function info( $message, ?string $context = '' ): void {
self::log( $message, 'info', $context );
}
/**
* Log a error message.
*
* @param mixed $message The message to log.
* @param string $context The context of the message.
*
* @return void
*/
public static function error( $message, ?string $context = '' ): void {
self::log( $message, 'error', $context );
}
/**
* Log a debug message.
*
* @param mixed $message The message to log.
* @param string $context The context of the message.
*
* @return void
*/
public static function debug( $message, ?string $context = '' ): void {
self::log( $message, 'debug', $context );
}
}
}