PluginProbe ʕ •ᴥ•ʔ
Advanced Database Cleaner – Optimize & Clean Database to Speed Up Site Performance / trunk
Advanced Database Cleaner – Optimize & Clean Database to Speed Up Site Performance vtrunk
trunk 1.0.0 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.5 1.3.6 1.3.7 2.0.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.1.0 4.1.1
advanced-database-cleaner / includes / utils / class-adbc-logging.php
advanced-database-cleaner / includes / utils Last commit date
validator 2 months ago class-adbc-common-utils.php 4 months ago class-adbc-files.php 7 months ago class-adbc-logging.php 2 months ago class-adbc-notifications.php 4 months ago class-adbc-rest.php 2 months ago
class-adbc-logging.php
166 lines
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined( 'ABSPATH' ) )
5 exit;
6
7 /**
8 * ADBC Logging class.
9 *
10 * This class provides logging methods.
11 */
12 class ADBC_Logging {
13
14 public const DEBUG_LOG_FILE_PATH = ADBC_UPLOADS_DIR_PATH . '/debug.log';
15 private const DEBUG_LOG_FILE_MAX_SIZE = 100 * 1024; // 100 KB.
16 private const WP_DEBUG_FILE_MAX_SIZE = 1 * 1024 * 1024; // 1 MB.
17 private const ERROR_MESSAGE_MAX_LENGTH = 300;
18 private const EXCEPTION_MESSAGE_MAX_LENGTH = 500;
19
20 /**
21 * Log an error to the debug.log file.
22 *
23 * @param string $message The error message.
24 * @param string $method The method name where the error occurred (optional).
25 * @param int $line The line number where the error occurred (optional).
26 * @return void
27 */
28 public static function log_error( $message, $method = "", $line = "" ) {
29
30 if ( ! empty( $method ) )
31 $message .= " in {$method}";
32
33 if ( ! empty( $line ) )
34 $message .= " on line {$line}";
35
36 self::log( $message );
37 }
38
39 /**
40 * Log an exception to the debug.log file.
41 *
42 * @param string $method_name The method name where the exception occurred.
43 * @param object $exception The exception object.
44 * @return void
45 */
46 public static function log_exception( $method_name, $exception ) {
47 self::log( "Exception in {$method_name}", $exception );
48 }
49
50 /**
51 * Log a message to the debug.log file.
52 *
53 * @param string $message The message to log.
54 * @param object $exception The exception object if any.
55 * @return boolean True if successful, false otherwise.
56 */
57 private static function log( $message, $exception = null ) {
58
59 if ( ! ADBC_Files::instance()->is_readable_and_writable( self::DEBUG_LOG_FILE_PATH ) )
60 return false;
61
62 $debug_log_file_size = ADBC_Files::instance()->size( self::DEBUG_LOG_FILE_PATH );
63 if ( $debug_log_file_size !== false && $debug_log_file_size > self::DEBUG_LOG_FILE_MAX_SIZE ) {
64 if ( ! self::clear_log() )
65 return false;
66 }
67
68 $message = ADBC_Common_Utils::truncate_string( $message, self::ERROR_MESSAGE_MAX_LENGTH );
69 $exception_msg = '';
70
71 if ( $exception !== null && $exception instanceof Throwable )
72 $exception_msg = self::prepare_exception_message( $exception );
73
74 if ( ! error_log( '[' . date( 'Y-m-d H:i:s' ) . '] ' . $message . ' ' . $exception_msg . PHP_EOL, 3, self::DEBUG_LOG_FILE_PATH ) )
75 return false;
76
77 return true;
78 }
79
80 /**
81 * Get the log file content.
82 *
83 * @param string $file_type The file type to get ('debug' or 'wp-debug').
84 * @return array The log content.
85 */
86 public static function get_log_content( $file_type = '' ) {
87
88 if ( $file_type === 'debug' ) {
89
90 $file_path = self::DEBUG_LOG_FILE_PATH;
91 $printable_path = ADBC_WP_UPLOADS_DIR_PATH . "/" . ADBC_UPLOADS_DIR_PREFIX . "******/" . basename( $file_path );
92 $max_file_size = self::DEBUG_LOG_FILE_MAX_SIZE;
93
94 } else {
95
96 $file_path = ADBC_WP_DEBUG_LOG_FILE_PATH;
97 $printable_path = ADBC_WP_DEBUG_LOG_FILE_PATH;
98 $max_file_size = self::WP_DEBUG_FILE_MAX_SIZE;
99
100 }
101
102 if ( ! ADBC_Files::instance()->is_readable( $file_path ) )
103 return [ 'success' => false, 'message' => __( 'The Log file is not readable or does not exist.', 'advanced-database-cleaner' ) ];
104
105 $debug_log_file_size = ADBC_Files::instance()->size( $file_path );
106 if ( $debug_log_file_size !== false && $debug_log_file_size > $max_file_size ) {
107 return [
108 'success' => false,
109 'message' => sprintf(
110 /* translators: 1: Log file path */
111 __( 'The log file cannot be loaded since its size exceeds the limit. You can access it directly via: %s', 'advanced-database-cleaner' ),
112 "\n" . $printable_path
113 )
114 ];
115 }
116
117 $content = ADBC_Files::instance()->get_contents( $file_path );
118
119 if ( $content === false )
120 return [ 'success' => false, 'message' => __( 'Failed to get the file content.', 'advanced-database-cleaner' ) ];
121
122 return [ 'success' => true, 'content' => $content ];
123 }
124
125 /**
126 * Clear the debug.log file.
127 *
128 * @return boolean True if successful, false otherwise.
129 */
130 public static function clear_log() {
131
132 // Clear the debug.log file.
133 if ( ! ADBC_Files::instance()->put_contents( self::DEBUG_LOG_FILE_PATH, '' ) )
134 return false;
135
136 return true;
137 }
138
139 /**
140 * Prepare the exception message to be logged.
141 *
142 * @param object $exception The exception object.
143 * @return string The exception message.
144 */
145 private static function prepare_exception_message( $exception ) {
146
147 // Prepare the message.
148 $message = $exception->getMessage();
149 $message = ADBC_Common_Utils::truncate_string( $message, self::EXCEPTION_MESSAGE_MAX_LENGTH );
150
151 // Prepare the path.
152 $full_path = $exception->getFile();
153 $position = strpos( $full_path, ADBC_PLUGIN_DIR_NAME );
154
155 if ( $position !== false ) {
156 $secured_path = substr( $full_path, $position ); // Trim the path to start from the plugin dir name
157 } else {
158 $secured_path = basename( $full_path ); // If plugin dir not found, use the file name only.
159 }
160
161 $exception_msg = '=> ' . $message . ". In file '" . $secured_path . "' on line " . $exception->getLine();
162
163 return $exception_msg;
164 }
165
166 }