| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Logger; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Simple logger for Atomic Widgets that writes to wp-content/debug.log (if WP_DEBUG_LOG enabled |
| 11 |
* and WP_DEBUG_DISPLAY disabled) or optionally to Elementor's DB logger. |
| 12 |
* Never displays errors on screen. |
| 13 |
*/ |
| 14 |
class Logger { |
| 15 |
|
| 16 |
public static function info( string $message, array $context = [], bool $use_elementor_logger = false ): void { |
| 17 |
self::log_message( $message, $context, $use_elementor_logger, 'info' ); |
| 18 |
} |
| 19 |
|
| 20 |
public static function warning( string $message, array $context = [], bool $use_elementor_logger = false ): void { |
| 21 |
self::log_message( $message, $context, $use_elementor_logger, 'warning' ); |
| 22 |
} |
| 23 |
|
| 24 |
public static function error( string $message, array $context = [], bool $use_elementor_logger = false ): void { |
| 25 |
self::log_message( $message, $context, $use_elementor_logger, 'error' ); |
| 26 |
} |
| 27 |
|
| 28 |
private static function log_message( string $message, array $context, bool $use_elementor_logger, string $level ): void { |
| 29 |
if ( $use_elementor_logger ) { |
| 30 |
self::log_to_elementor_db( $message, $context, $level ); |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
self::log_to_wp_debug_file( $message, $context, $level ); |
| 35 |
} |
| 36 |
|
| 37 |
private static function log_to_wp_debug_file( string $message, array $context, string $level ): void { |
| 38 |
if ( ! self::should_log_to_file() ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
$formatted_message = self::format_message( $message, $context, $level ); |
| 43 |
error_log( $formatted_message ); |
| 44 |
} |
| 45 |
|
| 46 |
private static function log_to_elementor_db( string $message, array $context, string $level ): void { |
| 47 |
if ( ! isset( \Elementor\Plugin::$instance->logger ) ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
try { |
| 52 |
$logger = \Elementor\Plugin::$instance->logger; |
| 53 |
|
| 54 |
switch ( $level ) { |
| 55 |
case 'error': |
| 56 |
$logger->error( $message, $context ); |
| 57 |
break; |
| 58 |
case 'warning': |
| 59 |
$logger->warning( $message, $context ); |
| 60 |
break; |
| 61 |
default: |
| 62 |
$logger->info( $message, $context ); |
| 63 |
break; |
| 64 |
} |
| 65 |
} catch ( \Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- Logger must not throw exceptions |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
private static function should_log_to_file(): bool { |
| 70 |
$debug_log_enabled = defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG; |
| 71 |
$debug_display_disabled = ! defined( 'WP_DEBUG_DISPLAY' ) || ! WP_DEBUG_DISPLAY; |
| 72 |
|
| 73 |
return $debug_log_enabled && $debug_display_disabled; |
| 74 |
} |
| 75 |
|
| 76 |
private static function format_message( string $message, array $context, string $level ): string { |
| 77 |
$level_prefix = strtoupper( $level ); |
| 78 |
$formatted = "[Elementor Atomic Widgets] [{$level_prefix}] " . $message; |
| 79 |
|
| 80 |
if ( ! empty( $context ) ) { |
| 81 |
$context_json = wp_json_encode( $context, JSON_UNESCAPED_SLASHES ); |
| 82 |
|
| 83 |
if ( false !== $context_json ) { |
| 84 |
$formatted .= ' | Context: ' . $context_json; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return $formatted; |
| 89 |
} |
| 90 |
} |
| 91 |
|