PluginProbe
Yatra – Travel Booking & Tour Operator Software / trunk
Yatra – Travel Booking & Tour Operator Software vtrunk
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 2.0.11 All 82 releases
yatra / app / Utils / Logger.php

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

216 lines 5.4 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 }
131 }
132
133 /**
134 * Log database query performance
135 */
136 public static function queryPerformance(string $query, float $executionTime, array $context = []): void
137 {
138 $context['execution_time'] = $executionTime;
139 $context['query'] = $query;
140
141 $level = $executionTime > 1.0 ? self::WARNING : self::DEBUG;
142 self::log($level, "Database query executed in {$executionTime}s", $context);
143 }
144
145 /**
146 * Log API request
147 */
148 public static function apiRequest(string $endpoint, string $method, array $data = [], ?float $responseTime = null): void
149 {
150 $context = [
151 'endpoint' => $endpoint,
152 'method' => $method,
153 'data_size' => count($data),
154 ];
155
156 if ($responseTime !== null) {
157 $context['response_time'] = $responseTime;
158 }
159
160 self::info("API request: {$method} {$endpoint}", $context);
161 }
162
163 /**
164 * Log validation errors
165 */
166 public static function validationError(string $entity, array $errors, array $data = []): void
167 {
168 $context = [
169 'entity' => $entity,
170 'errors' => $errors,
171 'data_keys' => array_keys($data),
172 ];
173
174 self::warning("Validation failed for {$entity}", $context);
175 }
176
177 /**
178 * Log business logic events
179 */
180 public static function businessEvent(string $event, array $context = []): void
181 {
182 self::info("Business event: {$event}", $context);
183 }
184
185 /**
186 * Get log file path
187 */
188 public static function getLogFile(): ?string
189 {
190 self::init();
191 return self::$logFile;
192 }
193
194 /**
195 * Clear old log files (keep last 30 days)
196 */
197 public static function cleanup(): void
198 {
199 $upload_dir = wp_upload_dir();
200 $log_dir = $upload_dir['basedir'] . '/yatra-logs';
201
202 if (!is_dir($log_dir)) {
203 return;
204 }
205
206 $files = glob($log_dir . '/yatra-*.log');
207 $cutoff = time() - (30 * 24 * 60 * 60); // 30 days ago
208
209 foreach ($files as $file) {
210 if (filemtime($file) < $cutoff) {
211 unlink($file);
212 }
213 }
214 }
215 }
216