class-wc-connect-logger.php
3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
if ( ! class_exists( 'WC_Connect_Logger' ) ) {
class WC_Connect_Logger {
/**
* @var WC_Logger
*/
private $logger;
private $is_logging_enabled = false;
private $is_debug_enabled = false;
private $feature;
public function __construct( WC_Logger $logger, $feature = '' ) {
$this->logger = $logger;
$this->feature = strtolower( $feature );
$this->is_logging_enabled = WC_Connect_Options::get_option( 'debug_logging_enabled', false );
$this->is_debug_enabled = WC_Connect_Options::get_option( 'debug_display_enabled', false );
}
/**
* Format a message with optional context for logging.
*
* @param string|WP_Error $message Either a string message, or WP_Error object.
* @param string $context Optional. Context for the logged message.
* @return string The formatted log message.
*/
protected function format_message( $message, $context = '' ) {
$formatted_message = $message;
if ( is_wp_error( $message ) ) {
$formatted_message = $message->get_error_code() . ' ' . $message->get_error_message();
}
if ( ! empty( $context ) ) {
$formatted_message .= ' (' . $context . ')';
}
return $formatted_message;
}
public function enable_logging() {
WC_Connect_Options::update_option( 'debug_logging_enabled', true );
$this->is_logging_enabled = true;
$this->log( 'Logging enabled' );
}
public function disable_logging() {
$this->log( 'Logging disabled' );
WC_Connect_Options::update_option( 'debug_logging_enabled', false );
$this->is_logging_enabled = false;
}
public function enable_debug() {
WC_Connect_Options::update_option( 'debug_display_enabled', true );
$this->is_debug_enabled = true;
$this->log( 'Debug enabled' );
}
public function disable_debug() {
$this->log( 'Debug disabled' );
WC_Connect_Options::update_option( 'debug_display_enabled', false );
$this->is_debug_enabled = false;
}
public function is_debug_enabled() {
return $this->is_debug_enabled;
}
public function is_logging_enabled() {
return $this->is_logging_enabled;
}
/**
* Log debug by printing it as notice when debugging is enabled.
*
* @param string $message Debug message.
* @param string $type Notice type.
*/
public function debug( $message, $type = 'notice' ) {
if ( $this->is_debug_enabled() && ! wc_has_notice( $message, $type ) ) {
wc_add_notice( $message, $type );
}
}
/**
* Logs messages even if debugging is disabled
*
* @param string $message Message to log
* @param string $context Optional context (e.g. a class or function name)
*/
public function error( $message, $context = '' ) {
WC_Connect_Error_Notice::instance()->enable_notice( $message );
$this->log( $message, $context, true );
}
/**
* Logs messages to file and error_log if WP_DEBUG
*
* @param string $message Message to log
* @param string $context Optional context (e.g. a class or function name)
*/
public function log( $message, $context = '', $force = false ) {
$log_message = $this->format_message( $message, $context );
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( $log_message );
}
if ( ! $this->is_logging_enabled() && ! $force ) {
return;
}
$log_file = 'wc-services';
if ( ! empty( $this->feature ) ) {
$log_file .= '-' . $this->feature;
}
$this->logger->add( $log_file, $log_message );
}
}
}