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

AI Logger

This plugin provides an action hook called ai_logger_insert that you can call from within your code to log exceptions, messages, responses, etc.

Usage

The ai_logger_insert action hook 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.

Examples

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,
);

do_action( 'ai_logger_insert', $key, $message, $args );

Capture an error and log the error message

try {

	do_something_dangerous();

} catch ( Exception $e ) {

	do_action( 'ai_logger_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 (Title) and $args['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