WooCommerce Code Reference

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!

Tags
class

WC_Log_Handler_Email

version
1.0.0

Table of Contents

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

Properties

Methods

__construct()

Constructor for log handler.

public __construct([string|array<string|int, mixed> $recipients = null ][, string $threshold = 'alert' ]) : mixed
Parameters
$recipients : string|array<string|int, mixed> = 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

handle()

Handle a log entry.

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

Log timestamp.

$level : string

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

$message : string

Log message.

$context : array<string|int, mixed>

Optional. Additional information for log handlers.

Return values
boolFalse if value was not handled and true if value was handled.

set_threshold()

Set handler severity threshold.

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

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

Return values
mixed

add_log()

Add log message.

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

Log timestamp.

$level : string

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

$message : string

Log message.

$context : array<string|int, mixed>

Additional information for log handlers.

Return values
mixed

format_entry()

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

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

Log timestamp.

$level : string

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

$message : string

Log message.

$context : array<string|int, mixed>

Additional information for log handlers.

Return values
stringFormatted log entry.

format_time()

Formats a timestamp for use in log messages.

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

Log timestamp.

Return values
stringFormatted time for use in log entry.

get_backtrace()

Get a backtrace that shows where the logging function was called.

protected static get_backtrace() : array<string|int, mixed>
Return values
array<string|int, mixed>

should_handle()

Determine whether handler should handle log.

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

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

Return values
boolTrue if the log should be handled.