PHPackages                             bradmkjr/monolog-wordpress - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. bradmkjr/monolog-wordpress

ActiveLibrary[Database &amp; ORM](/categories/database)

bradmkjr/monolog-wordpress
==========================

A handler for Monolog that sends messages to MySQL via WordPress Functions

2.2.1(4y ago)2817.5k6[1 PRs](https://github.com/bradmkjr/monolog-wordpress/pulls)MITPHPPHP &gt;=7.1.0CI failing

Since Feb 27Pushed 3y ago4 watchersCompare

[ Source](https://github.com/bradmkjr/monolog-wordpress)[ Packagist](https://packagist.org/packages/bradmkjr/monolog-wordpress)[ Docs](https://github.com/bradmkjr/monolog-wordpress)[ RSS](/packages/bradmkjr-monolog-wordpress/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (3)Versions (23)Used By (0)

monolog-wordpress
=================

[](#monolog-wordpress)

WordPress Handler for Monolog, which allows to store log messages in a MySQL Table. It can log text messages to a specific table, and create the table automatically if it does not exist. The class further allows to dynamically add extra attributes, which are stored in a separate database field, and can be used for later analyzing and sorting.

Original based on: Homepage:

Disclaimer
==========

[](#disclaimer)

This is a very simple handler for monolog. This version works for custom plugin development, but I would not advise to distribute this code in a public repository for general use on high traffic sites. You have been warned.

Installation
============

[](#installation)

monolog-wordpress is available via composer. Just add the following line to your required section in composer.json and do a `php composer.phar update` or your choice of composer update method.

```
"bradmkjr/monolog-wordpress": "^2.1.0"

```

Versions
========

[](#versions)

Since Monolog v2 broke compatibility with PHP versions before v7.1 some may want to keep using Monolog v1. **monolog-wordpress** is therefore offered in two major versions:

- [v1](https://github.com/bradmkjr/monolog-wordpress/tree/v1) - compatible with Monolog v1 and PHP v5.3 or later.
- [v2](https://github.com/bradmkjr/monolog-wordpress/tree/v2) - compatible with Monolog v2 and PHP v7.1 or later.
- [v3](https://github.com/bradmkjr/monolog-wordpress/tree/master) - compatible with Monolog v3 and PHP v8.1 or later.

Apart from the compatibility differences stated above the features of v1 and v2 are going to be kept the same as much as possible.

Usage
=====

[](#usage)

Just use it as any other Monolog Handler, push it to the stack of your Monolog Logger instance. The Handler however has some parameters:

- `$wpdb`: The instance of your DB connection. To use the global connection of WordPress, use `null`. Otherwise, use a `\wpdb` instance. *Default: `null`*
- `$table`: Name of the database table to store the logs in. The 'wp\_' (or other configured) prefix will be added automatically. *Default: `'logs'`*
- `$additionalFields`: simple array of additional database fields, which should be stored in the database. The columns are created automatically, and the fields can later be used in the extra context section of a record. See examples below. *Defaults to an empty `array()`*
- `$level`: The minimum logging level at which this handler will be triggered. Can be any of the standard Monolog logging levels. Use Monologs statically defined contexts. *Defaults to `Logger::DEBUG`*
- `$bubble`: Whether the messages that are handled can bubble up the stack or not. *Defaults to `true`*

Examples
========

[](#examples)

Given that the global `$wpdb` is your database instance, you could use the class as follows:

```
// Import class
use WordPressHandler\WordPressHandler;

// Create WordPressHandler
$wordPressHandler = new WordPressHandler(null, "log", ['username', 'userid'], \Monolog\Logger::DEBUG);

// Configure maximum number of rows to keep (old entries are deleted when reached)
$wordPressHandler->conf_table_size_limiter( 250000 );

// Setup array of extra fields
$record = ['extra' => []];

// Create database table if needed, add extra fields from above
$wordPressHandler->initialize($record);

// Create Logger
$context = 'channel-name';
$logger = new \Monolog\Logger($context);

// Add WordPressHandler as the Handler for the Logger
$logger->pushHandler($wordPressHandler);

// Now you can use the logger, and further attach additional information
$logger->warning("This is a great message, woohoo!", ['username'  => 'John Doe', 'userid'  => 245]);
```

Required code to set up tables on plugin activation:

```
require __DIR__.'/vendor/autoload.php';

// Create the logs table if it doesn't already exist on plugin activation
register_activation_hook(__FILE__, 'my_plugin_activation');
function my_plugin_activation() {
    $handler = new \WordPressHandler\WordPressHandler(
        null, "logs",
        array('username', 'userid'),
        \Monolog\Logger::DEBUG
    );

    // setup array of extra fields
    $record = array('extra' => array());

    // creates database table if needed, add extra fields from above
    $handler->initialize($record);
}
```

Now somewhere else in my plugin where I want to use the logger:

```
$logger = new \Monolog\Logger('channel');
$handler = new \WordPressHandler\WordPressHandler(
    null, "logs",
    [],
    \Monolog\Logger::DEBUG
);
$handler->initialized = true; // Don't do any extra work - we've already done it.
$logger->pushHandler($handler);

$logger->warning('Some message');
```

Example code to delete tables on plugin deactivation:

```
register_uninstall_hook(__FILE__, 'my_plugin_uninstall');
function my_plugin_uninstall()
{
    require __DIR__."/vendor/autoload.php";

    $handler = new \WordPressHandler\WordPressHandler(
        null, "logs",
        [],
        \Monolog\Logger::DEBUG
    );
    $handler->uninitialize();
}
```

Example to use in your custom WordPress Plugin

```
use WordPressHandler\WordPressHandler;

require __DIR__ . '/vendor/autoload.php';

add_action( 'plugins_loaded', 'demo_function' );
function demo_function(){
    // Create a WordPressHandler instance
    $wordPressHandler = new WordPressHandler(null, "log", ['app', 'version'], \Monolog\Logger::DEBUG);

    // Create logger
    $context = 'test-plugin-logging';
    $logger = new \Monolog\Logger($context);

    // Add WordPressHandler as the Handler for the Logger
    $logger->pushHandler($wordPressHandler);

    // Now you can use the logger, and further attach additional information
    $logger->warning("This is a great message, woohoo!", ['app'  => 'Test Plugin', 'version'  => '2.4.5']);
}
```

License
=======

[](#license)

This tool is free software and is distributed under the MIT license. Please have a look at the LICENSE file for further information.

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 53.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~103 days

Recently: every ~0 days

Total

22

Last Release

1605d ago

Major Versions

1.6.5 → 2.0.12020-10-15

1.7.0 → 2.1.02020-10-15

1.7.2 → 2.1.22020-11-29

1.8.0 → 2.2.02022-02-09

v1.x-dev → 2.2.12022-02-09

PHP version history (2 changes)1.6.4PHP &gt;=5.3.0

2.0.0PHP &gt;=7.1.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2095566?v=4)[bradmkjr](/maintainers/bradmkjr)[@bradmkjr](https://github.com/bradmkjr)

---

Top Contributors

[![bradmkjr](https://avatars.githubusercontent.com/u/2095566?v=4)](https://github.com/bradmkjr "bradmkjr (40 commits)")[![BenceSzalai](https://avatars.githubusercontent.com/u/14061916?v=4)](https://github.com/BenceSzalai "BenceSzalai (21 commits)")[![frugan-dev](https://avatars.githubusercontent.com/u/7957714?v=4)](https://github.com/frugan-dev "frugan-dev (5 commits)")[![Grapestain](https://avatars.githubusercontent.com/u/63913308?v=4)](https://github.com/Grapestain "Grapestain (5 commits)")[![Flynsarmy](https://avatars.githubusercontent.com/u/334808?v=4)](https://github.com/Flynsarmy "Flynsarmy (4 commits)")

---

Tags

logloggingwordpressdatabasemysqlmonolog

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bradmkjr-monolog-wordpress/health.svg)

```
[![Health](https://phpackages.com/badges/bradmkjr-monolog-wordpress/health.svg)](https://phpackages.com/packages/bradmkjr-monolog-wordpress)
```

###  Alternatives

[wazaari/monolog-mysql

A handler for Monolog that sends messages to MySQL

145904.1k6](/packages/wazaari-monolog-mysql)[danielme85/laravel-log-to-db

Custom Laravel Log channel handler that can store log events to SQL or MongoDB databases. Uses Laravel native logging functionality.

1341.0M1](/packages/danielme85-laravel-log-to-db)[karelwintersky/monolog-pdo-handler

A handler for Monolog that stores data via PDO driver to Database

109.4k1](/packages/karelwintersky-monolog-pdo-handler)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
