Monolog MySQL Handler

Monolog is a modern and light-weight, extensible logging platform implementing the PSR-3 Interface which is available via composer. It allows you to easily collect log messages, process them using various Formatters and finally allows a stack of different handlers, which allows for a fairly flexible configuration.

There are many database handlers, but at time of this writing there was no Monolog MySQL Handler, which allows to store log messages to database. Due to the extensibility of the framework, I decided to implement a Handler, which I present here.

Shortcuts

Download: https://github.com/waza-ari/monolog-mysql/archive/v1.0.3.zip

Readme: https://github.com/waza-ari/monolog-mysql

Features

Monolog supports the use of context attributes, which are passed among with the actual log message. This handler leverages this by storing a defined subset of these attributes as separate database column, which is useful for later searching and sorting. Examples for those attributes could be username, user-id or current URL of the user during the logged message.

During initialization of the handler, you can pass an array of attribute names, which are then used to create (or modify, using add and drop column statements) the database table. When logging a message, and those attributes are present in the context attributes, they get stored in the corresponding database field. it is set to NULL, if the attributes are not passed.

Further, it supports the main feature you would expect from a database handler – it stores the log messages in a database.

Installation

monolog-mysql is available via composer. Just add the following line to your required section in composer.json and update.

Usage

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

  • $pdo PDO Instance of your database. Pass along the PDO instantiation of your database connection with your database selected.
  • $table The table name where the logs should be stored
  • $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 can be any of the standard Monolog logging levels. Use Monologs statically defined contexts. Defaults to Logger::DEBUG
  • $bubble Defaults to true

License

This tool is free software and is distributed under the MIT license

8 thoughts on “Monolog MySQL Handler

  1. Luca

    Hi Daniel,
    I want to use your handler, but I don’t know to create a pdo instance with this packages joomla/database 1.2.1 for composer. Which class should I use ? Can you help me?

    Thanks.

    Luca

    Reply
  2. Bradford Knowlton

    Just wanted to reach out and say thanks for sharing your code, and giving me a foundation to build my own iteration with.

    https://github.com/bradmkjr/monolog-wordpress/

    I took yours as a very nicely done outline, and rewrote it to use native WordPress WPDB class, allowing it to easily be used in any custom plugin, without the need for PDO extension. It’s testing and working, and I will probably be setting it to stable flag very soon. One major advantage with WP is the built in DBDelta tools, to automatically handle column changes.

    Thanks again,
    Brad

    Reply
  3. Roy Burnap

    Just a thought on making it more user friendly. In the method write() after initializing, insert the following

    // Assure all user-defined fields have a value
    // and the array is in the proper binding order
    $userVariableArray = [];
    foreach ($this->additionalFields as $f) {
    if (!isset($record['context'][$f])) {
    $userVariableArray[$f] = null;
    } else {
    $userVariableArray[$f] = $record['context'][$f];
    }
    }

    Reply
  4. Roy Burnap

    Addendum to last post:

    Change array_merge() to use the $userVariableArray

    Reply
    1. Daniel Herrmann Post author

      Hi icsbcn,

      I haven’t tried yet, but I think it should be compatible.

      Reply
  5. Kevin Lane

    Just wanted to post a thank you for your efforts on this. I’ve just begun using Composer, Monolog and with this extension I’ve got exactly what I needed.

    thanks

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.