Files
homeproz/wp-content/plugins/mls-by-hansonxyz/includes/class-mls-logger.php
T
Hanson.xyz Dev 6556479417 Add MLS by HansonXyz plugin for MLS Grid API integration
Features:
- Full sync of NorthStar MLS properties via MLS Grid API v2
- Incremental sync using ModificationTimestamp
- Local media download and storage
- Rate limit compliance (2 req/sec, 7200/hr, 40000/day)
- Sync state tracking with resume capability
- WP-CLI commands: test, sync, status, stats, cache
- Admin settings page with manual sync triggers
- Public API functions: mls_get_properties, mls_get_property, etc.

Database tables:
- mls_properties: Listing data with full field mapping
- mls_media: Downloaded images
- mls_sync_state: Sync progress tracking
- mls_rate_limits: API usage tracking
- mls_sync_log: Debug logging

Documentation:
- docs/CLAUDE.md: AI development guide
- docs/API.md: MLS Grid API reference
- docs/USAGE.md: User documentation

Tested: Connection, auth, sync 10 records, media download verified

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 21:24:38 -06:00

199 lines
4.7 KiB
PHP

<?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()}");
}
}