WooCommerce Code Reference

Search results

WC_Log_Handler_Email extends WC_Log_Handler

Handles log entries by sending an email.

WARNING! This log handler has known limitations.

Log messages are aggregated and sent once per request (if necessary). If the site experiences a problem, the log email may never be sent. This handler should be used with another handler which stores logs in order to prevent loss.

It is not recommended to use this handler on a high traffic site. There will be a maximum of 1 email sent per request per handler, but that could still be a dangerous amount of emails under heavy traffic. Do not confuse this handler with an appropriate monitoring solution!

If you understand these limitations, feel free to use this handler or borrow parts of the design to implement your own!

Table of Contents

$threshold Minimum log level this handler will process. int
$recipients Stores email recipients. array
$logs Stores log messages. array
$max_severity Stores integer representation of maximum logged level. int
__construct() Constructor for log handler. mixed
set_threshold() Set handler severity threshold. mixed
should_handle() Determine whether handler should handle log. bool
handle() Handle a log entry. bool
send_log_email() Send log email. bool
get_subject() Build subject for log email. string
get_body() Build body for log email. string
add_email() Adds an email to the list of recipients. mixed
add_log() Add log message. mixed
clear_logs() Clear log messages. mixed
format_time() Formats a timestamp for use in log messages. string
format_entry() Builds a log entry text from level, timestamp and message. string

Properties

Methods

__construct()

Constructor for log handler.

public __construct( [ $recipients : string|array = null ] [, $threshold : string = 'alert' ] ) : mixed
Parameters
$recipients : string|array = null

Optional. Email(s) to receive log messages. Defaults to site admin email.

$threshold : string = 'alert'

Optional. Minimum level that should receive log messages. Default 'alert'. One of: emergency|alert|critical|error|warning|notice|info|debug.

Return values
mixed

set_threshold()

Set handler severity threshold.

public set_threshold( $level : string ) : mixed
Parameters
$level : string

emergency|alert|critical|error|warning|notice|info|debug.

Return values
mixed

should_handle()

Determine whether handler should handle log.

protected should_handle( $level : string ) : bool
Parameters
$level : string

emergency|alert|critical|error|warning|notice|info|debug.

Return values
bool

True if the log should be handled.

handle()

Handle a log entry.

public handle( $timestamp : int , $level : string , $message : string , $context : array ) : bool
Parameters
$timestamp : int

Log timestamp.

$level : string

emergency|alert|critical|error|warning|notice|info|debug.

$message : string

Log message.

$context : array

Optional. Additional information for log handlers.

Return values
bool

False if value was not handled and true if value was handled.

send_log_email()

Send log email.

public send_log_email( ) : bool
Return values
bool

True if email is successfully sent otherwise false.

add_email()

Adds an email to the list of recipients.

public add_email( $email : string ) : mixed
Parameters
$email : string

Email address to add.

Return values
mixed

add_log()

Add log message.

protected add_log( $timestamp : int , $level : string , $message : string , $context : array ) : mixed
Parameters
$timestamp : int

Log timestamp.

$level : string

emergency|alert|critical|error|warning|notice|info|debug.

$message : string

Log message.

$context : array

Additional information for log handlers.

Return values
mixed

format_time()

Formats a timestamp for use in log messages.

protected static format_time( $timestamp : int ) : string
Parameters
$timestamp : int

Log timestamp.

Return values
string

Formatted time for use in log entry.

format_entry()

Builds a log entry text from level, timestamp and message.

protected static format_entry( $timestamp : int , $level : string , $message : string , $context : array ) : string
Parameters
$timestamp : int

Log timestamp.

$level : string

emergency|alert|critical|error|warning|notice|info|debug.

$message : string

Log message.

$context : array

Additional information for log handlers.

Return values
string

Formatted log entry.

Back to the top