Skip to content

CategoryLogger

A PSR-3 logger bound to a single category.

The 8 level methods come from LoggerTrait and funnel into CategoryLogger::log(); CategoryLogger::isEnabled() is the cheap hot-path guard for callers to skip expensive message construction. The category threshold is resolved via LogRegistry and cached on the instance, along with the per-level isEnabled() answers.

Those caches are keyed to LogRegistry::generation(), which every configuration change bumps. Instances are shared per category and live for the worker’s lifetime, so a level rule or sink registered after one was handed out would otherwise never be seen by it — the hot path pays one integer comparison to make reconfiguration actually take effect.

final class CategoryLogger implements LoggerInterface

ImplementsLoggerInterface
UsesLoggerTrait
SourceLogging/CategoryLogger.php

public function __construct(string $category): mixed

ParameterTypeDescription
$categorystring

Returns mixed

MethodDescription
[`alert(Stringablestring $message, array $context = []): void`](#alert)
category(): stringReturns the category name this logger is bound to.
[`critical(Stringablestring $message, array $context = []): void`](#critical)
[`debug(Stringablestring $message, array $context = []): void`](#debug)
debugWith(callable(): string $build): voidEmit a debug message built by $build, but only when debug logging is on, and never let building it affect the caller.
[`emergency(Stringablestring $message, array $context = []): void`](#emergency)
[`error(Stringablestring $message, array $context = []): void`](#error)
[`info(Stringablestring $message, array $context = []): void`](#info)
isEnabled(Level $level): boolWhether an event at $level for this category would be emitted by at least one sink: passes the category threshold AND some sink accepts it.
[`log(mixed $level, Stringablestring $message, array<string, mixed> $context = []): void`](#log)
[`notice(Stringablestring $message, array $context = []): void`](#notice)
[`warning(Stringablestring $message, array $context = []): void`](#warning)

public function alert(Stringable|string $message, array $context = []): void

Composed in from LoggerTrait.

Action must be taken immediately.

Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.

ParameterTypeDescription
$messageStringable`“string`
$contextarray

public function category(): string

Returns the category name this logger is bound to.

Returns string

public function critical(Stringable|string $message, array $context = []): void

Composed in from LoggerTrait.

Critical conditions.

Example: Application component unavailable, unexpected exception.

ParameterTypeDescription
$messageStringable`“string`
$contextarray

public function debug(Stringable|string $message, array $context = []): void

Composed in from LoggerTrait.

Detailed debug information.

ParameterTypeDescription
$messageStringable`“string`
$contextarray

public function debugWith(callable(): string $build): void

Emit a debug message built by $build, but only when debug logging is on, and never let building it affect the caller.

Returns the message; the empty string emits nothing.

ParameterTypeDescription
$buildcallable(): stringReturns the message; the empty string emits nothing.

public function emergency(Stringable|string $message, array $context = []): void

Composed in from LoggerTrait.

System is unusable.

ParameterTypeDescription
$messageStringable`“string`
$contextarray

public function error(Stringable|string $message, array $context = []): void

Composed in from LoggerTrait.

Runtime errors that do not require immediate action but should typically be logged and monitored.

ParameterTypeDescription
$messageStringable`“string`
$contextarray

public function info(Stringable|string $message, array $context = []): void

Composed in from LoggerTrait.

Interesting events.

Example: User logs in, SQL logs.

ParameterTypeDescription
$messageStringable`“string`
$contextarray

public function isEnabled(Level $level): bool

Whether an event at $level for this category would be emitted by at least one sink: passes the category threshold AND some sink accepts it.

Allocates nothing; safe to call per request on the hot path.

ParameterTypeDescription
$levelLevel

Returns bool

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

Composed in from LoggerTrait.

Logs with an arbitrary level.

ParameterTypeDescription
$levelmixedA PSR-3 level string or a Level.
$messageStringable`“string`
$contextarray``<``string``, ``mixed``>
ThrowsWhen
InvalidArgumentException

public function notice(Stringable|string $message, array $context = []): void

Composed in from LoggerTrait.

Normal but significant events.

ParameterTypeDescription
$messageStringable`“string`
$contextarray

public function warning(Stringable|string $message, array $context = []): void

Composed in from LoggerTrait.

Exceptional occurrences that are not errors.

Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.

ParameterTypeDescription
$messageStringable`“string`
$contextarray