Stars: 141
Forks: 83
Pull Requests: 23
Issues: 23
Watchers: 19
Last Updated: 2023-09-07 02:04:05
MySQL Handler for Monolog, which allows to store log messages to a MySQL Table
License: MIT License
Languages: PHP, Makefile, Dockerfile
MySQL Handler for Monolog, which allows to store log messages in a MySQL Table. It can log text messages to a specific table, and creates 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.
As I do not use this project myself anymore and I do not find the time to maintain this project as it deserves I would be happy to find someone taking it over. Please contact me at [email protected] if you'd be interesting to take over that project. Thanks!
monolog-mysql is available via composer. Just add the following line to your required section in composer.json and do a php composer.phar update
.
"wazaari/monolog-mysql": ">1.0.0"
Just use it as any other Monolog Handler, push it to the stack of your Monolog Logger instance. The Handler however needs some parameters:
If $skipDatabaseModifications is set to true, please use the following query as a template to create the log table (with additional fields, if necessary)
CREATE TABLE `log` (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, channel VARCHAR(255), level INTEGER, message LONGTEXT, time INTEGER UNSIGNED, INDEX(channel) USING HASH, INDEX(level) USING HASH, INDEX(time) USING BTREE
)
Given that $pdo is your database instance, you could use the class as follows:
//Import class
use MySQLHandler\MySQLHandler;
//Create MysqlHandler
$mySQLHandler = new MySQLHandler($pdo, "log", array('username', 'userid'), \Monolog\Logger::DEBUG);
//Create logger
$logger = new \Monolog\Logger($context);
$logger->pushHandler($mySQLHandler);
//Now you can use the logger, and further attach additional information
$logger->addWarning("This is a great message, woohoo!", array('username' => 'John Doe', 'userid' => 245));
This tool is free software and is distributed under the MIT license. Please have a look at the LICENSE file for further information.