Oihana PHP System

CompositeLogger implements LoggerInterface

A simple composite logger that broadcasts log messages to multiple logger instances.

This class implements the Composite pattern with a minimal API, allowing you to add multiple PSR-3 loggers and broadcast all log calls to each registered logger.

Uses WeakMap (PHP 8.4+) to automatically remove loggers when they are garbage collected.

Example:

$fileLogger = new FileLogger('/var/log/app.log');
$emailLogger = new EmailLogger('admin@example.com');

$composite = new CompositeLogger([$fileLogger, $emailLogger]);

// This will log to both file and email
$composite->error('Database connection failed', ['host' => 'localhost']);

// Remove a specific logger by reference
$composite->removeLogger($emailLogger);

// Logger is automatically removed when no external reference exists
unset($emailLogger); // Automatically removed from composite

Table of Contents

Interfaces

LoggerInterface

Properties

$count  : int
Indicates the number of registered loggers.
$loggers  : WeakMap<LoggerInterface, true>
WeakMap storing loggers with automatic garbage collection.

Methods

__construct()  : mixed
Constructor optionally accepting an array of loggers.
addLogger()  : static
Adds a logger to the composite.
alert()  : void
{@inheritdoc}
clear()  : static
Removes all registered loggers.
critical()  : void
{@inheritdoc}
debug()  : void
{@inheritdoc}
emergency()  : void
{@inheritdoc}
error()  : void
{@inheritdoc}
getLoggers()  : array<string|int, LoggerInterface>
Returns all registered loggers.
hasLogger()  : bool
Checks if a logger is registered in the composite.
info()  : void
{@inheritdoc}
log()  : void
{@inheritdoc}
notice()  : void
{@inheritdoc}
removeLogger()  : static
Removes a logger from the composite by reference.
warning()  : void
{@inheritdoc}

Properties

$count read-only virtual

Indicates the number of registered loggers.

public int $count
Hooks
public int get

$loggers

WeakMap storing loggers with automatic garbage collection.

private WeakMap<LoggerInterface, true> $loggers

Methods

__construct()

Constructor optionally accepting an array of loggers.

public __construct([array<string|int, LoggerInterface$loggers = [] ]) : mixed
Parameters
$loggers : array<string|int, LoggerInterface> = []

Initial loggers to register

addLogger()

Adds a logger to the composite.

public addLogger(LoggerInterface $logger) : static
Parameters
$logger : LoggerInterface

The logger instance to add

Return values
static

Returns the current instance for method chaining

alert()

{@inheritdoc}

public alert(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

clear()

Removes all registered loggers.

public clear() : static
Return values
static

Returns the current instance for method chaining

critical()

{@inheritdoc}

public critical(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

debug()

{@inheritdoc}

public debug(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

emergency()

{@inheritdoc}

public emergency(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

error()

{@inheritdoc}

public error(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

getLoggers()

Returns all registered loggers.

public getLoggers() : array<string|int, LoggerInterface>

Note: Returns a new array of active loggers at the time of call.

Return values
array<string|int, LoggerInterface>

hasLogger()

Checks if a logger is registered in the composite.

public hasLogger(LoggerInterface $logger) : bool
Parameters
$logger : LoggerInterface

The logger instance to check

Return values
bool

True if the logger is registered, false otherwise

info()

{@inheritdoc}

public info(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

log()

{@inheritdoc}

public log(mixed $level, string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$level : mixed
$message : string|Stringable
$context : array<string|int, mixed> = []

notice()

{@inheritdoc}

public notice(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

removeLogger()

Removes a logger from the composite by reference.

public removeLogger(LoggerInterface $logger) : static
Parameters
$logger : LoggerInterface

The logger instance to remove

Return values
static

Returns the current instance for method chaining

warning()

{@inheritdoc}

public warning(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

        
On this page

Search results