API Reference

Class and method documentation for WPZylos Logger.

Logger

PSR-3 compliant logger. Writes to plugin-specific log files and optionally to error_log when WP_DEBUG is enabled.

Namespace: WPZylos\Framework\LoggerImplements: Psr\Log\LoggerInterface

Constructor

public function __construct(
    ContextInterface $context,
    ?string $logPath = null,
    string $minLevel = LogLevel::DEBUG,
    bool $useErrorLog = true
)
ParameterTypeDefaultDescription
$contextContextInterfacePlugin context
$logPath?stringnullCustom log file path (null = auto)
$minLevelstringLogLevel::DEBUGMinimum level to log
$useErrorLogbooltrueAlso write to error_log when WP_DEBUG

Log Methods (PSR-3)

MethodDescription
emergency(string $message, array $context = []): voidSystem is unusable
alert(string $message, array $context = []): voidAction must be taken immediately
critical(string $message, array $context = []): voidCritical conditions
error(string $message, array $context = []): voidError conditions
warning(string $message, array $context = []): voidWarning conditions
notice(string $message, array $context = []): voidNormal but significant
info(string $message, array $context = []): voidInformational messages
debug(string $message, array $context = []): voidDebug-level messages
log(string $level, string $message, array $context = []): voidLog at arbitrary level

Utility Methods

MethodReturnDescription
cleanOldLogs(int $daysToKeep = 30)intDelete log files older than $daysToKeep days. Returns number of files deleted.

Message Interpolation

Context values are interpolated into {placeholder} tokens:

$logger->info('User {name} logged in from {ip}', [
    'name' => 'John',
    'ip'   => '192.168.1.1',
]);
// Output: User John logged in from 192.168.1.1

Exception Logging

Pass exceptions in the exception context key:

try {
    riskyOperation();
} catch (\Throwable $e) {
    $logger->error('Operation failed: {message}', [
        'message'   => $e->getMessage(),
        'exception' => $e, // Stack trace appended automatically
    ]);
}

Log File Location

Default path: {wp_upload_dir}/{plugin_slug}/logs/{Y-m-d}.log

Log directories are protected with .htaccess (Deny from all) and index.php (silence file).


LoggerServiceProvider

Registers the Logger with the application container.

Registered Services

BindingTypeDescription
Logger::classSingletonLogger instance
LoggerInterface::classSingletonPSR-3 interface binding
'logger'AliasShorthand alias
'log'AliasShorthand alias

Configuration

  • In production (WP_DEBUG off): minimum level is warning
  • In development (WP_DEBUG on): minimum level is debug

Usage via Container

// Via class
$logger = $app->make(Logger::class);

// Via PSR-3 interface
$logger = $app->make(LoggerInterface::class);

// Via alias
$logger = $app->make('log');
$logger = $app->make('logger');