314 lines
7.9 KiB
PHP
314 lines
7.9 KiB
PHP
<?php
|
|
/**
|
|
* 日志类
|
|
*
|
|
* @package YooneSubscriptions
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Yoone_Logger类
|
|
* 处理日志记录
|
|
*/
|
|
class Yoone_Logger {
|
|
|
|
/**
|
|
* 日志级别
|
|
*/
|
|
const EMERGENCY = 'emergency';
|
|
const ALERT = 'alert';
|
|
const CRITICAL = 'critical';
|
|
const ERROR = 'error';
|
|
const WARNING = 'warning';
|
|
const NOTICE = 'notice';
|
|
const INFO = 'info';
|
|
const DEBUG = 'debug';
|
|
|
|
/**
|
|
* WooCommerce日志实例
|
|
*
|
|
* @var WC_Logger
|
|
*/
|
|
private static $logger = null;
|
|
|
|
/**
|
|
* 日志来源
|
|
*
|
|
* @var string
|
|
*/
|
|
private static $source = 'yoone-subscriptions';
|
|
|
|
/**
|
|
* 获取日志实例
|
|
*
|
|
* @return WC_Logger
|
|
*/
|
|
private static function get_logger() {
|
|
if (is_null(self::$logger)) {
|
|
self::$logger = wc_get_logger();
|
|
}
|
|
return self::$logger;
|
|
}
|
|
|
|
/**
|
|
* 记录紧急日志
|
|
*
|
|
* @param string $message 消息
|
|
* @param array $context 上下文
|
|
*/
|
|
public static function emergency($message, $context = array()) {
|
|
self::log(self::EMERGENCY, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* 记录警报日志
|
|
*
|
|
* @param string $message 消息
|
|
* @param array $context 上下文
|
|
*/
|
|
public static function alert($message, $context = array()) {
|
|
self::log(self::ALERT, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* 记录严重日志
|
|
*
|
|
* @param string $message 消息
|
|
* @param array $context 上下文
|
|
*/
|
|
public static function critical($message, $context = array()) {
|
|
self::log(self::CRITICAL, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* 记录错误日志
|
|
*
|
|
* @param string $message 消息
|
|
* @param array $context 上下文
|
|
*/
|
|
public static function error($message, $context = array()) {
|
|
self::log(self::ERROR, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* 记录警告日志
|
|
*
|
|
* @param string $message 消息
|
|
* @param array $context 上下文
|
|
*/
|
|
public static function warning($message, $context = array()) {
|
|
self::log(self::WARNING, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* 记录通知日志
|
|
*
|
|
* @param string $message 消息
|
|
* @param array $context 上下文
|
|
*/
|
|
public static function notice($message, $context = array()) {
|
|
self::log(self::NOTICE, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* 记录信息日志
|
|
*
|
|
* @param string $message 消息
|
|
* @param array $context 上下文
|
|
*/
|
|
public static function info($message, $context = array()) {
|
|
self::log(self::INFO, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* 记录调试日志
|
|
*
|
|
* @param string $message 消息
|
|
* @param array $context 上下文
|
|
*/
|
|
public static function debug($message, $context = array()) {
|
|
self::log(self::DEBUG, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* 记录日志
|
|
*
|
|
* @param string $level 级别
|
|
* @param string $message 消息
|
|
* @param array $context 上下文
|
|
*/
|
|
public static function log($level, $message, $context = array()) {
|
|
if (!class_exists('WC_Logger')) {
|
|
return;
|
|
}
|
|
|
|
$logger = self::get_logger();
|
|
|
|
// 添加上下文信息到消息中
|
|
if (!empty($context)) {
|
|
$message .= ' ' . wp_json_encode($context);
|
|
}
|
|
|
|
$logger->log($level, $message, array('source' => self::$source));
|
|
}
|
|
|
|
/**
|
|
* 记录订阅相关日志
|
|
*
|
|
* @param int $subscription_id 订阅ID
|
|
* @param string $message 消息
|
|
* @param string $level 级别
|
|
*/
|
|
public static function log_subscription($subscription_id, $message, $level = self::INFO) {
|
|
$context = array('subscription_id' => $subscription_id);
|
|
self::log($level, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* 记录支付相关日志
|
|
*
|
|
* @param int $order_id 订单ID
|
|
* @param string $message 消息
|
|
* @param string $level 级别
|
|
*/
|
|
public static function log_payment($order_id, $message, $level = self::INFO) {
|
|
$context = array('order_id' => $order_id);
|
|
self::log($level, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* 记录混装相关日志
|
|
*
|
|
* @param int $bundle_id 混装ID
|
|
* @param string $message 消息
|
|
* @param string $level 级别
|
|
*/
|
|
public static function log_bundle($bundle_id, $message, $level = self::INFO) {
|
|
$context = array('bundle_id' => $bundle_id);
|
|
self::log($level, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* 记录API调用日志
|
|
*
|
|
* @param string $api API名称
|
|
* @param array $request 请求数据
|
|
* @param array $response 响应数据
|
|
* @param string $level 级别
|
|
*/
|
|
public static function log_api_call($api, $request = array(), $response = array(), $level = self::INFO) {
|
|
$context = array(
|
|
'api' => $api,
|
|
'request' => $request,
|
|
'response' => $response
|
|
);
|
|
self::log($level, 'API调用: ' . $api, $context);
|
|
}
|
|
|
|
/**
|
|
* 记录Moneris支付日志
|
|
*
|
|
* @param string $transaction_type 交易类型
|
|
* @param array $request 请求数据
|
|
* @param array $response 响应数据
|
|
* @param string $level 级别
|
|
*/
|
|
public static function log_moneris($transaction_type, $request = array(), $response = array(), $level = self::INFO) {
|
|
// 移除敏感信息
|
|
if (isset($request['api_token'])) {
|
|
$request['api_token'] = '***';
|
|
}
|
|
if (isset($request['pan'])) {
|
|
$request['pan'] = substr($request['pan'], 0, 4) . '****' . substr($request['pan'], -4);
|
|
}
|
|
if (isset($request['cvd'])) {
|
|
$request['cvd'] = '***';
|
|
}
|
|
|
|
$context = array(
|
|
'transaction_type' => $transaction_type,
|
|
'request' => $request,
|
|
'response' => $response
|
|
);
|
|
self::log($level, 'Moneris交易: ' . $transaction_type, $context);
|
|
}
|
|
|
|
/**
|
|
* 获取日志文件路径
|
|
*
|
|
* @return string 日志文件路径
|
|
*/
|
|
public static function get_log_file_path() {
|
|
$upload_dir = wp_upload_dir();
|
|
return $upload_dir['basedir'] . '/wc-logs/' . self::$source . '-' . sanitize_file_name(wp_hash(self::$source)) . '.log';
|
|
}
|
|
|
|
/**
|
|
* 清理旧日志
|
|
*
|
|
* @param int $days 保留天数
|
|
*/
|
|
public static function cleanup_old_logs($days = 30) {
|
|
if (!class_exists('WC_Log_Handler_File')) {
|
|
return;
|
|
}
|
|
|
|
$log_handler = new WC_Log_Handler_File();
|
|
$logs = $log_handler->get_log_files();
|
|
|
|
foreach ($logs as $log_file) {
|
|
if (strpos($log_file, self::$source) !== false) {
|
|
$file_path = WC_Log_Handler_File::get_log_file_path($log_file);
|
|
|
|
if (file_exists($file_path)) {
|
|
$file_time = filemtime($file_path);
|
|
$cutoff_time = time() - ($days * 24 * 60 * 60);
|
|
|
|
if ($file_time < $cutoff_time) {
|
|
unlink($file_path);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取最近的日志条目
|
|
*
|
|
* @param int $limit 限制数量
|
|
* @return array 日志条目
|
|
*/
|
|
public static function get_recent_logs($limit = 100) {
|
|
$log_file = self::get_log_file_path();
|
|
|
|
if (!file_exists($log_file)) {
|
|
return array();
|
|
}
|
|
|
|
$lines = file($log_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
$lines = array_reverse($lines);
|
|
|
|
return array_slice($lines, 0, $limit);
|
|
}
|
|
|
|
/**
|
|
* 设置日志来源
|
|
*
|
|
* @param string $source 来源
|
|
*/
|
|
public static function set_source($source) {
|
|
self::$source = $source;
|
|
}
|
|
|
|
/**
|
|
* 获取日志来源
|
|
*
|
|
* @return string 来源
|
|
*/
|
|
public static function get_source() {
|
|
return self::$source;
|
|
}
|
|
} |