Skip to content
Jared Cobb edited this page Sep 10, 2016 · 24 revisions

AI Logger

This plugin provides a globally accessible class called AI_Logger that you can call from within your code to log exceptions, messages, responses, etc.

Usage

AI_Logger is a singleton class and currently has one method named insert(). The insert() method accepts the following parameters:

Parameters

$key

String. A short and unique title for this specific log entry

$message

String. The full message for the log entry

$args

Array. (Optional)

level - String, 'info', 'warning', 'error', 'critical'. Default is 'error'.

context - String, This value helps to organize your logs into logical areas. For example, if you assign a context of your theme name to any theme code, you'll be able to filter down to log entries in that context.

include_stack_trace - Boolean, Whether or not to append the full stack trace to the log entry. Default is true.

Example

Insert a general info message to the log. Typically used to confirm a successful action or response.

$key = 'This is a unique log title';
$message = 'This can be an exception message, API response, wp-cron results, WP_Error message, etc';
$args = array(
	'level' => 'info',
	'context' => 'My plugin name / theme name / general feature',
	'include_stack_trace' => false,
);
AI_Logger::instance()->insert( $key, $message, $args );

Capture an error and log the error message

try {
	do_something_dangerous();
} catch ( Exception $e ) {
	AI_Logger::instance()->insert(
		'Dangerous Function Failed',
		$e->getMessage(),
	);
}

Viewing Logs

Currently the logs are stored as a custom post type named ai_log and are accessible by the Administrator role (more specifically, access is limited to user with the update_core capability).

A new admin menu labeled Logs will be available just after the Settings menu.

Throttling

In order to prevent multiple log entries from being written to the database (especially on high volume sites), each unique log entry is limited to one per 15 minutes. A log entry is considered unique if it has the same $key and $context.

The exception to this rule is if WP_DEBUG is set to true, in which case, every log entry will be recorded and throttling is disabled.

Info Log Entries

Log entries with a level of info will only be written to the database if WP_DEBUG is true

Filters

ai_logger_throttle_limit

Integer. The number of seconds until a duplicate log entry is allowed to be written to the database. Default is 15 minutes.

ai_logger_allow_production_info_logs

Boolean. Whether or not to allow info level log entries in a production environment. Default is false.

Clone this wiki locally