Search by

A PHP component/library. PSR-3 logger that writes one file per channel per day and stores each message's context in its own file.

Package info

github.com/webservco/log

Homepage

pkg:composer/webservco/log

Statistics

Installs: 1 041

Dependents: 3

Suggesters: 0

Stars: 0

Open Issues: 0

v2.3.0.74 2026-10-11 16:43 UTC

README

A PHP component/library. Please see webservco/sandbox for an example project using these types of components.

PSR-3 logger that writes one file per channel per day and stores each message's context in its own file.

  • Implements: PSR-3: Logger Interface
  • Provides: psr/log-implementation
  • Requires: a writable log directory
  • Related: webservco/dependency-container (creates loggers in var/log/ by channel)

Installation

composer require webservco/log

Usage

use WebServCo\Log\Factory\ContextFileLoggerFactory;

// \WebServCo\Log\Contract\LoggerFactoryInterface
$loggerFactory = new ContextFileLoggerFactory(sprintf('%svar/log', $projectPath));
// \Psr\Log\LoggerInterface
$logger = $loggerFactory->createLogger('application');

$logger->error('Payment failed.', ['orderReference' => 'A123']);

This writes a line to var/log/20261007/application.log:

[20261007.142301.123456] error Payment failed. [context]

The context goes to var/log/20261007/context-application/20261007.142301.123456.context. Messages without context get no [context] marker and no context file.

Contracts

Interface Implementation Purpose
\Psr\Log\LoggerInterface Service\ContextFileLogger Writes the log line and the context file.
Contract\LoggerFactoryInterface Factory\ContextFileLoggerFactory Creates a logger for a channel.
Contract\Context\ContextServiceInterface Service\Context\ContextVarExportService Turns the context array into text.
Contract\Filesystem\FilesystemServiceInterface Service\Filesystem\FilesystemWithDateService Builds the dated paths and writes the files.

Namespaces are relative to WebServCo\Log\.

Notes

The factory throws an OutOfBoundsException if the log directory isn't writable. Subdirectories are created as needed.

The ID at the start of each line is a timestamp with microseconds. Search for that ID to find the matching context file. Nothing rotates or deletes old date folders. Clean them up yourself, say from a daily cron job.

Development

# Start project.
ddev start
# Install dependencies.
ddev c u
# Run all validations.
ddev c c
# Run unit tests.
ddev c test:dox

License

MIT