PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.6
Yatra – Travel Booking & Tour Operator Software v3.0.2.6
3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 All 83 releases
yatra / app / Utils / Logger.php

Logger.php in Yatra – Travel Booking & Tour Operator Software 3.0.2.6, at app/Utils/Logger.php

217 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Yatra\Utils;
6
7 /**
8 * Yatra Logger
9 *
10 * Centralized logging system with different log levels and contexts
11 */
12 class Logger
13 {
14 /**
15 * Log levels
16 */
17 public const EMERGENCY = 'emergency';
18 public const ALERT = 'alert';
19 public const CRITICAL = 'critical';
20 public const ERROR = 'error';
21 public const WARNING = 'warning';
22 public const NOTICE = 'notice';
23 public const INFO = 'info';
24 public const DEBUG = 'debug';
25
26 /**
27 * Log file path
28 */
29 private static ?string $logFile = null;
30
31 /**
32 * Initialize logger
33 */
34 public static function init(): void
35 {
36 if (self::$logFile === null) {
37 $upload_dir = wp_upload_dir();
38 $log_dir = $upload_dir['basedir'] . '/yatra-logs';
39
40 if (!file_exists($log_dir)) {
41 wp_mkdir_p($log_dir);
42 }
43
44 self::$logFile = $log_dir . '/yatra-' . date('Y-m-d') . '.log';
45 }
46 }
47
48 /**
49 * Log emergency message
50 */
51 public static function emergency(string $message, array $context = []): void
52 {
53 self::log(self::EMERGENCY, $message, $context);
54 }
55
56 /**
57 * Log alert message
58 */
59 public static function alert(string $message, array $context = []): void
60 {
61 self::log(self::ALERT, $message, $context);
62 }
63
64 /**
65 * Log critical message
66 */
67 public static function critical(string $message, array $context = []): void
68 {
69 self::log(self::CRITICAL, $message, $context);
70 }
71
72 /**
73 * Log error message
74 */
75 public static function error(string $message, array $context = []): void
76 {
77 self::log(self::ERROR, $message, $context);
78 }
79
80 /**
81 * Log warning message
82 */
83 public static function warning(string $message, array $context = []): void
84 {
85 self::log(self::WARNING, $message, $context);
86 }
87
88 /**
89 * Log notice message
90 */
91 public static function notice(string $message, array $context = []): void
92 {
93 self::log(self::NOTICE, $message, $context);
94 }
95
96 /**
97 * Log info message
98 */
99 public static function info(string $message, array $context = []): void
100 {
101 self::log(self::INFO, $message, $context);
102 }
103
104 /**
105 * Log debug message
106 */
107 public static function debug(string $message, array $context = []): void
108 {
109 if (defined('WP_DEBUG') && WP_DEBUG) {
110 self::log(self::DEBUG, $message, $context);
111 }
112 }
113
114 /**
115 * Log message with level
116 */
117 public static function log(string $level, string $message, array $context = []): void
118 {
119 self::init();
120
121 $timestamp = date('Y-m-d H:i:s');
122 $contextStr = !empty($context) ? ' | Context: ' . json_encode($context, JSON_UNESCAPED_SLASHES) : '';
123 $logEntry = "[{$timestamp}] [{$level}] {$message}{$contextStr}" . PHP_EOL;
124
125 // Write to file
126 file_put_contents(self::$logFile, $logEntry, FILE_APPEND | LOCK_EX);
127
128 // Also log to WordPress debug log for critical errors
129 if (in_array($level, [self::EMERGENCY, self::ALERT, self::CRITICAL, self::ERROR], true) && (defined('WP_DEBUG') && WP_DEBUG)) {
130 error_log('[Yatra][' . $level . '] ' . $message . $contextStr);
131 }
132 }
133
134 /**
135 * Log database query performance
136 */
137 public static function queryPerformance(string $query, float $executionTime, array $context = []): void
138 {
139 $context['execution_time'] = $executionTime;
140 $context['query'] = $query;
141
142 $level = $executionTime > 1.0 ? self::WARNING : self::DEBUG;
143 self::log($level, "Database query executed in {$executionTime}s", $context);
144 }
145
146 /**
147 * Log API request
148 */
149 public static function apiRequest(string $endpoint, string $method, array $data = [], ?float $responseTime = null): void
150 {
151 $context = [
152 'endpoint' => $endpoint,
153 'method' => $method,
154 'data_size' => count($data),
155 ];
156
157 if ($responseTime !== null) {
158 $context['response_time'] = $responseTime;
159 }
160
161 self::info("API request: {$method} {$endpoint}", $context);
162 }
163
164 /**
165 * Log validation errors
166 */
167 public static function validationError(string $entity, array $errors, array $data = []): void
168 {
169 $context = [
170 'entity' => $entity,
171 'errors' => $errors,
172 'data_keys' => array_keys($data),
173 ];
174
175 self::warning("Validation failed for {$entity}", $context);
176 }
177
178 /**
179 * Log business logic events
180 */
181 public static function businessEvent(string $event, array $context = []): void
182 {
183 self::info("Business event: {$event}", $context);
184 }
185
186 /**
187 * Get log file path
188 */
189 public static function getLogFile(): ?string
190 {
191 self::init();
192 return self::$logFile;
193 }
194
195 /**
196 * Clear old log files (keep last 30 days)
197 */
198 public static function cleanup(): void
199 {
200 $upload_dir = wp_upload_dir();
201 $log_dir = $upload_dir['basedir'] . '/yatra-logs';
202
203 if (!is_dir($log_dir)) {
204 return;
205 }
206
207 $files = glob($log_dir . '/yatra-*.log');
208 $cutoff = time() - (30 * 24 * 60 * 60); // 30 days ago
209
210 foreach ($files as $file) {
211 if (filemtime($file) < $cutoff) {
212 unlink($file);
213 }
214 }
215 }
216 }
217