moddix/wp-logger

Simple WP Logger

Installs: 6

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/moddix/wp-logger

1.0.0 2025-10-15 10:14 UTC

This package is auto-updated.

Last update: 2025-11-15 10:31:33 UTC


README

A simple logging library for WordPress projects using the Bedrock structure. This library allows you to log messages to a specified log file, streams and others, like in same on Monolog.

Library register set_error_handler, set_exception_handler and register_shutdown_function to log errors, exceptions and fatal errors.

Installation

Install the latest version with

composer require moddix/wp-logger

Usage

  1. Create a file in the mu-plugins directory of your Bedrock project (e.g., wp-logger.php) and include the following code:
use Moddix\WpLogger\Configurator;
use Moddix\WpLogger\WpLogger;
use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Formatter\JsonFormatter;

// Create a Configurator instance
$configurator = new Configurator();

// Example: Set handler to output logs to stdout in JSON format
$handler = new StreamHandler('php://stdout', Level::Debug);
$handler->setFormatter(new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES, true));

// push the handler to the configurator
$configurator->pushHandler($handler);

// Example: Add a processor to add extra data to each log record
$configurator->pushProcessor(function ($record) {
    $record->extra['dummy'] = 'Hello world!';

    return $record;
});

// Example: Add a subscriber to log wp_die events, see HookSubscribersInterface for more options
$configurator->addSubscriberClasses([
    Moddix\WpLogger\HookSubscribers\WpDieSubscriber::class,
]);

// Boot the logger with the configurator
WpLogger::boot($configurator);
  1. Use the facade to log messages in your WordPress code:
\Moddix\WpLogger\Log::info('This is an info message.');
\Moddix\WpLogger\Log::warning('This is a warning message.');
\Moddix\WpLogger\Log::error('This is an error message.');

\Moddix\WpLogger\Log::debug('This is a debug message with context.', ['key' => 'value']);

\Moddix\WpLogger\Log::log('info', 'This is a log message with dynamic level.', ['key' => 'value']);