PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.7
Yatra – Travel Booking & Tour Operator Software v3.0.2.7
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 / Services / LoggingService.php

LoggingService.php in Yatra – Travel Booking & Tour Operator Software 3.0.2.7, at app/Services/LoggingService.php

137 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Logging Service
4 *
5 * Handles application logging
6 *
7 * @package Yatra\Services
8 * @since 3.0.0
9 */
10
11 declare(strict_types=1);
12
13 namespace Yatra\Services;
14
15 class LoggingService
16 {
17 /**
18 * Log levels
19 */
20 const LEVEL_DEBUG = 'debug';
21 const LEVEL_INFO = 'info';
22 const LEVEL_WARNING = 'warning';
23 const LEVEL_ERROR = 'error';
24
25 /**
26 * Log a message
27 *
28 * @param string $message Log message
29 * @param string $level Log level
30 * @param array $context Additional context
31 */
32 public static function log(string $message, string $level = self::LEVEL_INFO, array $context = []): void
33 {
34 // Check if logging is enabled
35 if (!SettingsService::isEnabled('enable_logging')) {
36 return;
37 }
38
39 // Format log entry
40 $timestamp = current_time('Y-m-d H:i:s');
41 $context_str = !empty($context) ? ' | Context: ' . wp_json_encode($context) : '';
42 $log_entry = sprintf('[%s] [%s] %s%s', $timestamp, strtoupper($level), $message, $context_str);
43
44 // Write to WordPress debug log if WP_DEBUG_LOG is enabled
45 if (defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) {
46 error_log('Yatra: ' . $log_entry);
47 }
48
49 // Store in database for admin viewing
50 self::storeLog($message, $level, $context);
51 }
52
53 /**
54 * Log debug message
55 */
56 public static function debug(string $message, array $context = []): void
57 {
58 self::log($message, self::LEVEL_DEBUG, $context);
59 }
60
61 /**
62 * Log info message
63 */
64 public static function info(string $message, array $context = []): void
65 {
66 self::log($message, self::LEVEL_INFO, $context);
67 }
68
69 /**
70 * Log warning message
71 */
72 public static function warning(string $message, array $context = []): void
73 {
74 self::log($message, self::LEVEL_WARNING, $context);
75 }
76
77 /**
78 * Log error message
79 */
80 public static function error(string $message, array $context = []): void
81 {
82 self::log($message, self::LEVEL_ERROR, $context);
83 }
84
85 /**
86 * Store log in database
87 */
88 private static function storeLog(string $message, string $level, array $context): void
89 {
90 $logs = get_option('yatra_logs', []);
91
92 $logs[] = [
93 'timestamp' => current_time('mysql'),
94 'level' => $level,
95 'message' => $message,
96 'context' => $context,
97 ];
98
99 // Keep only last 1000 logs
100 if (count($logs) > 1000) {
101 $logs = array_slice($logs, -1000);
102 }
103
104 update_option('yatra_logs', $logs);
105 }
106
107 /**
108 * Get logs
109 *
110 * @param int $limit Number of logs to retrieve
111 * @param string|null $level Filter by level
112 * @return array
113 */
114 public static function getLogs(int $limit = 100, ?string $level = null): array
115 {
116 $logs = get_option('yatra_logs', []);
117
118 // Filter by level if specified
119 if ($level) {
120 $logs = array_filter($logs, function($log) use ($level) {
121 return $log['level'] === $level;
122 });
123 }
124
125 // Return last N logs
126 return array_slice(array_reverse($logs), 0, $limit);
127 }
128
129 /**
130 * Clear logs
131 */
132 public static function clearLogs(): void
133 {
134 delete_option('yatra_logs');
135 }
136 }
137