-
Notifications
You must be signed in to change notification settings - Fork 1
API Reference
Authoritative reference for every public class member shipped by
initphp/logger. Method signatures are reproduced verbatim from the
source; behavioural details correspond to the current main branch.
| Class | Namespace | Extends / Implements |
|---|---|---|
| Logger | InitPHP\Logger\Logger |
Psr\Log\AbstractLogger (final) |
| FileLogger | InitPHP\Logger\FileLogger |
Psr\Log\AbstractLogger |
| PDOLogger | InitPHP\Logger\PDOLogger |
Psr\Log\AbstractLogger |
| HelperTrait | InitPHP\Logger\HelperTrait |
trait (@internal) |
All three concrete classes implement Psr\Log\LoggerInterface transitively
through AbstractLogger.
final class InitPHP\Logger\Logger extends \Psr\Log\AbstractLoggerFan-out multiplexer. Forwards every PSR-3 call to every registered inner logger, in registration order. See Multi-Logger for the narrative documentation.
public function __construct(\Psr\Log\LoggerInterface ...$loggers)Parameters
| Name | Type | Description |
|---|---|---|
$loggers |
variadic LoggerInterface
|
One or more PSR-3 loggers to dispatch to. |
Throws
-
\InvalidArgumentException— when zero loggers are supplied. -
\TypeError— when any argument is not aPsr\Log\LoggerInterface(raised by PHP at the call site).
Example
new Logger(
new FileLogger(['path' => '/var/log/app.log']),
new PDOLogger(['pdo' => $pdo, 'table' => 'logs']),
);public function log($level, string|\Stringable $message, array $context = []): voidIterates the registered loggers and calls $inner->log($level, $message, $context)
on each, in order. Exceptions raised by inner loggers are not caught and
abort the rest of the fan-out.
Parameters
| Name | Type | Description |
|---|---|---|
$level |
mixed (typically a Psr\Log\LogLevel constant) |
One of the eight PSR-3 levels. |
$message |
string|\Stringable |
Log message, optionally containing {placeholder} tokens. |
$context |
array<string, mixed> |
Placeholder values. See Context Interpolation. |
Throws
-
\Psr\Log\InvalidArgumentException— when an inner handler raises it for an unknown level. - Any exception thrown by an inner handler (e.g.
\PDOException).
public function getLoggers(): arrayReturns the inner loggers in the order they were registered.
Returns list<\Psr\Log\LoggerInterface>
From \Psr\Log\AbstractLogger, all eight delegate to log():
public function emergency(string|\Stringable $message, array $context = []): void;
public function alert (string|\Stringable $message, array $context = []): void;
public function critical (string|\Stringable $message, array $context = []): void;
public function error (string|\Stringable $message, array $context = []): void;
public function warning (string|\Stringable $message, array $context = []): void;
public function notice (string|\Stringable $message, array $context = []): void;
public function info (string|\Stringable $message, array $context = []): void;
public function debug (string|\Stringable $message, array $context = []): void;class InitPHP\Logger\FileLogger extends \Psr\Log\AbstractLogger
{
use \InitPHP\Logger\HelperTrait;
}Appends each record as a single line to a file. See FileLogger for the narrative documentation.
public function __construct(array $options = [])Parameters
| Name | Type | Description |
|---|---|---|
$options |
array{path?: string} |
Configuration array. Only the path key is read. |
Recognised options
| Key | Type | Required | Description |
|---|---|---|---|
path |
string |
yes | Destination file path. May contain {year}, {month}, {day}, {hour}, {minute}, {second} tokens. |
Throws
-
\InvalidArgumentException— whenpathis missing, not a string, or empty.
Example
new FileLogger(['path' => __DIR__ . '/logs/app-{year}-{month}-{day}.log']);public function log($level, string|\Stringable $message, array $context = []): voidWrites exactly one line of the form:
<ISO-8601 timestamp> [<UPPERCASE-LEVEL>] <interpolated message>\n
Parameters
| Name | Type | Description |
|---|---|---|
$level |
mixed (typically a Psr\Log\LogLevel constant) |
One of the eight PSR-3 levels. |
$message |
string|\Stringable |
Log message with optional placeholders. |
$context |
array<string, mixed> |
Placeholder values. |
Throws
-
\Psr\Log\InvalidArgumentException— when$levelis not a recognised PSR-3 level.
Failure modes (silent)
- Failure to create the parent directory →
error_log()notice, no exception. - Failure of
file_put_contents()→error_log()notice, no exception.
Concurrency: writes use FILE_APPEND | LOCK_EX.
public function getPath(): stringReturns the destination file path after token interpolation.
Returns string
Example
$logger = new FileLogger(['path' => '/var/log/app-{year}.log']);
$logger->getPath(); // "/var/log/app-2026.log"| Visibility | Name | Type | Description |
|---|---|---|---|
protected |
$path |
string |
Token-resolved destination path. |
class InitPHP\Logger\PDOLogger extends \Psr\Log\AbstractLogger
{
use \InitPHP\Logger\HelperTrait;
}Inserts each record as a row in a relational table. See PDOLogger for the narrative documentation.
public function __construct(array $options = [])Parameters
| Name | Type | Description |
|---|---|---|
$options |
array{pdo?: \PDO, table?: string} |
Configuration array. |
Recognised options
| Key | Type | Required | Description |
|---|---|---|---|
pdo |
\PDO |
yes | Configured PDO connection. |
table |
string |
yes | Destination table name. Must match /^[A-Za-z_][A-Za-z0-9_]*$/. |
Throws (all \InvalidArgumentException)
-
pdokey missing -
pdonot aPDOinstance -
tablekey missing -
tableempty / not a string -
tablefails the identifier regex
The exact messages are listed in PDOLogger › Validation.
public function log($level, string|\Stringable $message, array $context = []): voidInserts one row:
INSERT INTO <table> (level, message, date) VALUES (?, ?, ?)with level uppercased, message interpolated, date formatted as
Y-m-d H:i:s at insertion time. Bound through prepared statements.
Throws
-
\Psr\Log\InvalidArgumentException— unknown level. -
\PDOException— any database failure (connection lost, table missing, permission denied, …). Not swallowed; see Error Handling › Database failures.
public function getTable(): stringReturns the configured destination table name.
Returns string
| Visibility | Name | Type | Description |
|---|---|---|---|
protected |
$pdo |
\PDO |
The supplied PDO connection. |
protected |
$table |
string |
Validated table name. |
| Visibility | Name | Type | Value |
|---|---|---|---|
private |
TABLE_NAME_PATTERN |
string |
/^[A-Za-z_][A-Za-z0-9_]*$/ |
trait InitPHP\Logger\HelperTraitNote. Marked
@internal. The trait is part of the implementation surface of the bundled handlers and may shift between patch releases. Reusing it in your own handlers is supported but at your own risk — there is no compatibility guarantee.
protected function interpolate(string|\Stringable $message, array $context = []): stringExpands {placeholder} tokens. Rendering rules:
| Value type | Rendered as |
|---|---|
null |
"" |
true, false
|
"true", "false"
|
int, float, string
|
(string) $value |
\Stringable |
(string) $value |
\Throwable |
"<Class>(<code>): <message> in <file>:<line>" |
| arrays | placeholder left untouched |
| non-stringable objects | placeholder left untouched |
Non-string context keys are skipped. See Context Interpolation for examples.
Returns string
protected function getDate(string $format = 'c'): stringReturns the current time formatted with DateTimeImmutable::format().
Defaults to ISO-8601 with offset ('c').
Returns string
protected function logLevelVerify(mixed $level): voidValidates that $level is one of the eight PSR-3 level strings, comparing
case-insensitively.
Throws \Psr\Log\InvalidArgumentException on failure.
PHPStan note. The trait carries a @phpstan-assert string $level
annotation, so after calling logLevelVerify($level) static analysers
know $level is a string.
| Visibility | Name | Type | Description |
|---|---|---|---|
private |
$levels |
list<string> |
The eight canonical PSR-3 level strings, in severity order. |
| Where? | Exception | When? |
|---|---|---|
Logger::__construct |
\InvalidArgumentException |
Zero loggers. |
Logger::__construct |
\TypeError |
Non-LoggerInterface argument (PHP-enforced). |
FileLogger::__construct |
\InvalidArgumentException |
path missing / empty / non-string. |
PDOLogger::__construct |
\InvalidArgumentException |
pdo or table missing/wrong/invalid. |
*Logger::log() |
\Psr\Log\InvalidArgumentException |
Unknown level. |
PDOLogger::log() |
\PDOException |
Backend failure (propagated). |
Logger::log() |
(inner exception verbatim) | Inner handler raised. |
- Narrative docs: Multi-Logger, FileLogger, PDOLogger.
- PSR-3 Compliance and Error Handling for behavioural details.
- Custom Handlers › Guidelines for writing handlers that match the conventions above.
initphp/logger · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Handlers
PSR-3 Behaviour
Practical Guides
Reference