PluginProbe ʕ •ᴥ•ʔ
Easy HTTPS Redirection (SSL) / 2.0.1
Easy HTTPS Redirection (SSL) v2.0.1
trunk 1.5 1.6 1.8 1.9.1 1.9.2 2.0.0 2.0.1
https-redirection / classes / ehssl-debug-logger.php
https-redirection / classes Last commit date
utilities 1 day ago ehssl-config.php 1 year ago ehssl-cronjob.php 1 year ago ehssl-custom-post-types.php 1 year ago ehssl-debug-logger.php 1 year ago ehssl-email-handler.php 1 year ago ehssl-init-time-tasks.php 1 day ago ehssl-installation.php 1 day ago ehssl-non-https-resources-scan-result-table.php 1 day ago ehssl-non-https-resources-scan-update.php 1 day ago ehssl-rules-helper.php 1 day ago ehssl-ssl-certificate.php 1 year ago index.php 1 year ago
ehssl-debug-logger.php
157 lines
1 <?php
2 /**
3 * Logs debug data to a file.
4 */
5 class EHSSL_Logger
6 {
7 public static $log_folder_path = EASY_HTTPS_SSL_PATH . '/logs';
8 public static $default_log_file = 'log.txt';
9 public static $debug_status = array('SUCCESS', 'STATUS', 'NOTICE', 'WARNING', 'FAILURE', 'CRITICAL');
10 public static $section_break_marker = "\n----------------------------------------------------------\n\n";
11 public static $log_reset_marker = "-------- Log File Reset --------\r\n";
12
13 /**
14 * Checks whether debug logging is enabled or not.
15 *
16 * @return boolean
17 */
18 public static function is_logging_enabled()
19 {
20 global $httpsrdrctn_options;
21
22 $enable_debug_logging = isset($httpsrdrctn_options['enable_debug_logging']) ? esc_attr($httpsrdrctn_options['enable_debug_logging']) : 0;
23
24 return $enable_debug_logging == '1';
25 }
26
27 /**
28 * Generates a unique suffix for filename.
29 *
30 * @return string File name suffix.
31 */
32 public static function get_log_file_suffix()
33 {
34 global $httpsrdrctn_options;
35
36 $suffix = isset($httpsrdrctn_options['ehssl_logfile_suffix']) ? esc_attr($httpsrdrctn_options['ehssl_logfile_suffix']) : '';
37 if (!empty($suffix)) {
38 return $suffix;
39 }
40
41 $suffix = uniqid();
42 $httpsrdrctn_options['ehssl_logfile_suffix'] = $suffix;
43 update_option('httpsrdrctn_options', $httpsrdrctn_options);
44
45 return $suffix;
46 }
47
48 /**
49 * Get the log file with a unique name.
50 *
51 * @return string Log file name.
52 */
53 public static function get_log_file_name()
54 {
55 return 'log-' . self::get_log_file_suffix() . '.txt';
56 }
57
58 /**
59 * Get the log filename with absolute path.
60 *
61 * @return string Debug log file.
62 */
63 public static function get_log_file()
64 {
65 return self::$log_folder_path . '/' . self::get_log_file_name();
66 }
67
68 public static function get_debug_timestamp()
69 {
70 return '[' . gmdate( 'm/d/Y h:i:s A' ) . ']';
71 }
72
73 public static function get_debug_status($level)
74 {
75 $size = count(self::$debug_status);
76 if ($level >= $size) {
77 return 'UNKNOWN';
78 } else {
79 return self::$debug_status[$level];
80 }
81 }
82
83 public static function get_section_break($section_break)
84 {
85 if ($section_break) {
86 return self::$section_break_marker;
87 }
88 return "";
89 }
90
91 public static function write_to_file($content, $file_name, $overwrite = false)
92 {
93 if (empty($file_name)) {
94 $file_name = self::$default_log_file;
95 }
96
97 $debug_log_file = self::$log_folder_path . '/' . $file_name;
98
99 //Write to the log file
100 if (!file_put_contents($debug_log_file, $content . "\r\n", ( ! $overwrite ? FILE_APPEND : 0))) {
101 return false;
102 }
103 return true;
104 }
105
106 public static function reset_log_file($file_name = '')
107 {
108 if (empty($file_name)) {
109 $file_name = self::$default_log_file;
110 }
111
112 $debug_log_file = self::$log_folder_path . '/' . $file_name;
113 $content = self::get_debug_timestamp() . ' ' . self::$log_reset_marker;
114 $fp = fopen($debug_log_file, 'w');
115 fwrite($fp, $content);
116 fclose($fp);
117 }
118
119 /**
120 * Logs the message is a log-<uniqueId>-.txt
121 *
122 * @param string|array $message Data to write to the log file
123 * @param integer $level Specify the log level.
124 * @param boolean $section_break Whether to add a line break.
125 * @param boolean $overwrite Whether to overwrite the file content.
126 * @return boolean True if file write is success, false if not.
127 */
128 public static function log($message, $level = 0, $section_break = false, $overwrite = false)
129 {
130 if (!self::is_logging_enabled()) {
131 return;
132 }
133
134 $file_name = self::get_log_file_name();
135
136 //Timestamp
137 $content = self::get_debug_timestamp();
138 //Debug status
139 $content .= '[' . self::get_debug_status($level) . ']';
140 $content .= ' - ';
141
142 if (is_array($message)) {
143 // Print the array content into a string.
144 ob_start();
145 print_r($message);
146 $printed_array = ob_get_contents();
147 ob_end_clean();
148 $content .= $printed_array;
149 }else{
150 $content .= $message;
151 }
152
153 $content .= self::get_section_break($section_break);
154 return self::write_to_file($content, $file_name, $overwrite);
155 }
156 }
157