abstracts
2 years ago
admin
2 years ago
elementor
4 years ago
export
3 years ago
fields
2 years ago
interfaces
8 years ago
libraries
2 years ago
log-handlers
4 years ago
shortcodes
2 years ago
stats
3 years ago
templates
5 years ago
class-everest-forms.php
2 years ago
class-evf-ajax.php
2 years ago
class-evf-autoloader.php
7 years ago
class-evf-background-updater.php
7 years ago
class-evf-cache-helper.php
6 years ago
class-evf-cron.php
3 years ago
class-evf-deprecated-action-hooks.php
6 years ago
class-evf-deprecated-filter-hooks.php
5 years ago
class-evf-emails.php
2 years ago
class-evf-fields.php
2 years ago
class-evf-form-block.php
4 years ago
class-evf-form-handler.php
3 years ago
class-evf-form-task.php
2 years ago
class-evf-forms-features.php
2 years ago
class-evf-frontend-scripts.php
2 years ago
class-evf-install.php
2 years ago
class-evf-integrations.php
7 years ago
class-evf-log-levels.php
8 years ago
class-evf-logger.php
5 years ago
class-evf-post-types.php
5 years ago
class-evf-privacy.php
6 years ago
class-evf-session-handler.php
7 years ago
class-evf-shortcodes.php
4 years ago
class-evf-smart-tags.php
2 years ago
class-evf-template-loader.php
2 years ago
class-evf-validation.php
6 years ago
evf-conditional-functions.php
6 years ago
evf-core-functions.php
2 years ago
evf-deprecated-functions.php
6 years ago
evf-entry-functions.php
3 years ago
evf-formatting-functions.php
4 years ago
evf-notice-functions.php
4 years ago
evf-template-functions.php
4 years ago
evf-template-hooks.php
7 years ago
evf-update-functions.php
5 years ago
class-evf-logger.php
303 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Provides logging capabilities for debugging purposes. |
| 4 | * |
| 5 | * @class EVF_Logger |
| 6 | * @version 1.0.0 |
| 7 | * @package EverestForms/Classes |
| 8 | */ |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * EVF_Logger class |
| 14 | */ |
| 15 | class EVF_Logger implements EVF_Logger_Interface { |
| 16 | |
| 17 | /** |
| 18 | * Stores registered log handlers. |
| 19 | * |
| 20 | * @var array |
| 21 | */ |
| 22 | protected $handlers; |
| 23 | |
| 24 | /** |
| 25 | * Minimum log level this handler will process. |
| 26 | * |
| 27 | * @var int Integer representation of minimum log level to handle. |
| 28 | */ |
| 29 | protected $threshold; |
| 30 | |
| 31 | /** |
| 32 | * Constructor for the logger. |
| 33 | * |
| 34 | * @param array $handlers Optional. Array of log handlers. If $handlers is not provided, the filter 'everest_forms_register_log_handlers' will be used to define the handlers. If $handlers is provided, the filter will not be applied and the handlers will be used directly. |
| 35 | * @param string $threshold Optional. Define an explicit threshold. May be configured via EVF_LOG_THRESHOLD. By default, all logs will be processed. |
| 36 | */ |
| 37 | public function __construct( $handlers = null, $threshold = null ) { |
| 38 | if ( null === $handlers ) { |
| 39 | $handlers = apply_filters( 'everest_forms_register_log_handlers', array() ); |
| 40 | } |
| 41 | |
| 42 | $register_handlers = array(); |
| 43 | |
| 44 | if ( ! empty( $handlers ) && is_array( $handlers ) ) { |
| 45 | foreach ( $handlers as $handler ) { |
| 46 | $implements = class_implements( $handler ); |
| 47 | if ( is_object( $handler ) && is_array( $implements ) && in_array( 'EVF_Log_Handler_Interface', $implements, true ) ) { |
| 48 | $register_handlers[] = $handler; |
| 49 | } else { |
| 50 | evf_doing_it_wrong( |
| 51 | __METHOD__, |
| 52 | sprintf( |
| 53 | /* translators: 1: class name 2: EVF_Log_Handler_Interface */ |
| 54 | __( 'The provided handler %1$s does not implement %2$s.', 'everest-forms' ), |
| 55 | '<code>' . esc_html( is_object( $handler ) ? get_class( $handler ) : $handler ) . '</code>', |
| 56 | '<code>EVF_Log_Handler_Interface</code>' |
| 57 | ), |
| 58 | '1.0' |
| 59 | ); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if ( null !== $threshold ) { |
| 65 | $threshold = EVF_Log_Levels::get_level_severity( $threshold ); |
| 66 | } elseif ( defined( 'EVF_LOG_THRESHOLD' ) && EVF_Log_Levels::is_valid_level( EVF_LOG_THRESHOLD ) ) { |
| 67 | $threshold = EVF_Log_Levels::get_level_severity( EVF_LOG_THRESHOLD ); |
| 68 | } else { |
| 69 | $threshold = null; |
| 70 | } |
| 71 | |
| 72 | $this->handlers = $register_handlers; |
| 73 | $this->threshold = $threshold; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Determine whether to handle or ignore log. |
| 78 | * |
| 79 | * @param string $level emergency|alert|critical|error|warning|notice|info|debug. |
| 80 | * @return bool True if the log should be handled. |
| 81 | */ |
| 82 | protected function should_handle( $level ) { |
| 83 | if ( null === $this->threshold ) { |
| 84 | return true; |
| 85 | } |
| 86 | return $this->threshold <= EVF_Log_Levels::get_level_severity( $level ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Add a log entry. |
| 91 | * |
| 92 | * This is not the preferred method for adding log messages. Please use log() or any one of |
| 93 | * the level methods (debug(), info(), etc.). This method may be deprecated in the future. |
| 94 | * |
| 95 | * @param string $handle File handle. |
| 96 | * @param string $message Message to log. |
| 97 | * @param string $level Logging level. |
| 98 | * |
| 99 | * @return bool |
| 100 | */ |
| 101 | public function add( $handle, $message, $level = EVF_Log_Levels::NOTICE ) { |
| 102 | $message = apply_filters( 'everest_forms_logger_add_message', $message, $handle ); |
| 103 | $this->log( |
| 104 | $level, |
| 105 | $message, |
| 106 | array( |
| 107 | 'source' => $handle, |
| 108 | '_legacy' => true, |
| 109 | ) |
| 110 | ); |
| 111 | evf_do_deprecated_action( 'everest_forms_log_add', array( $handle, $message ), '1.2', 'This action has been deprecated with no alternative.' ); |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Add a log entry. |
| 117 | * |
| 118 | * @param string $level One of the following: |
| 119 | * 'emergency': System is unusable. |
| 120 | * 'alert': Action must be taken immediately. |
| 121 | * 'critical': Critical conditions. |
| 122 | * 'error': Error conditions. |
| 123 | * 'warning': Warning conditions. |
| 124 | * 'notice': Normal but significant condition. |
| 125 | * 'info': Informational messages. |
| 126 | * 'debug': Debug-level messages. |
| 127 | * @param string $message Log message. |
| 128 | * @param array $context Optional. Additional information for log handlers. |
| 129 | */ |
| 130 | public function log( $level, $message, $context = array() ) { |
| 131 | // Check Log is disabled. |
| 132 | if ( 'no' === get_option( 'everest_forms_enable_log', 'no' ) ) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | if ( ! EVF_Log_Levels::is_valid_level( $level ) ) { |
| 137 | /* translators: 1: EVF_Logger::log 2: level */ |
| 138 | evf_doing_it_wrong( __METHOD__, sprintf( __( '%1$s was called with an invalid level "%2$s".', 'everest-forms' ), '<code>EVF_Logger::log</code>', $level ), '1.2' ); |
| 139 | } |
| 140 | |
| 141 | if ( $this->should_handle( $level ) ) { |
| 142 | $message = apply_filters( 'everest_forms_logger_log_message', $message, $level, $context ); |
| 143 | |
| 144 | foreach ( $this->handlers as $handler ) { |
| 145 | $handler->handle( time(), $level, $message, $context ); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Adds an emergency level message. |
| 152 | * |
| 153 | * System is unusable. |
| 154 | * |
| 155 | * @see EVF_Logger::log |
| 156 | * |
| 157 | * @param string $message Message to log. |
| 158 | * @param array $context Log context. |
| 159 | */ |
| 160 | public function emergency( $message, $context = array() ) { |
| 161 | $this->log( EVF_Log_Levels::EMERGENCY, $message, $context ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Adds an alert level message. |
| 166 | * |
| 167 | * Action must be taken immediately. |
| 168 | * Example: Entire website down, database unavailable, etc. |
| 169 | * |
| 170 | * @see EVF_Logger::log |
| 171 | * |
| 172 | * @param string $message Message to log. |
| 173 | * @param array $context Log context. |
| 174 | */ |
| 175 | public function alert( $message, $context = array() ) { |
| 176 | $this->log( EVF_Log_Levels::ALERT, $message, $context ); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Adds a critical level message. |
| 181 | * |
| 182 | * Critical conditions. |
| 183 | * Example: Application component unavailable, unexpected exception. |
| 184 | * |
| 185 | * @see EVF_Logger::log |
| 186 | * |
| 187 | * @param string $message Message to log. |
| 188 | * @param array $context Log context. |
| 189 | */ |
| 190 | public function critical( $message, $context = array() ) { |
| 191 | $this->log( EVF_Log_Levels::CRITICAL, $message, $context ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Adds an error level message. |
| 196 | * |
| 197 | * Runtime errors that do not require immediate action but should typically be logged |
| 198 | * and monitored. |
| 199 | * |
| 200 | * @see EVF_Logger::log |
| 201 | * |
| 202 | * @param string $message Message to log. |
| 203 | * @param array $context Log context. |
| 204 | */ |
| 205 | public function error( $message, $context = array() ) { |
| 206 | $this->log( EVF_Log_Levels::ERROR, $message, $context ); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Adds a warning level message. |
| 211 | * |
| 212 | * Exceptional occurrences that are not errors. |
| 213 | * |
| 214 | * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not |
| 215 | * necessarily wrong. |
| 216 | * |
| 217 | * @see EVF_Logger::log |
| 218 | * |
| 219 | * @param string $message Message to log. |
| 220 | * @param array $context Log context. |
| 221 | */ |
| 222 | public function warning( $message, $context = array() ) { |
| 223 | $this->log( EVF_Log_Levels::WARNING, $message, $context ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Adds a notice level message. |
| 228 | * |
| 229 | * Normal but significant events. |
| 230 | * |
| 231 | * @see EVF_Logger::log |
| 232 | * |
| 233 | * @param string $message Message to log. |
| 234 | * @param array $context Log context. |
| 235 | */ |
| 236 | public function notice( $message, $context = array() ) { |
| 237 | $this->log( EVF_Log_Levels::NOTICE, $message, $context ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Adds a info level message. |
| 242 | * |
| 243 | * Interesting events. |
| 244 | * Example: User logs in, SQL logs. |
| 245 | * |
| 246 | * @see EVF_Logger::log |
| 247 | * |
| 248 | * @param string $message Message to log. |
| 249 | * @param array $context Log context. |
| 250 | */ |
| 251 | public function info( $message, $context = array() ) { |
| 252 | $this->log( EVF_Log_Levels::INFO, $message, $context ); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Adds a debug level message. |
| 257 | * |
| 258 | * Detailed debug information. |
| 259 | * |
| 260 | * @see EVF_Logger::log |
| 261 | * |
| 262 | * @param string $message Message to log. |
| 263 | * @param array $context Log context. |
| 264 | */ |
| 265 | public function debug( $message, $context = array() ) { |
| 266 | $this->log( EVF_Log_Levels::DEBUG, $message, $context ); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Clear entries from chosen file. |
| 271 | * |
| 272 | * @param string $source Source/handle to clear. |
| 273 | * @return bool |
| 274 | */ |
| 275 | public function clear( $source = '' ) { |
| 276 | if ( ! $source ) { |
| 277 | return false; |
| 278 | } |
| 279 | foreach ( $this->handlers as $handler ) { |
| 280 | if ( is_callable( array( $handler, 'clear' ) ) ) { |
| 281 | $handler->clear( $source ); |
| 282 | } |
| 283 | } |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Clear all logs older than a defined number of days. Defaults to 30 days. |
| 289 | * |
| 290 | * @since 1.6.2 |
| 291 | */ |
| 292 | public function clear_expired_logs() { |
| 293 | $days = absint( apply_filters( 'everest_forms_logger_days_to_retain_logs', 30 ) ); |
| 294 | $timestamp = strtotime( "-{$days} days" ); |
| 295 | |
| 296 | foreach ( $this->handlers as $handler ) { |
| 297 | if ( is_callable( array( $handler, 'delete_logs_before_timestamp' ) ) ) { |
| 298 | $handler->delete_logs_before_timestamp( $timestamp ); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 |