yard/logger

Basic replaceable PSR-3 logger

v1.0.0 2025-04-09 09:11 UTC

This package is auto-updated.

Last update: 2025-04-09 09:28:09 UTC


README

Code Style PHPStan Tests Code Coverage Badge Lines of Code Badge

Composer package that provides a very basic PSR-3 logger. This logger is just a wrapper around PHP function error_log(). As such it does not have any requirements other than psr/log. It is meant to be used in PHP environments that might not feature a DI container, like WordPress.

The provided logger is actually meant as a temporary stub. The most prominent feature of this package is that the logger can be replaced by any another PSR-3 logger. This allows projects to push a single logger to its dependencies and achieve consistent log handling.

Installation

To install this package using Composer, follow these steps:

  1. Add the following to the repositories section of your composer.json:

    {
      "type": "vcs",
      "url": "git@github.com:yardinternet/logger.git"
    }
  2. Install this package with Composer:

    composer require yard/logger

Usage

Simply use the static Log facade:

Log::error('Whoops.');

Or get the PSR-3 logger instance from it:

$logger = Log::getLogger();

Projects that wish to replace the logger instance can use the static setLogger() method, like this:

Log::setLogger(app()->make('log'));

WordPress

When using the logger in WordPress themes, the above replacement method is not recommended, due to the practise of vendor prefixing. The recommended method to replace the logger from a WordPress theme is to:

  1. Use do_action() in WordPress themes to pass the desired logger to any plugins
  2. Use add_action() in WordPress plugins to receive a logger instance and set it.

The Log class provides the WP_ACTION_SET_LOGGER constant, which contains the name of the WordPress action that should be used. Hooking into the action should look like:

add_action(Yard\Logging\Log::WP_ACTION_SET_LOGGER, Log::setLogger(...));

An Acorn based WordPress theme for example could push its Laravel logger like this:

do_action(Yard\Logging\Log::WP_ACTION_SET_LOGGER, app()->make('log'));