65 lines
2.0 KiB
PHP
65 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace SiteSync;
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Class Log
|
|
*
|
|
* Handles logging for the Site Sync plugin, including info, warning, and error levels.
|
|
*/
|
|
class Log {
|
|
/**
|
|
* Logs an informational message.
|
|
*
|
|
* @param string $message The log message.
|
|
* @param array $context Additional context for the log entry.
|
|
*/
|
|
public static function info( string $message, array $context = array() ): void {
|
|
Logger::log( 'INFO', $message, $context );
|
|
self::write( 'INFO', $message, $context );
|
|
}
|
|
|
|
/**
|
|
* Logs a warning message.
|
|
*
|
|
* @param string $message The log message.
|
|
* @param array $context Additional context for the log entry.
|
|
*/
|
|
public static function warning( string $message, array $context = array() ): void {
|
|
Logger::log( 'WARN', $message, $context );
|
|
self::write( 'WARN', $message, $context );
|
|
}
|
|
|
|
/**
|
|
* Logs an error message.
|
|
*
|
|
* @param string $message The log message.
|
|
* @param array $context Additional context for the log entry.
|
|
*/
|
|
public static function error( string $message, array $context = array() ): void {
|
|
Logger::log( 'ERROR', $message, $context );
|
|
self::write( 'ERROR', $message, $context );
|
|
}
|
|
|
|
/**
|
|
* Writes a log entry to the error log and triggers the 'site_sync/log' action.
|
|
*
|
|
* @param string $level The log level (e.g., INFO, WARN, ERROR).
|
|
* @param string $message The log message.
|
|
* @param array $context Additional context for the log entry.
|
|
*/
|
|
private static function write( string $level, string $message, array $context ): void {
|
|
$contextStr = $context ? ' ' . wp_json_encode( $context ) : '';
|
|
error_log( sprintf( '[Site Sync][%s] %s%s', $level, $message, $contextStr ) ); // phpcs:ignore
|
|
|
|
/**
|
|
* Allow other listeners to capture logs (e.g., to DB table or UI).
|
|
*/
|
|
do_action( 'site_sync/log', $level, $message, $context );
|
|
}
|
|
}
|