PluginProbe
seQura / 4.0.0
seQura v4.0.0
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
sequra / src / Services / Log / class-logger-service.php

class-logger-service.php in seQura 4.0.0, at src/Services/Log/class-logger-service.php

195 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 * Logger service
4 *
5 * @package SeQura/WC
6 * @subpackage SeQura/WC/Services
7 */
8
9 namespace SeQura\WC\Services\Log;
10
11 use Exception;
12 use SeQura\Core\Infrastructure\Logger\LogContextData;
13 use SeQura\Core\Infrastructure\Logger\Logger;
14 use SeQura\Core\Infrastructure\Logger\LoggerConfiguration;
15 use Throwable;
16
17 /**
18 * Logger service
19 */
20 class Logger_Service implements Interface_Logger_Service {
21
22 /**
23 * Logger configuration.
24 *
25 * @var LoggerConfiguration
26 */
27 private $configuration;
28
29 /**
30 * The log file.
31 *
32 * @var Interface_Log_File
33 */
34 private $log_file;
35
36 /**
37 * Constructor.
38 *
39 * @param LoggerConfiguration $configuration The logger configuration.
40 * @param Interface_Log_File $log_file The log file.
41 */
42 public function __construct( LoggerConfiguration $configuration, Interface_Log_File $log_file ) {
43 $this->configuration = $configuration;
44 $this->log_file = $log_file;
45 }
46
47 /**
48 * Get the content of the log file.
49 *
50 * @param string $store_id The store ID.
51 * @throws \Exception If something goes wrong. The exception message will contain the error message.
52 *
53 * @return string[] Each element is a line of the log file.
54 */
55 public function get_content( $store_id ): array {
56 return $this->log_file->get_content( $store_id );
57 }
58
59 /**
60 * Clear the log file.
61 *
62 * @param string $store_id The store ID.
63 * @throws \Exception If something goes wrong. The exception message will contain the error message.
64 */
65 public function clear( $store_id ): void {
66 $this->log_file->clear( $store_id );
67 }
68
69 /**
70 * Log a message with the severity "WARNING".
71 *
72 * @param string $message The message to log.
73 * @param string|null $func The method name.
74 * @param string|null $class_name The class name.
75 * @param LogContextData[] $context The context.
76 */
77 public function log_warning( $message, $func = null, $class_name = null, $context = array() ): void {
78 Logger::logWarning( $this->format_msg( $message, $func, $class_name ), 'Plugin', $context );
79 }
80
81 /**
82 * Log a message with the severity "INFO".
83 *
84 * @param string $message The message to log.
85 * @param string|null $func The method name.
86 * @param string|null $class_name The class name.
87 * @param LogContextData[] $context The context.
88 */
89 public function log_info( $message, $func = null, $class_name = null, $context = array() ): void {
90 Logger::logInfo( $this->format_msg( $message, $func, $class_name ), 'Plugin', $context );
91 }
92
93 /**
94 * Log a message with the severity "DEBUG".
95 *
96 * @param string $message The message to log.
97 * @param string|null $func The method name.
98 * @param string|null $class_name The class name.
99 * @param LogContextData[] $context The context.
100 */
101 public function log_debug( $message, $func = null, $class_name = null, $context = array() ): void {
102 Logger::logDebug( $this->format_msg( $message, $func, $class_name ), 'Plugin', $context );
103 }
104
105 /**
106 * Log a message with the severity "ERROR".
107 *
108 * @param string $message The message to log.
109 * @param string|null $func The method name.
110 * @param string|null $class_name The class name.
111 * @param LogContextData[] $context The context.
112 */
113 public function log_error( $message, $func = null, $class_name = null, $context = array() ): void {
114 Logger::logError( $this->format_msg( $message, $func, $class_name ), 'Plugin', $context );
115 }
116
117 /**
118 * Log a message with the severity "ERROR".
119 *
120 * @param Throwable $throwable The throwable to log.
121 * @param string|null $func The method name.
122 * @param string|null $class_name The class name.
123 * @param LogContextData[] $context Additional context.
124 */
125 public function log_throwable( Throwable $throwable, $func = null, $class_name = null, $context = array() ): void {
126 $message = get_class( $throwable );
127 $error_context = array(
128 new LogContextData( 'message', $throwable->getMessage() ),
129 new LogContextData( 'code', $throwable->getCode() ),
130 new LogContextData( 'file', $throwable->getFile() ),
131 new LogContextData( 'line', $throwable->getLine() ),
132 new LogContextData( 'trace', $throwable->getTraceAsString() ),
133 );
134 Logger::logError( $this->format_msg( $message, $func, $class_name ), 'Plugin', array_merge( $context, $error_context ) );
135 }
136
137 /**
138 * Check if logging is enabled.
139 */
140 public function is_enabled(): bool {
141 return $this->configuration->isDefaultLoggerEnabled();
142 }
143
144 /**
145 * Set on/off the logger.
146 *
147 * @param bool $is_enabled True to enable the logger, false to disable it.
148 */
149 public function enable( $is_enabled ): void {
150 $this->configuration->setDefaultLoggerEnabled( $is_enabled );
151 }
152
153 /**
154 * Get the minimum log level.
155 */
156 public function get_min_log_level(): int {
157 return $this->configuration->getMinLogLevel();
158 }
159
160 /**
161 * Set the minimum log level.
162 *
163 * @param int $level The minimum log level.
164 */
165 public function set_min_log_level( $level ): void {
166 $this->configuration->setMinLogLevel( $level );
167 }
168
169 /**
170 * Helper function to format the message.
171 *
172 * @param string $msg The message to log.
173 * @param string $func The method name.
174 * @param string $class_name_name The class name.
175 *
176 * @return string
177 */
178 private function format_msg( $msg, $func = null, $class_name_name = null ) {
179 $message = '';
180
181 if ( ! empty( $func ) ) {
182 $message .= $func . '()';
183 }
184 if ( ! empty( $class_name_name ) ) {
185 $message = $class_name_name . '::' . $message;
186 }
187
188 if ( ! empty( $message ) ) {
189 $message .= ' - ';
190 }
191
192 return $message . $msg;
193 }
194 }
195