vsemayki/monolog-mysql

A handler for Monolog that sends messages to MySQL

3.0.0 2017-01-31 05:37 UTC

This package is not auto-updated.

Last update: 2025-05-05 18:25:30 UTC


README

MySQL Handler for Monolog, which allows to store log messages in a MySQL table. It can log text messages to a monolog table. The class further is added extra attributes - channel, level, ip, created_at and user_agent, which are stored in a separate database fields, and can be used for later analyzing and sorting.

Installation

monolog-mysql is available via composer:

composer require vsemayki/monolog-mysql:^2.0

Usage

First of all, import dump.sql.

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
  • $level can be any of the standard Monolog logging levels. Use Monologs statically defined contexts. Defaults to Logger::INFO
  • $bubble Defaults to true

Examples

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

<?php
use MySQLHandler\MySQLHandler;

$mySQLHandler = new MySQLHandler($pdo, 'monolog', $level = \Monolog\Logger::INFO, $bubble = true);

// Create logger
$logger = new \Monolog\Logger('db_logger');
$logger->pushHandler($mySQLHandler);

// Now you can use the logger, and further attach additional information
// user_id is required!
$logger->addInfo('User has been created, woohoo!', ['action' => 'user/create', 'user_id' => 3562]);

License

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