An extendable logging utility class with built-in support for various logging formats.
php
Find a file
Asko Nõmm e2c0cd76f0
All checks were successful
Coverage / Coverage (push) Successful in 23s
Lint / Lint (push) Successful in 24s
Add dependency installation step to CI workflows for coverage and linting
2026-02-01 11:33:51 +02:00
.forgejo/workflows Add dependency installation step to CI workflows for coverage and linting 2026-02-01 11:33:51 +02:00
src Fix tests 2025-04-10 11:39:12 +03:00
tests Fix tests 2025-04-10 11:39:12 +03:00
.gitignore Improve tests. 2024-11-11 19:34:44 +02:00
composer.json Fix tests 2025-04-10 11:39:12 +03:00
composer.lock Fix tests 2025-04-10 11:39:12 +03:00
LICENSE.txt Add license 2024-11-10 22:31:54 +02:00
phpstan.neon Add phpstan 2024-11-12 17:38:31 +02:00
phpunit.xml Improve testability of FileSystemDriver 2024-11-11 02:17:07 +02:00
README.md Add CI workflows for coverage and linting; update README to remove Codecov badge 2026-02-01 11:20:30 +02:00

Loggr

An extendable logging utility class brought to you by the frustration of every logging class always having its own unique format, which makes using log viewing tools difficult. Instead of having its own yet-another-format that no tool supports, Loggr attempts to match many already existing formats, allowing you to use whichever you prefer most.

Requirements

  • PHP 8.3+

Installation

composer require asko/loggr

Usage

Loggr is very simple to use, and looks like this:

$loggr = new Loggr(new FileSystemDriver('path-to-logs'));
$loggr->info('message', ['some-data' => 'goes-here']);

All you have to do is instantiate Loggr with the appropriate driver for your use case and then simply log away with any data you want to give it. As per the PSR-3 standard, you can also interpolate context values into the message placeholder, like so:

$loggr = new Loggr(new FileSystemDriver('path-to-logs'));
$loggr->info('Hello {who}', ['who' => 'World']);

Which would then output Hello World as the message.

Methods

Loggr supports all of these logging methods:

  • emergency(string $message, mixed $context = null)
  • alert(string $message, mixed $context = null)
  • critical(string $message, mixed $context = null)
  • error(string $message, mixed $context = null)
  • warning(string $message, mixed $context = null)
  • notice(string $message, mixed $context = null)
  • info(string $message, mixed $context = null)
  • debug(string $message, mixed $context = null)
  • log(mixed $level, string $message, mixed $context = null)

Setting log format

You can change the logging format by setting the format variable in Loggr constructor to a value of Format enum, like so:

$loggr = new Loggr(new FileSystemDriver('path-to-logs'), format: Format::JSON);

Or, if that gets a bit too long for just one line, you can also do:

$loggr = new Loggr(new FileSystemDriver('path-to-logs'));
$loggr->format = Format::JSON;

Loggr supports the following log formats:

  • Format::JSON - Entries are JSON objects.
  • Format::Laravel - Entries correspond to the Laravel log format.
  • Format::Symfony - Entries correspond to the Symfony log format.
  • Format::IntelliJ - Entries correspond to the IntelliJ log format.

Extending

You can extend Loggr with your own custom drivers. A driver is a class that implements the Driver interface and is responsible for making sure that the serialized log entry would end up in the right place.

Built-in drivers