PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.6
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.6
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / elementor / wp-one-package / src / Logger.php

Logger.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.6, at vendor/elementor/wp-one-package/src/Logger.php

113 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ElementorOne;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly.
7 }
8
9 /**
10 * Class Logger
11 */
12 class Logger {
13
14 public const LEVEL_ERROR = 'error';
15 public const LEVEL_WARN = 'warn';
16 public const LEVEL_INFO = 'info';
17
18 /**
19 * @var string Prefix for log messages (e.g., app name)
20 */
21 private string $prefix;
22
23 /**
24 * Constructor
25 *
26 * @param string $prefix Prefix for log messages (e.g., 'Elementor One', 'Elementor AI')
27 */
28 public function __construct( string $prefix ) {
29 $this->prefix = $prefix;
30 }
31
32 /**
33 * Log a message
34 *
35 * @param string $log_level Log level
36 * @param mixed $message Message to log
37 * @return void
38 */
39 private function log( string $log_level, $message ): void {
40 $raw_message = $this->message_to_string( $message );
41 $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
42
43 $caller = $this->caller_from_backtrace( $backtrace );
44
45 $formatted_message = '[' . $this->prefix . ']: ' . $log_level . ' in ' . $caller . ': ' . $raw_message;
46
47 error_log( $formatted_message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
48
49 LogStore::instance()->append(
50 $log_level,
51 $raw_message,
52 [
53 'source' => $this->prefix,
54 'call_stack' => $caller,
55 ]
56 );
57 }
58
59 /**
60 * @param mixed $message
61 * @return string
62 */
63 private function message_to_string( $message ): string {
64 if ( is_string( $message ) ) {
65 return $message;
66 }
67
68 if ( is_scalar( $message ) ) {
69 return (string) $message;
70 }
71
72 $encoded = wp_json_encode( $message );
73
74 return is_string( $encoded ) ? $encoded : '';
75 }
76
77 /**
78 * @param array<int, array<string, mixed>> $backtrace
79 * @return string
80 */
81 private function caller_from_backtrace( array $backtrace ): string {
82 $class = $backtrace[2]['class'] ?? null;
83 $type = $backtrace[2]['type'] ?? null;
84 $function = $backtrace[2]['function'] ?? '';
85
86 if ( $class ) {
87 return "$class$type$function()";
88 }
89
90 return "$function()";
91 }
92
93 /**
94 * Log an error message
95 *
96 * @param mixed $message Message to log
97 * @return void
98 */
99 public function error( $message ): void {
100 $this->log( self::LEVEL_ERROR, $message );
101 }
102
103 /**
104 * Log an info message
105 *
106 * @param mixed $message Message to log
107 * @return void
108 */
109 public function info( $message ): void {
110 $this->log( self::LEVEL_INFO, $message );
111 }
112 }
113