Files
WooList/woolist-phplist/includes/class-woolist-logger.php

83 lines
2.7 KiB
PHP
Raw Normal View History

<?php
/**
* Logger for WooList delegates to WooCommerce's built-in logging system.
*
* Logs are visible at WooCommerce Status Logs, source: woolist-phplist.
*
* Levels:
* INFO always written (subscription events, connection test results)
* ERROR always written (API failures, config problems)
* DEBUG written only when "Enable debug logging" is checked in settings
*
* @package WooList
*/
defined( 'ABSPATH' ) || exit;
class WooList_Logger {
private const SOURCE = 'woolist-phplist';
/** Whether verbose debug lines are recorded. */
private static bool $debug_enabled = false;
/** Lazy-loaded WC_Logger instance. */
private static ?WC_Logger_Interface $wc_logger = null;
/**
* Initialise: read the debug setting.
* Called on plugins_loaded after options are available.
*/
public static function init(): void {
self::$debug_enabled = ( get_option( 'woolist_enable_debug_log' ) === 'yes' );
}
// ── Public logging methods ───────────────────────────────────────────────
/** Always logged. Use for normal subscription / connection events. */
public static function info( string $message ): void {
self::wc()->info( $message, [ 'source' => self::SOURCE ] );
}
/** Always logged. */
public static function error( string $message ): void {
self::wc()->error( $message, [ 'source' => self::SOURCE ] );
}
/**
* Logged only when debug mode is enabled.
* Use for full request URLs (password redacted), raw responses, flow steps.
*/
public static function debug( string $message ): void {
if ( self::$debug_enabled ) {
self::wc()->debug( $message, [ 'source' => self::SOURCE ] );
}
}
// ── Utility ─────────────────────────────────────────────────────────────
/**
* Strip the password parameter from a phpList URL before logging it.
*
* @param string $url Full API URL.
* @return string URL with password value replaced by ***.
*/
public static function redact_url( string $url ): string {
return preg_replace( '/(\bpassword=)[^&]+/', '$1***', $url );
}
public static function is_debug_enabled(): bool {
return self::$debug_enabled;
}
// ── Internal ─────────────────────────────────────────────────────────────
/** Return (and lazy-load) the WC_Logger instance. */
private static function wc(): WC_Logger_Interface {
if ( self::$wc_logger === null ) {
self::$wc_logger = wc_get_logger();
}
return self::$wc_logger;
}
}