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
)
| Parameter | Type | Default | Description |
|---|---|---|---|
$context | ContextInterface | — | Plugin context |
$logPath | ?string | null | Custom log file path (null = auto) |
$minLevel | string | LogLevel::DEBUG | Minimum level to log |
$useErrorLog | bool | true | Also write to error_log when WP_DEBUG |
Log Methods (PSR-3)
| Method | Description |
|---|---|
emergency(string $message, array $context = []): void | System is unusable |
alert(string $message, array $context = []): void | Action must be taken immediately |
critical(string $message, array $context = []): void | Critical conditions |
error(string $message, array $context = []): void | Error conditions |
warning(string $message, array $context = []): void | Warning conditions |
notice(string $message, array $context = []): void | Normal but significant |
info(string $message, array $context = []): void | Informational messages |
debug(string $message, array $context = []): void | Debug-level messages |
log(string $level, string $message, array $context = []): void | Log at arbitrary level |
Utility Methods
| Method | Return | Description |
|---|---|---|
cleanOldLogs(int $daysToKeep = 30) | int | Delete 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
| Binding | Type | Description |
|---|---|---|
Logger::class | Singleton | Logger instance |
LoggerInterface::class | Singleton | PSR-3 interface binding |
'logger' | Alias | Shorthand alias |
'log' | Alias | Shorthand alias |
Configuration
- In production (
WP_DEBUGoff): minimum level iswarning - In development (
WP_DEBUGon): minimum level isdebug
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');