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(),
	);
}
Clone this wiki locally