Download the PHP package bradmkjr/monolog-wordpress without Composer
On this page you can find all versions of the php package bradmkjr/monolog-wordpress. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package 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: http://www.d-herrmann.de/projects/monolog-mysql-handler/
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
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.
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 - compatible with Monolog v1 and PHP v5.3 or later.
- v2 - compatible with Monolog v2 and PHP v7.1 or later.
- v3 - 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
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, usenull
. 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 emptyarray()
$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 toLogger::DEBUG
$bubble
: Whether the messages that are handled can bubble up the stack or not. Defaults totrue
Examples
Given that the global $wpdb
is your database instance, you could use the class as follows:
Required code to set up tables on plugin activation:
Now somewhere else in my plugin where I want to use the logger:
Example code to delete tables on plugin deactivation:
Example to use in your custom WordPress Plugin
License
This tool is free software and is distributed under the MIT license. Please have a look at the LICENSE file for further information.