exolnet/wp-graylog

Adds a mu-plugin that register a Monolog handler to send exception to a Graylog channel.

Installs: 3 226

Dependents: 0

Suggesters: 0

Security: 0

Stars: 3

Watchers: 4

Forks: 0

Open Issues: 0

Type:wordpress-muplugin

v2.0.1 2022-10-20 15:33 UTC

This package is auto-updated.

Last update: 2024-03-01 00:13:23 UTC


README

Software License Build Status Latest Stable Version Total Downloads

Adds a mu-plugin that register a Monolog handler to send exception to a Graylog channel.

Requirements

  • Bedrock
  • Composer
  • PHP 7.3 or greater
  • Wordpress

Installation

Require this package with Composer:

composer require exolnet/wp-graylog

Define the following PHP constants in your config/application.php file:

/**
 * wp-graylog
 *
 * Here you may configure the Graylog channel for your application. Behind the
 * scene, it uses the Monolog PHP logging library.
 */
Config::define('GRAYLOG_TRANSPORT', env('GRAYLOG_TRANSPORT'));
Config::define('GRAYLOG_HOST', env('GRAYLOG_HOST'));
Config::define('GRAYLOG_PORT', env('GRAYLOG_PORT'));
Config::define('GRAYLOG_LEVEL', env('GRAYLOG_LEVEL'));

Then, update your .env to add GRAYLOG_HOST environment variable:

GRAYLOG_HOST=localhost

Usage

Once installed, errors occurring in your code with a level higher or equals to the GRAYLOG_LEVEL will be sent to the specified Graylog instance. You’ll then be able to centralize all your logs in one place.

Supported Transports

The following transports are supported: UDP, TCP, SSL, HTTP and HTTPS. Select the transport accordingly to your Graylog set up using the GRAYLOG_TRANSPORT configuration. By default, the UDP transport is used.

The default path for HTTP and HTTPS transports is /gelf. This value can be configured using the GRAYLOG_PATH configuration.

Config::define('GRAYLOG_PATH', env('GRAYLOG_PATH', '/gelf'));

Application Name

By default, the blog name is used as the application name, but you can overwrite it with the variable GRAYLOG_APP:

Config::define('GRAYLOG_APP', 'custom-app-name');

Disable Default Error Handler

By default, if a GRAYLOG_HOST is configured, a PHP error handler will be configured to send all errors to Graylog. This behaviour can be disabled by configuring the following environment variable:

Config::define('GRAYLOG_INITIALIZE_ERROR_HANDLER', false);

Capturing Errors

You can either capture a caught exception or capture the last error:

try {
    $this->functionFailsForSure();
} catch (\Throwable $exception) {
    \Exolnet\Wordpress\Graylog\WpGraylog::captureException($exception);
}

// OR

\Exolnet\Wordpress\Graylog\WpGraylog::captureLastError();

Testing

To run PHPUnit tests, please use:

composer test

Contributing

Please see CONTRIBUTING and CODE OF CONDUCT for details.

Security

If you discover any security related issues, please email security@exolnet.com instead of using the issue tracker.

Credits

License

This code is licensed under the MIT license. Please see the license file for more information.