debug( $message, $context ) : null; } /** * Adds a log record at the INFO level. * * @since 3.1.4 * * @param string $message The log message. * @param array $context The log context. * @return bool|null Whether the record has been processed. */ public static function info( $message, array $context = [] ) { return static::debug_enabled() ? static::get_logger()->info( $message, $context ) : null; } /** * Adds a log record at the NOTICE level. * * @since 3.1.4 * * @param string $message The log message. * @param array $context The log context. * @return bool|null Whether the record has been processed. */ public static function notice( $message, array $context = [] ) { return static::debug_enabled() ? static::get_logger()->notice( $message, $context ) : null; } /** * Adds a log record at the WARNING level. * * @since 3.1.4 * * @param string $message The log message. * @param array $context The log context. * @return bool|null Whether the record has been processed. */ public static function warning( $message, array $context = [] ) { return static::debug_enabled() ? static::get_logger()->warning( $message, $context ) : null; } /** * Adds a log record at the ERROR level. * * @since 3.1.4 * * @param string $message The log message. * @param array $context The log context. * @return bool|null Whether the record has been processed. */ public static function error( $message, array $context = [] ) { return static::debug_enabled() ? static::get_logger()->error( $message, $context ) : null; } /** * Adds a log record at the CRITICAL level. * * @since 3.1.4 * * @param string $message The log message. * @param array $context The log context. * @return bool|null Whether the record has been processed. */ public static function critical( $message, array $context = [] ) { return static::debug_enabled() ? static::get_logger()->critical( $message, $context ) : null; } /** * Adds a log record at the ALERT level. * * @since 3.1.4 * * @param string $message The log message. * @param array $context The log context. * @return bool|null Whether the record has been processed. */ public static function alert( $message, array $context = [] ) { return static::debug_enabled() ? static::get_logger()->alert( $message, $context ) : null; } /** * Adds a log record at the EMERGENCY level. * * @since 3.1.4 * * @param string $message The log message. * @param array $context The log context. * @return bool|null Whether the record has been processed. */ public static function emergency( $message, array $context = [] ) { return static::debug_enabled() ? static::get_logger()->emergency( $message, $context ) : null; } /** * Get the logger instance. * * @since 3.1.4 * * @return Logger A Logger instance. */ public static function get_logger() { $logger_name = static::LOGGER_NAME; $log_level = Monologger::DEBUG; if ( Registry::hasLogger( $logger_name ) ) { return Registry::$logger_name(); } /** * File handler. * HTML formatter is used. */ $handler = new StreamHandler( static::get_log_file_path(), $log_level ); $formatter = new HtmlFormatter(); $handler->setFormatter( $formatter ); /** * Thanks to the processors, add data to each log: * - `debug_backtrace()` (exclude this class and Abstract_Buffer). */ $trace_processor = new IntrospectionProcessor( $log_level, [ get_called_class(), 'Abstract_Buffer' ] ); // Create the logger. $logger = new Monologger( $logger_name, [ $handler ], [ $trace_processor ] ); // Store the logger. Registry::addLogger( $logger ); return $logger; } /** ----------------------------------------------------------------------------------------- */ /** LOG FILE ================================================================================ */ /** ----------------------------------------------------------------------------------------- */ /** * Get the path to the log file. * * @since 3.1.4 * * @return string */ public static function get_log_file_path() { if ( defined( 'WP_ROCKET_DEBUG_LOG_FILE' ) && WP_ROCKET_DEBUG_LOG_FILE && is_string( WP_ROCKET_DEBUG_LOG_FILE ) ) { // Make sure the file uses a ".log" extension. return preg_replace( '/\.[^.]*$/', '', WP_ROCKET_DEBUG_LOG_FILE ) . '.log'; } if ( defined( 'WP_ROCKET_DEBUG_INTERVAL' ) ) { // Adds an optional logs rotator depending on a constant value - WP_ROCKET_DEBUG_INTERVAL (interval by minutes). $rotator = str_pad( round( ( strtotime( 'now' ) - strtotime( 'today midnight' ) ) / 60 / WP_ROCKET_DEBUG_INTERVAL ), 4, '0', STR_PAD_LEFT ); return WP_CONTENT_DIR . '/wp-rocket-config/' . $rotator . '-' . static::LOG_FILE_NAME; } else { return WP_CONTENT_DIR . '/wp-rocket-config/' . static::LOG_FILE_NAME; } } /** * Get the log file contents. * * @since 3.1.4 * * @return string|object The file contents on success. A WP_Error object on failure. */ public static function get_log_file_contents() { $filesystem = \rocket_direct_filesystem(); $file_path = static::get_log_file_path(); if ( ! $filesystem->exists( $file_path ) ) { return new \WP_Error( 'no_file', __( 'The log file does not exist.', 'rocket' ) ); } $contents = $filesystem->get_contents( $file_path ); if ( false === $contents ) { return new \WP_Error( 'file_not_read', __( 'The log file could not be read.', 'rocket' ) ); } return $contents; } /** * Get the log file size and number of entries. * * @since 3.1.4 * * @return array|object An array of statistics on success. A WP_Error object on failure. */ public static function get_log_file_stats() { $formatter = static::get_stream_formatter(); if ( ! $formatter ) { return new \WP_Error( 'no_stream_formatter', __( 'The logs are not saved into a file.', 'rocket' ) ); } $filesystem = \rocket_direct_filesystem(); $file_path = static::get_log_file_path(); if ( ! $filesystem->exists( $file_path ) ) { return new \WP_Error( 'no_file', __( 'The log file does not exist.', 'rocket' ) ); } $contents = $filesystem->get_contents( $file_path ); if ( false === $contents ) { return new \WP_Error( 'file_not_read', __( 'The log file could not be read.', 'rocket' ) ); } if ( $formatter instanceof HtmlFormatter ) { $entries = preg_split( '@