A zero-dependency structured leveled logger for HolyPHP supporting DEBUG, INFO, WARN, and ERROR levels, level filtering, text and JSON formatters, contextual fields, and custom sink callbacks.
bundled — works offlinezero dependencieslogjson
Quick start
import "logger";
$log = logger_create("api", "INFO", "text", ["env" => "prod"]);
$log->info("Server started", ["port" => 8080]);
$log->debug("This will be filtered out because minLevel is INFO");
$reqLog = $log->withContext(["request_id" => "req-42"]);
$reqLog->warn("Slow query detected", ["ms" => 350]);
$jsonLog = logger_create("worker", "DEBUG", "json");
$jsonLog->error("Job failed", ["job_id" => 99, "retry" => true]);
$minLevel: Minimum level to emit ("DEBUG", "INFO", "WARN", "ERROR").
$format: Output format ("text" or "json").
$context: Associative array of default contextual fields attached to every record.
$sink: Optional callback function(string $formatted, array $record) to handle emitted logs instead of println.
$includeTimestamp: Whether to include a Unix timestamp (time()) in each record (default true).
Methods
setLevel(string $level): void — Updates the minimum log level.
getLevel(): string — Returns the current minimum log level.
setFormat(string $format): void — Switches between "text" and "json".
getFormat(): string — Returns the current format.
setSink($sink): void — Sets (or clears with null) the custom sink callback.
setIncludeTimestamp(bool $enabled): void — Enables or disables timestamps.
withContext(array $extraContext): Logger — Returns a new child Logger with merged contextual fields (child keys override parent keys).
shouldLog(string $level): bool — Returns true if $level meets or exceeds the minimum level.
log(string $level, string $message, array $context = []) — Emits a log entry if $level is enabled, returning the formatted string or null if filtered out.