PluginProbe
seQura / 2.0.11
seQura v2.0.11
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 / class-sequralogger.php

class-sequralogger.php in seQura 2.0.11, at class-sequralogger.php

226 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sequra Logger class.
4 *
5 * @package woocommerce-sequra
6 */
7
8 // phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_error_log
9 /**
10 * Sequra Logger class.
11 */
12 class SequraLogger {
13
14 private const DEBUG = 0;
15 private const INFO = 1;
16 private const WARNING = 2;
17 private const ERROR = 3;
18
19 private const LEVEL_MAP = array(
20 self::DEBUG => 'DEBUG',
21 self::INFO => 'INFO',
22 self::WARNING => 'WARNING',
23 self::ERROR => 'ERROR',
24 );
25
26 /**
27 * The path to the log file.
28 *
29 * @var string
30 */
31 private $log_file_path;
32
33 /**
34 * Constructor.
35 */
36 public function __construct() {
37 $this->log_file_path = WC_SEQURA_PLG_PATH . 'sequra.log';
38 }
39
40 /**
41 * Make sure the log file exists and is writable.
42 *
43 * @return bool
44 */
45 private function setup() {
46 if ( ! file_exists( $this->log_file_path ) ) {
47 $dir = dirname( $this->log_file_path );
48 if ( ! is_dir( $dir ) && file_exists( $dir ) ) {
49 return false;
50 }
51 // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.directory_mkdir
52 if ( ! file_exists( $dir ) && ! mkdir( $dir, 0755, true ) ) {
53 return false;
54 }
55
56 if ( ! file_put_contents( $this->get_formatted_message( 'Log file created' ), $this->log_file_path ) ) { // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_file_put_contents
57 return false;
58 }
59 }
60
61 if ( ! is_writable( dirname( $this->log_file_path ) ) ) { // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_is_writable
62 return false;
63 }
64
65 return true;
66 }
67
68 /**
69 * Get the content of the log file.
70 *
71 * @throws \Exception If something goes wrong. The exception message will contain the error message.
72 *
73 * @return string The content of the log file
74 */
75 public function get_log_content() {
76 if ( ! $this->setup() ) {
77 throw new \Exception( 'Could not setup log file.' );
78 }
79
80 $content = file_get_contents( $this->log_file_path ); // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown
81 if ( false === $content ) {
82 throw new \Exception( 'Could not read log file.' );
83 }
84
85 return $content;
86 }
87
88 /**
89 * Clear the log file.
90 *
91 * @throws \Exception If something goes wrong. The exception message will contain the error message.
92 */
93 public function clear_log() {
94 if ( file_exists( $this->log_file_path ) && ! unlink( $this->log_file_path ) ) { // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_unlink
95 throw new \Exception( 'Could not clear log file.' );
96 }
97 if ( ! $this->setup() ) {
98 throw new \Exception( 'Could not setup log file.' );
99 }
100 }
101
102 /**
103 * Log a message with the severity "WARNING".
104 *
105 * @param string $message The message to log.
106 * @param string|null $func The method name.
107 * @param string|null $class_name The class name.
108 */
109 public function log_warning( $message, $func = null, $class_name = null ) {
110 $this->log( $message, $func, $class_name, self::WARNING );
111 }
112
113 /**
114 * Log a message with the severity "INFO".
115 *
116 * @param string $message The message to log.
117 * @param string|null $func The method name.
118 * @param string|null $class_name The class name.
119 */
120 public function log_info( $message, $func = null, $class_name = null ) {
121 $this->log( $message, $func, $class_name, self::INFO );
122 }
123
124 /**
125 * Log a message with the severity "DEBUG".
126 *
127 * @param string $message The message to log.
128 * @param string|null $func The method name.
129 * @param string|null $class_name The class name.
130 */
131 public function log_debug( $message, $func = null, $class_name = null ) {
132 $this->log( $message, $func, $class_name, self::DEBUG );
133 }
134
135 /**
136 * Log a message with the severity "ERROR".
137 *
138 * @param string $message The message to log.
139 * @param string|null $func The method name.
140 * @param string|null $class_name The class name.
141 */
142 public function log_error( $message, $func = null, $class_name = null ) {
143 $this->log( $message, $func, $class_name, self::ERROR );
144 }
145
146 /**
147 * Log a message with the severity "ERROR".
148 *
149 * @param string $message The message to log.
150 * @param string|null $func The method name.
151 * @param string|null $class_name The class name.
152 * @param int $level The log level. Use self::DEBUG, self::INFO, self::WARNING, self::ERROR.
153 * @throws \Exception If something goes wrong. The exception message will contain the error message.
154 */
155 private function log( $message, $func = null, $class_name = null, $level = self::DEBUG ) {
156 if ( ! $this->is_log_enabled() ) {
157 return;
158 }
159 if ( $this->setup() ) {
160 $formatted_message = $this->get_formatted_message( $message, $func, $class_name, $level );
161 file_put_contents( $this->log_file_path, $formatted_message, FILE_APPEND ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_file_put_contents
162 }
163 }
164 /**
165 * Get the formatted message.
166 *
167 * @param mixed $message Message to log.
168 * @param mixed $func Function name.
169 * @param mixed $class_name Class name.
170 * @param int $level Debug level.
171 *
172 * @return string
173 */
174 private function get_formatted_message( $message, $func = null, $class_name = null, $level = self::DEBUG ) {
175 return sprintf(
176 "*%s*\tv%s\t%s: %s\r\n",// phpcs:ignore
177 self::LEVEL_MAP[ $level ],
178 get_bloginfo( 'version' ),
179 date( 'Y-m-d H:i:s' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
180 $this->format_msg( $message, $func, $class_name )
181 );
182 }
183
184 /**
185 * Check if the log is enabled.
186 *
187 * @return bool
188 */
189 private function is_log_enabled() {
190 // Move this responsibility to a settings service when proper class autoloading is working.
191 // Current implementation of loading classes by using require_once might be problematic.
192 $opts = (array) get_option( 'woocommerce_sequra_settings', false );
193 return isset( $opts['debug'] ) && 'yes' === $opts['debug'];
194 }
195
196 /**
197 * Helper function to format the message.
198 *
199 * @param string $msg The message to log.
200 * @param string $func The method name.
201 * @param string $class_name_name The class name.
202 *
203 * @return string
204 */
205 private function format_msg( $msg, $func = null, $class_name_name = null ) {
206 $message = '';
207
208 if ( ! empty( $func ) ) {
209 $message .= $func . '()';
210 }
211 if ( ! empty( $class_name_name ) ) {
212 $message = $class_name_name . '::' . $message;
213 }
214
215 if ( ! empty( $message ) ) {
216 $message .= ' - ';
217 }
218
219 if ( is_array( $msg ) || is_object( $msg ) ) {
220 $msg = wp_json_encode( $msg );
221 }
222
223 return $message . $msg;
224 }
225 }
226