-
Notifications
You must be signed in to change notification settings - Fork 4
Home
This plugin provides an action hook called ai_logger_insert
that you can call from within your code to log exceptions, messages, responses, etc.
The ai_logger_insert
action hook accepts the following 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'.
- NOTE: Log entries with a level of
info
will only be written to the database ifWP_DEBUG
istrue
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.
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() );
}
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.
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.
Log entries with a level of info
will only be written to the database if WP_DEBUG
is true
true.
ai_logger_garbage_collector_max_age
Integer. The number of days to keep logs for.