$context Additional context to log. */ public function log( string $message, array $context = [] ) { if ( ! $this->should_log() ) { return; } error_log( $this->format( 'info', $message, $context ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log } /** * Logs a warning message. * * @param string $message The message to log. * @param array $context Additional context to log. */ public function warning( string $message, array $context = [] ) { if ( ! $this->should_log() ) { return; } error_log( $this->format( 'warning', $message, $context ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log } /** * Logs an error message. * * @param string $message The message to log. * @param array $context Additional context to log. */ public function error( string $message, array $context = [] ) { if ( ! $this->should_log() ) { return; } error_log( $this->format( 'error', $message, $context ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log } /** * Returns whether logging is enabled. */ protected function should_log(): bool { return defined( 'WP_DEBUG' ) && WP_DEBUG; } /** * Formats a log line. * * @param string $level The level of the log. * @param string $message The message to log. * @param array $context Additional context to log. */ protected function format( string $level, string $message, array $context = [] ): string { $line = sprintf( '[Admin Optimizer] %s: %s', strtoupper( $level ), $message ); if ( [] === $context ) { return $line; } $encoded = wp_json_encode( $context ); if ( ! is_string( $encoded ) || '' === $encoded ) { return $line; } return $line . ' ' . $encoded; } }