| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
class Logger { |
| 10 |
public const LEVEL_ERROR = 'error'; |
| 11 |
public const LEVEL_WARN = 'warn'; |
| 12 |
public const LEVEL_INFO = 'info'; |
| 13 |
|
| 14 |
public static function log( string $log_level, $message ): void { |
| 15 |
$backtrace = debug_backtrace(); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 16 |
|
| 17 |
$class = $backtrace[1]['class'] ?? null; |
| 18 |
$type = $backtrace[1]['type'] ?? null; |
| 19 |
$function = $backtrace[1]['function']; |
| 20 |
|
| 21 |
if ( $class ) { |
| 22 |
$message = '[Image Optimizer]: ' . $log_level . ' in ' . "$class$type$function()" . ': ' . $message; |
| 23 |
} else { |
| 24 |
$message = '[Image Optimizer]: ' . $log_level . ' in ' . "$function()" . ': ' . $message; |
| 25 |
} |
| 26 |
|
| 27 |
error_log( $message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 28 |
} |
| 29 |
} |
| 30 |
|