199 lines
4.7 KiB
PHP
Executable File
199 lines
4.7 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Logger class for sync operations
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class MLS_Logger {
|
|
|
|
/**
|
|
* Log levels
|
|
*/
|
|
const DEBUG = 'debug';
|
|
const INFO = 'info';
|
|
const WARNING = 'warning';
|
|
const ERROR = 'error';
|
|
|
|
/**
|
|
* Database instance
|
|
*/
|
|
private $db;
|
|
|
|
/**
|
|
* Current sync state ID
|
|
*/
|
|
private $sync_state_id = null;
|
|
|
|
/**
|
|
* Whether to also write to WP debug log
|
|
*/
|
|
private $write_to_debug_log = true;
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param MLS_DB $db Database instance
|
|
*/
|
|
public function __construct(MLS_DB $db) {
|
|
$this->db = $db;
|
|
$this->write_to_debug_log = defined('WP_DEBUG') && WP_DEBUG;
|
|
}
|
|
|
|
/**
|
|
* Set current sync state ID for associating logs
|
|
*
|
|
* @param int|null $sync_state_id
|
|
*/
|
|
public function set_sync_state($sync_state_id) {
|
|
$this->sync_state_id = $sync_state_id;
|
|
}
|
|
|
|
/**
|
|
* Log a message
|
|
*
|
|
* @param string $level Log level
|
|
* @param string $message Message
|
|
* @param array $context Additional context
|
|
*/
|
|
public function log($level, $message, $context = array()) {
|
|
global $wpdb;
|
|
|
|
// Insert into database
|
|
$wpdb->insert(
|
|
$this->db->sync_log_table(),
|
|
array(
|
|
'sync_state_id' => $this->sync_state_id,
|
|
'level' => $level,
|
|
'message' => $message,
|
|
'context' => !empty($context) ? wp_json_encode($context) : null,
|
|
'created_at' => current_time('mysql'),
|
|
),
|
|
array('%d', '%s', '%s', '%s', '%s')
|
|
);
|
|
|
|
// Also write to WP debug log if enabled
|
|
if ($this->write_to_debug_log) {
|
|
$log_message = sprintf(
|
|
'[MLS] [%s] %s',
|
|
strtoupper($level),
|
|
$message
|
|
);
|
|
if (!empty($context)) {
|
|
$log_message .= ' | Context: ' . wp_json_encode($context);
|
|
}
|
|
error_log($log_message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Debug log
|
|
*
|
|
* @param string $message
|
|
* @param array $context
|
|
*/
|
|
public function debug($message, $context = array()) {
|
|
$this->log(self::DEBUG, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* Info log
|
|
*
|
|
* @param string $message
|
|
* @param array $context
|
|
*/
|
|
public function info($message, $context = array()) {
|
|
$this->log(self::INFO, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* Warning log
|
|
*
|
|
* @param string $message
|
|
* @param array $context
|
|
*/
|
|
public function warning($message, $context = array()) {
|
|
$this->log(self::WARNING, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* Error log
|
|
*
|
|
* @param string $message
|
|
* @param array $context
|
|
*/
|
|
public function error($message, $context = array()) {
|
|
$this->log(self::ERROR, $message, $context);
|
|
}
|
|
|
|
/**
|
|
* Get recent logs
|
|
*
|
|
* @param int $limit Number of logs to retrieve
|
|
* @param string|null $level Filter by level
|
|
* @param int|null $sync_state_id Filter by sync state
|
|
* @return array
|
|
*/
|
|
public function get_logs($limit = 100, $level = null, $sync_state_id = null) {
|
|
global $wpdb;
|
|
|
|
$where = array('1=1');
|
|
$values = array();
|
|
|
|
if ($level) {
|
|
$where[] = 'level = %s';
|
|
$values[] = $level;
|
|
}
|
|
|
|
if ($sync_state_id) {
|
|
$where[] = 'sync_state_id = %d';
|
|
$values[] = $sync_state_id;
|
|
}
|
|
|
|
$where_sql = implode(' AND ', $where);
|
|
$values[] = $limit;
|
|
|
|
$sql = "SELECT * FROM {$this->db->sync_log_table()}
|
|
WHERE {$where_sql}
|
|
ORDER BY created_at DESC
|
|
LIMIT %d";
|
|
|
|
if (!empty($values)) {
|
|
$sql = $wpdb->prepare($sql, $values);
|
|
}
|
|
|
|
return $wpdb->get_results($sql);
|
|
}
|
|
|
|
/**
|
|
* Clear old logs
|
|
*
|
|
* @param int $days_to_keep Keep logs from last N days
|
|
* @return int Number of deleted rows
|
|
*/
|
|
public function clear_old_logs($days_to_keep = 30) {
|
|
global $wpdb;
|
|
|
|
$cutoff = gmdate('Y-m-d H:i:s', strtotime("-{$days_to_keep} days"));
|
|
|
|
return $wpdb->query(
|
|
$wpdb->prepare(
|
|
"DELETE FROM {$this->db->sync_log_table()} WHERE created_at < %s",
|
|
$cutoff
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Clear all logs
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function clear_all() {
|
|
global $wpdb;
|
|
return false !== $wpdb->query("TRUNCATE TABLE {$this->db->sync_log_table()}");
|
|
}
|
|
}
|