11
11
*/
12
12
class WC_Stripe_Logger {
13
13
14
- public static $ logger ;
15
14
const WC_LOG_FILENAME = 'woocommerce-gateway-stripe ' ;
16
15
16
+ const LOG_CONTEXT = [
17
+ 'source ' => self ::WC_LOG_FILENAME ,
18
+ 'stripe_version ' => WC_STRIPE_VERSION ,
19
+ 'stripe_api_version ' => WC_Stripe_API::STRIPE_API_VERSION ,
20
+ ];
21
+
22
+ /**
23
+ * Log handler instance.
24
+ *
25
+ * @see https://developer.wordpress.org/reference/classes/wp_logger/
26
+ * @see https://developer.woocommerce.com/docs/best-practices/data-management/logging/#log-handlers
27
+ *
28
+ * @var WC_Logger
29
+ */
30
+ public static $ logger ;
31
+
17
32
/**
18
33
* Utilize WC logger class
19
34
*
35
+ * @deprecated 9.7.0 Use the shortcut methods for each log severity level: info(), error(), etc. instead.
36
+ *
20
37
* @since 4.0.0
21
- * @version 4.0.0
22
38
*/
23
39
public static function log ( $ message , $ start_time = null , $ end_time = null ) {
24
40
if ( ! self ::can_log () ) {
@@ -52,13 +68,99 @@ public static function log( $message, $start_time = null, $end_time = null ) {
52
68
self ::$ logger ->debug ( $ log_entry , [ 'source ' => self ::WC_LOG_FILENAME ] );
53
69
}
54
70
71
+ // Logs have eight different severity levels:
72
+ // - emergency
73
+ // - alert
74
+ // - critical
75
+ // - error
76
+ // - warning
77
+ // - notice
78
+ // - info
79
+ // - debug
80
+
81
+ /**
82
+ * Creates a log entry of type emergency.
83
+ *
84
+ * @since 9.7.0
85
+ *
86
+ * @param string $message Message to send to the log file.
87
+ * @param array $context Additional context to add to the log.
88
+ *
89
+ * @return void
90
+ */
91
+ public static function emergency ( $ message , $ context = [] ) {
92
+ if ( empty ( self ::$ logger ) ) {
93
+ self ::$ logger = wc_get_logger ();
94
+ }
95
+
96
+ self ::$ logger ->emergency ( $ message , array_merge ( self ::LOG_CONTEXT , $ context , [ 'backtrace ' => true ] ) );
97
+ }
98
+
99
+ /**
100
+ * Creates a log entry of type alert.
101
+ *
102
+ * @since 9.7.0
103
+ *
104
+ * @param string $message Message to send to the log file.
105
+ * @param array $context Additional context to add to the log.
106
+ *
107
+ * @return void
108
+ */
109
+ public static function alert ( $ message , $ context = [] ) {
110
+ if ( empty ( self ::$ logger ) ) {
111
+ self ::$ logger = wc_get_logger ();
112
+ }
113
+
114
+ self ::$ logger ->alert ( $ message , array_merge ( self ::LOG_CONTEXT , $ context , [ 'backtrace ' => true ] ) );
115
+ }
116
+
117
+ /**
118
+ * Creates a log entry of type critical.
119
+ *
120
+ * @since 9.7.0
121
+ *
122
+ * @param string $message Message to send to the log file.
123
+ * @param array $context Additional context to add to the log.
124
+ *
125
+ * @return void
126
+ */
127
+ public static function critical ( $ message , $ context = [] ) {
128
+ if ( empty ( self ::$ logger ) ) {
129
+ self ::$ logger = wc_get_logger ();
130
+ }
131
+
132
+ self ::$ logger ->critical ( $ message , array_merge ( self ::LOG_CONTEXT , $ context , [ 'backtrace ' => true ] ) );
133
+ }
134
+
55
135
/**
56
136
* Creates a log entry of type error.
57
137
*
58
- * @param string $message To send to the log file.
138
+ * @since 4.0.0
139
+ *
140
+ * @param string $message Message to send to the log file.
141
+ * @param array $context Additional context to add to the log.
142
+ *
59
143
* @return void
60
144
*/
61
- public static function error ( $ message ) {
145
+ public static function error ( $ message , $ context = [] ) {
146
+ if ( empty ( self ::$ logger ) ) {
147
+ self ::$ logger = wc_get_logger ();
148
+ }
149
+
150
+ self ::$ logger ->error ( $ message , array_merge ( self ::LOG_CONTEXT , $ context , [ 'backtrace ' => true ] ) );
151
+ }
152
+
153
+ /**
154
+ * Creates a log entry of type warning.
155
+ *
156
+ * @since 9.7.0
157
+ *
158
+ * @param string $message Message to send to the log file.
159
+ * @param array $context Additional context to add to the log.
160
+ *
161
+ * @return void
162
+ */
163
+ public static function warning ( $ message , $ context = [] ) {
62
164
if ( ! self ::can_log () ) {
63
165
return ;
64
166
}
@@ -67,16 +169,64 @@ public static function error( $message ) {
67
169
self ::$ logger = wc_get_logger ();
68
170
}
69
171
70
- self ::$ logger ->error ( $ message , [ 'source ' => self ::WC_LOG_FILENAME ] );
172
+ self ::$ logger ->warning ( $ message , array_merge ( self ::LOG_CONTEXT , $ context , [ 'backtrace ' => true ] ) );
173
+ }
174
+
175
+ /**
176
+ * Creates a log entry of type notice.
177
+ *
178
+ * @since 9.7.0
179
+ *
180
+ * @param string $message Message to send to the log file.
181
+ * @param array $context Additional context to add to the log.
182
+ *
183
+ * @return void
184
+ */
185
+ public static function notice ( $ message , $ context = [] ) {
186
+ if ( ! self ::can_log () ) {
187
+ return ;
188
+ }
189
+
190
+ if ( empty ( self ::$ logger ) ) {
191
+ self ::$ logger = wc_get_logger ();
192
+ }
193
+
194
+ self ::$ logger ->notice ( $ message , array_merge ( self ::LOG_CONTEXT , $ context ) );
195
+ }
196
+
197
+ /**
198
+ * Creates a log entry of type info.
199
+ *
200
+ * @since 9.7.0
201
+ *
202
+ * @param string $message Message to send to the log file.
203
+ * @param array $context Additional context to add to the log.
204
+ *
205
+ * @return void
206
+ */
207
+ public static function info ( $ message , $ context = [] ) {
208
+ if ( ! self ::can_log () ) {
209
+ return ;
210
+ }
211
+
212
+ if ( empty ( self ::$ logger ) ) {
213
+ self ::$ logger = wc_get_logger ();
214
+ }
215
+
216
+ self ::$ logger ->info ( $ message , array_merge ( self ::LOG_CONTEXT , $ context ) );
71
217
}
72
218
73
219
/**
74
220
* Creates a log entry of type debug.
75
221
*
76
- * @param string $message To send to the log file.
222
+ * @since 4.0.0
223
+ *
224
+ * @param string $message Message to send to the log file.
225
+ * @param array $context Additional context to add to the log.
226
+ *
77
227
* @return void
78
228
*/
79
- public static function debug ( $ message ) {
229
+ public static function debug ( $ message, $ context = [] ) {
80
230
if ( ! self ::can_log () ) {
81
231
return ;
82
232
}
@@ -85,19 +235,15 @@ public static function debug( $message ) {
85
235
self ::$ logger = wc_get_logger ();
86
236
}
87
237
88
- self ::$ logger ->debug ( $ message , [ ' source ' => self ::WC_LOG_FILENAME ] );
238
+ self ::$ logger ->debug ( $ message , array_merge ( self ::LOG_CONTEXT , $ context ) );
89
239
}
90
240
91
241
/**
92
- * Whether we can log based on settings and filters .
242
+ * Whether we can log based on the plugin settings .
93
243
*
94
244
* @return boolean
95
245
*/
96
246
public static function can_log (): bool {
97
- if ( ! class_exists ( 'WC_Logger ' ) ) {
98
- return false ;
99
- }
100
-
101
247
$ settings = WC_Stripe_Helper::get_stripe_settings ();
102
248
103
249
if ( empty ( $ settings ) || ( isset ( $ settings ['logging ' ] ) && 'yes ' !== $ settings ['logging ' ] ) ) {
0 commit comments