222 lines
5.8 KiB
PHP
222 lines
5.8 KiB
PHP
<?php
|
|
/**
|
|
* IGNY8 File Logger
|
|
*
|
|
* Provides file-based logging for all publish/sync workflows
|
|
*
|
|
* @package IGNY8_Bridge
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class Igny8_Logger {
|
|
|
|
/**
|
|
* Log directory path
|
|
*/
|
|
private static $log_dir = null;
|
|
|
|
/**
|
|
* Initialize logger
|
|
*/
|
|
public static function init() {
|
|
// Set log directory to plugin root/logs/publish-sync-logs
|
|
self::$log_dir = dirname(dirname(__FILE__)) . '/logs/publish-sync-logs';
|
|
|
|
// Create directory if it doesn't exist
|
|
if (!file_exists(self::$log_dir)) {
|
|
wp_mkdir_p(self::$log_dir);
|
|
}
|
|
|
|
// Ensure directory is writable
|
|
if (!is_writable(self::$log_dir)) {
|
|
@chmod(self::$log_dir, 0755);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Write log message to file
|
|
*
|
|
* @param string $message Log message
|
|
* @param string $level Log level (INFO, WARNING, ERROR)
|
|
* @param string $log_file Log file name (without .log extension)
|
|
*/
|
|
public static function log($message, $level = 'INFO', $log_file = 'publish-sync') {
|
|
if (self::$log_dir === null) {
|
|
self::init();
|
|
}
|
|
|
|
$timestamp = current_time('Y-m-d H:i:s');
|
|
$formatted_message = "[{$timestamp}] [{$level}] {$message}\n";
|
|
|
|
$file_path = self::$log_dir . '/' . $log_file . '.log';
|
|
|
|
// Append to log file
|
|
error_log($formatted_message, 3, $file_path);
|
|
|
|
// Also log to WordPress debug.log if WP_DEBUG is enabled
|
|
if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
error_log("[IGNY8] [{$level}] {$message}");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Log info message
|
|
*/
|
|
public static function info($message, $log_file = 'publish-sync') {
|
|
self::log($message, 'INFO', $log_file);
|
|
}
|
|
|
|
/**
|
|
* Log warning message
|
|
*/
|
|
public static function warning($message, $log_file = 'publish-sync') {
|
|
self::log($message, 'WARNING', $log_file);
|
|
}
|
|
|
|
/**
|
|
* Log error message
|
|
*/
|
|
public static function error($message, $log_file = 'publish-sync') {
|
|
self::log($message, 'ERROR', $log_file);
|
|
}
|
|
|
|
/**
|
|
* Log API request
|
|
*/
|
|
public static function api_request($method, $endpoint, $data = null) {
|
|
$message = "API REQUEST: {$method} {$endpoint}";
|
|
if ($data) {
|
|
$message .= "\n Data: " . json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
|
}
|
|
self::log($message, 'INFO', 'wordpress-api');
|
|
}
|
|
|
|
/**
|
|
* Log API response
|
|
*/
|
|
public static function api_response($status_code, $body) {
|
|
$message = "API RESPONSE: HTTP {$status_code}";
|
|
if ($body) {
|
|
$body_str = is_string($body) ? $body : json_encode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
|
$message .= "\n Body: " . substr($body_str, 0, 500);
|
|
}
|
|
self::log($message, 'INFO', 'wordpress-api');
|
|
}
|
|
|
|
/**
|
|
* Log API error
|
|
*/
|
|
public static function api_error($error_message) {
|
|
self::log("API ERROR: {$error_message}", 'ERROR', 'wordpress-api');
|
|
}
|
|
|
|
/**
|
|
* Log webhook event
|
|
*/
|
|
public static function webhook($event_type, $data) {
|
|
$message = "WEBHOOK EVENT: {$event_type}";
|
|
if ($data) {
|
|
$message .= "\n Data: " . json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
|
}
|
|
self::log($message, 'INFO', 'webhooks');
|
|
}
|
|
|
|
/**
|
|
* Log workflow separator
|
|
*/
|
|
public static function separator($title = '') {
|
|
$line = str_repeat('=', 80);
|
|
self::log($line);
|
|
if ($title) {
|
|
self::log($title);
|
|
self::log($line);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get log file contents
|
|
*
|
|
* @param string $log_file Log file name
|
|
* @param int $lines Number of lines to read (default 100, 0 for all)
|
|
* @return string Log contents
|
|
*/
|
|
public static function get_log_contents($log_file = 'publish-sync', $lines = 100) {
|
|
if (self::$log_dir === null) {
|
|
self::init();
|
|
}
|
|
|
|
$file_path = self::$log_dir . '/' . $log_file . '.log';
|
|
|
|
if (!file_exists($file_path)) {
|
|
return '';
|
|
}
|
|
|
|
if ($lines === 0) {
|
|
return file_get_contents($file_path);
|
|
}
|
|
|
|
// Read last N lines efficiently
|
|
$file = new SplFileObject($file_path, 'r');
|
|
$file->seek(PHP_INT_MAX);
|
|
$total_lines = $file->key() + 1;
|
|
|
|
$start_line = max(0, $total_lines - $lines);
|
|
$file->seek($start_line);
|
|
|
|
$content = '';
|
|
while (!$file->eof()) {
|
|
$content .= $file->fgets();
|
|
}
|
|
|
|
return $content;
|
|
}
|
|
|
|
/**
|
|
* Clear log file
|
|
*/
|
|
public static function clear_log($log_file = 'publish-sync') {
|
|
if (self::$log_dir === null) {
|
|
self::init();
|
|
}
|
|
|
|
$file_path = self::$log_dir . '/' . $log_file . '.log';
|
|
|
|
if (file_exists($file_path)) {
|
|
@unlink($file_path);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get all log files
|
|
*/
|
|
public static function get_log_files() {
|
|
if (self::$log_dir === null) {
|
|
self::init();
|
|
}
|
|
|
|
if (!is_dir(self::$log_dir)) {
|
|
return array();
|
|
}
|
|
|
|
$files = glob(self::$log_dir . '/*.log');
|
|
$log_files = array();
|
|
|
|
foreach ($files as $file) {
|
|
$log_files[] = array(
|
|
'name' => basename($file, '.log'),
|
|
'path' => $file,
|
|
'size' => filesize($file),
|
|
'modified' => filemtime($file),
|
|
);
|
|
}
|
|
|
|
return $log_files;
|
|
}
|
|
}
|
|
|
|
// Initialize logger
|
|
Igny8_Logger::init();
|