1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Pagantis\ModuleUtils\Model\Log\LogEntry; |
4
|
|
|
|
5
|
|
|
if (!defined('ABSPATH')) { |
6
|
|
|
exit; |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class WC_Pagantis_Logger |
11
|
|
|
{ |
12
|
|
|
public static $logger; |
13
|
|
|
const WC_LOG_FILENAME = 'pagantis-wc'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Utilize WC logger class |
17
|
|
|
* |
18
|
|
|
* @param $message |
19
|
|
|
* @param null $start_time |
|
|
|
|
20
|
|
|
* @param null $end_time |
|
|
|
|
21
|
|
|
* @since 8.6.7 |
22
|
|
|
*/ |
23
|
|
|
public static function writeLog($message, $start_time = null, $end_time = null) |
24
|
|
|
{ |
25
|
|
|
if (! class_exists('WC_Logger')) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if ($message) { |
30
|
|
|
if (empty(self::$logger)) { |
31
|
|
|
self::$logger = wc_get_logger(); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$isDebugLogEnabled = 'yes' === WC_Admin_Settings::get_option('debug', 'no'); |
|
|
|
|
35
|
|
|
$settings = get_option('woocommerce_pagantis_settings'); |
|
|
|
|
36
|
|
|
|
37
|
|
|
if (empty($settings) || isset($settings['debug']) && $isDebugLogEnabled) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (! is_null($start_time)) { |
|
|
|
|
42
|
|
|
$formatted_start_time = date_i18n(get_option('date_format') . ' g:ia', $start_time); |
|
|
|
|
43
|
|
|
$end_time = is_null($end_time) ? current_time('timestamp') : $end_time; |
|
|
|
|
44
|
|
|
$formatted_end_time = date_i18n(get_option('date_format') . ' g:ia', $end_time); |
45
|
|
|
$elapsed_time = round(abs($end_time - $start_time) / 60, 2); |
46
|
|
|
|
47
|
|
|
$log_entry = "\n" . '====Pagantis Version: ' . PAGANTIS_VERSION . '====' . "\n"; |
48
|
|
|
$log_entry .= '====Start Log ' . $formatted_start_time . '====' . "\n" . $message . "\n"; |
49
|
|
|
$log_entry .= '====End Log ' . $formatted_end_time . ' (' . $elapsed_time . ')====' . "\n\n"; |
50
|
|
|
} else { |
51
|
|
|
$log_entry = "\n" . '====Pagantis Version: ' . PAGANTIS_VERSION . '====' . "\n"; |
52
|
|
|
$log_entry .= '====Start Log====' . "\n" . $message . "\n" . '====End Log====' . "\n\n"; |
53
|
|
|
} |
54
|
|
|
if (defined('WP_DEBUG') && WP_DEBUG) { |
|
|
|
|
55
|
|
|
error_log($log_entry); |
56
|
|
|
} |
57
|
|
|
print_r($log_entry); |
58
|
|
|
self::$logger->debug($log_entry, array( 'source' => self::WC_LOG_FILENAME )); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* empty log file |
64
|
|
|
*/ |
65
|
|
|
public static function clearLogs() |
66
|
|
|
{ |
67
|
|
|
if (empty(self::$logger)) { |
68
|
|
|
self::$logger = wc_get_logger(); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
self::$logger->clear(self::WC_LOG_FILENAME); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param null $exception |
|
|
|
|
75
|
|
|
* @param null $message |
|
|
|
|
76
|
|
|
* @global wpdb $wpdb WordPress database abstraction object. |
77
|
|
|
*/ |
78
|
|
|
public static function insert_log_entry_in_wpdb($exception = null, $message = null) |
79
|
|
|
{ |
80
|
|
|
require_once(__ROOT__.'/vendor/autoload.php'); |
81
|
|
|
|
82
|
|
|
global $wpdb; |
83
|
|
|
pg_wc_check_db_log_table(); |
84
|
|
|
$logEntry = new LogEntry(); |
85
|
|
|
if ($exception instanceof \Exception) { |
86
|
|
|
$logEntry = $logEntry->error($exception); |
87
|
|
|
} else { |
88
|
|
|
$logEntry = $logEntry->info($message); |
89
|
|
|
} |
90
|
|
|
$tableName = $wpdb->prefix . PAGANTIS_LOGS_TABLE; |
91
|
|
|
$wpdb->insert($tableName, array('log' => $logEntry->toJson())); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|