logger

Structured leveled logger with text/JSON formatters, context fields and sinks.

v1.0.0Tooling & CLIbundledimport "logger";
Overview Installation Documentation Examples

Overview

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 offline zero dependencies log json

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]);

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install logger

This adds the library to hphp.yaml, installs it into .hphp/packages/logger/ and pins the exact version in hphp.lock:

require:
  logger: "^1.0"

Use it

import "logger";

logger is bundled with the compiler, so import "logger"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install logger          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install logger
Packagelogger
Version1.0.0 (follows the compiler)
Entry filelogger.hphp
Sourcepackages/lib/logger ↗

Documentation

API Reference

Log Levels

Supported levels (case-insensitive, normalized to uppercase):

Helper Functions

Logger Class

Constructor

new Logger(string $name = "app", string $minLevel = "INFO", string $format = "text", array $context = [], $sink = null, bool $includeTimestamp = true)

Methods

Error Handling

All invalid configurations and arguments throw Exception with a clear logger: ... message:

Examples

01Quick start

Bundled with the compiler — import "logger"; works offline in any file, or add it to a project with hphp install logger.

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]);
Try it
hphp install logger
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_logger.hphp.