A lightweight structured logging library for PHP.

Maintainers

Package info

github.com/WebFiori/log

pkg:composer/webfiori/log

Statistics

Installs: 109

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-05-28 23:29 UTC

This package is auto-updated.

Last update: 2026-05-29 01:02:23 UTC


README

A lightweight structured logging library for PHP with daily file rotation and level filtering.

Supported PHP Versions

This library requires PHP 8.1 or higher.

Build Status

Features

  • Logger interface — swap implementations without changing application code
  • FileLogger — daily-rotated log files with structured format
  • Static facade (LoggerFacade) for quick usage without DI
  • Level filtering — set minimum level (debug, info, warning, error, critical)
  • Structured context — attach key-value metadata to log entries
  • Zero dependencies — requires only PHP 8.1+

Installation

composer require webfiori/log

Usage

Basic Logging

use WebFiori\Log\FileLogger;
use WebFiori\Log\LogLevel;

$logger = new FileLogger('/var/log/myapp', LogLevel::INFO);

$logger->info('User logged in', ['user_id' => 42, 'ip' => '192.168.1.1']);
$logger->error('Payment failed', ['order_id' => 123, 'reason' => 'timeout']);
$logger->debug('This will be filtered out'); // below INFO threshold

Output (/var/log/myapp/app-2026-05-29.log):

[2026-05-29 01:00:00] [INFO] User logged in {"user_id":42,"ip":"192.168.1.1"}
[2026-05-29 01:00:00] [ERROR] Payment failed {"order_id":123,"reason":"timeout"}

Static Facade

use WebFiori\Log\LoggerFacade;

LoggerFacade::info('Application started');
LoggerFacade::error('Something went wrong', ['exception' => $e->getMessage()]);

Custom Logger Implementation

use WebFiori\Log\Logger;

class DatabaseLogger implements Logger {
    public function debug(string $message, array $context = []): void { /* ... */ }
    public function info(string $message, array $context = []): void { /* ... */ }
    public function warning(string $message, array $context = []): void { /* ... */ }
    public function error(string $message, array $context = []): void { /* ... */ }
    public function critical(string $message, array $context = []): void { /* ... */ }
    public function log(string $level, string $message, array $context = []): void { /* ... */ }
}

LoggerFacade::setInstance(new DatabaseLogger());

API

Logger (interface)

Method Description
debug(string $message, array $context = []) Log debug message
info(string $message, array $context = []) Log informational message
warning(string $message, array $context = []) Log warning message
error(string $message, array $context = []) Log error message
critical(string $message, array $context = []) Log critical message
log(string $level, string $message, array $context = []) Log at specified level

FileLogger

Method Description
__construct(string $logDir, string $minLevel = 'debug') Create logger with directory and minimum level
getLogDir(): string Returns the log directory path
getMinLevel(): string Returns the current minimum level
setMinLevel(string $level): void Change the minimum level at runtime

LogLevel

Constant Value
LogLevel::DEBUG 'debug'
LogLevel::INFO 'info'
LogLevel::WARNING 'warning'
LogLevel::ERROR 'error'
LogLevel::CRITICAL 'critical'

LoggerFacade

Method Description
getInstance(): Logger Get the default logger
setInstance(Logger $logger): void Replace the default logger
reset(): void Destroy the default instance
All Logger methods Delegates to the default instance

License

MIT