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

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

132 lines 3.2 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 // Store in database for admin viewing
45 self::storeLog($message, $level, $context);
46 }
47
48 /**
49 * Log debug message
50 */
51 public static function debug(string $message, array $context = []): void
52 {
53 self::log($message, self::LEVEL_DEBUG, $context);
54 }
55
56 /**
57 * Log info message
58 */
59 public static function info(string $message, array $context = []): void
60 {
61 self::log($message, self::LEVEL_INFO, $context);
62 }
63
64 /**
65 * Log warning message
66 */
67 public static function warning(string $message, array $context = []): void
68 {
69 self::log($message, self::LEVEL_WARNING, $context);
70 }
71
72 /**
73 * Log error message
74 */
75 public static function error(string $message, array $context = []): void
76 {
77 self::log($message, self::LEVEL_ERROR, $context);
78 }
79
80 /**
81 * Store log in database
82 */
83 private static function storeLog(string $message, string $level, array $context): void
84 {
85 $logs = get_option('yatra_logs', []);
86
87 $logs[] = [
88 'timestamp' => current_time('mysql'),
89 'level' => $level,
90 'message' => $message,
91 'context' => $context,
92 ];
93
94 // Keep only last 1000 logs
95 if (count($logs) > 1000) {
96 $logs = array_slice($logs, -1000);
97 }
98
99 update_option('yatra_logs', $logs);
100 }
101
102 /**
103 * Get logs
104 *
105 * @param int $limit Number of logs to retrieve
106 * @param string|null $level Filter by level
107 * @return array
108 */
109 public static function getLogs(int $limit = 100, ?string $level = null): array
110 {
111 $logs = get_option('yatra_logs', []);
112
113 // Filter by level if specified
114 if ($level) {
115 $logs = array_filter($logs, function($log) use ($level) {
116 return $log['level'] === $level;
117 });
118 }
119
120 // Return last N logs
121 return array_slice(array_reverse($logs), 0, $limit);
122 }
123
124 /**
125 * Clear logs
126 */
127 public static function clearLogs(): void
128 {
129 delete_option('yatra_logs');
130 }
131 }
132