Skip to content

Commit

Permalink
Merge pull request #17 from BenceSzalai/master
Browse files Browse the repository at this point in the history
  • Loading branch information
BenceSzalai authored Nov 29, 2020
2 parents af72d5f + 0391041 commit 30994a5
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 100 deletions.
18 changes: 16 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased v2]

## [2.1.2] - 2020-11-29
### Improved
- Constructor can now be called without passing the global `$wpdb`, it'll be used by default.

## [2.1.1] - 2020-11-29
### Improved
- The logging table size limiter caused slow logging once the limit was reached, due to the overhead of truncating the table after every row written. This is now fixed by doing the truncations in batches.

### Changed
- The `set_max_table_rows()` method is deprecated, use `conf_table_size_limiter()` instead.
- The `maybe_truncate()` method was not intended to be used outside of the class, so it is not a public method anymore.

## [2.1.0] - 2020-10-15
### Added
- Feature to limit the maximum number of rows to keep in the log table. Use `set_max_table_rows()` method on the handler instance to configure the limit.
Expand Down Expand Up @@ -57,11 +69,13 @@ V1 is continued to be updated for continued support for Monolog v1 and PHP versi
No changelog had been maintained up to this point. Refer to the GIT commit history for more details.


[Unreleased v2]: https://github.com/bradmkjr/monolog-wordpress/compare/2.1.0...HEAD
[Unreleased v2]: https://github.com/bradmkjr/monolog-wordpress/compare/2.1.2...HEAD
[2.1.2]: https://github.com/bradmkjr/monolog-wordpress/tree/2.1.2
[2.1.1]: https://github.com/bradmkjr/monolog-wordpress/tree/2.1.1
[2.1.0]: https://github.com/bradmkjr/monolog-wordpress/tree/2.1.0
[2.0.1]: https://github.com/bradmkjr/monolog-wordpress/tree/2.0.1
[2.0.0]: https://github.com/bradmkjr/monolog-wordpress/tree/2.0.0
[v1 changes after v2 release]: https://github.com/bradmkjr/monolog-wordpress/compare/1.6.4...v2
[v1 changes after v2 release]: https://github.com/bradmkjr/monolog-wordpress/compare/1.6.4...v1
[1.6.4]: https://github.com/bradmkjr/monolog-wordpress/tree/1.6.4
[1.6.3]: https://github.com/bradmkjr/monolog-wordpress/tree/1.6.3
[1.6.2]: https://github.com/bradmkjr/monolog-wordpress/tree/1.6.2
Expand Down
65 changes: 29 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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 distrubte this code in a public repository for general use on high traffic sites. You have been warned.
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.
Expand All @@ -26,29 +26,26 @@ Since Monolog v2 broke compatibility with PHP versions before v7.1 some may want
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 needs some parameters:
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** Global instance of your DB connection.
- **$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_
- `$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
Given that $wpdb is your database instance, you could use the class as follows:
Given that the global `$wpdb` is your database instance, you could use the class as follows:

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

// Ensure access to global $wpdb
global $wpdb;

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

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

// Setup array of extra fields
$record = ['extra' => []];
Expand All @@ -64,7 +61,7 @@ $logger = new \Monolog\Logger($context);
$logger->pushHandler($wordPressHandler);

// 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));
$logger->warning("This is a great message, woohoo!", ['username' => 'John Doe', 'userid' => 245]);
```

Required code to set up tables on plugin activation:
Expand All @@ -73,33 +70,34 @@ 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
function register_activation_hook(__FILE__, function() {
global $wpdb;

register_activation_hook(__FILE__, 'my_plugin_activation');
function my_plugin_activation() {
$handler = new \WordPressHandler\WordPressHandler(
$wpdb, "logs",
null, "logs",
array('username', 'userid'),
\Monolog\Logger::DEBUG
);

// setup array of extra fields
$record = ['extra' => []];
$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
Now somewhere else in my plugin where I want to use the logger:
```php
$logger = new \Monolog\Logger('channel');
$handler = new \WordPressHandler\WordPressHandler(
$wpdb, "logs",
null, "logs",
[],
\Monolog\Logger::DEBUG
);
$handler->initialized = true; // Don't do any extra work - we've already done it.
$logger->pushHandler($handler);

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

Example code to delete tables on plugin deactivation:
Expand All @@ -109,10 +107,9 @@ register_uninstall_hook(__FILE__, 'my_plugin_uninstall');
function my_plugin_uninstall()
{
require __DIR__."/vendor/autoload.php";
global $wpdb;

$handler = new \WordPressHandler\WordPressHandler(
$wpdb, "logs",
null, "logs",
[],
\Monolog\Logger::DEBUG
);
Expand All @@ -124,18 +121,14 @@ function my_plugin_uninstall()
Example to use in your custom WordPress Plugin

```php
use WordPressHandler\WordPressHandler;

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

add_action( 'plugins_loaded', 'demo_function' );
function demo_function(){
require __DIR__ . '/vendor/autoload.php';

// Import class
use WordPressHandler\WordPressHandler;

// Ensure access to global $wpdb
global $wpdb;

// Create WordPressHandler
$wordPressHandler = new WordPressHandler($wpdb, "log", array('app', 'version'), \Monolog\Logger::DEBUG);
// Create a WordPressHandler instance
$wordPressHandler = new WordPressHandler(null, "log", ['app', 'version'], \Monolog\Logger::DEBUG);

// Create logger
$context = 'test-plugin-logging';
Expand All @@ -145,7 +138,7 @@ function demo_function(){
$logger->pushHandler($wordPressHandler);

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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["wordpress", "log", "logging", "monolog", "mysql", "database"],
"homepage": "https://github.com/bradmkjr/monolog-wordpress",
"license": "MIT",
"version": "2.1.0",
"version": "2.1.2",
"authors": [
{
"name": "Bradford Knowlton",
Expand Down
Loading

0 comments on commit 30994a5

Please sign in to comment.