WC_Log_Handler_Email
extends WC_Log_Handler
in package
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
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
$logs
Stores log messages.
protected
array<string|int, mixed>
$logs
= array()
$max_severity
Stores integer representation of maximum logged level.
protected
int
$max_severity
=
ull
$recipients
Stores email recipients.
protected
array<string|int, mixed>
$recipients
= array()
$threshold
Minimum log level this handler will process.
protected
int
$threshold
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 —add_email()
Adds an email to the list of recipients.
public
add_email(string $email) : mixed
Parameters
- $email : string
-
Email address to add.
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
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.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 —clear_logs()
Clear log messages.
protected
clear_logs() : mixed
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
string — Formatted 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
string — Formatted 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> —get_body()
Build body for log email.
protected
get_body() : string
Return values
string — bodyget_subject()
Build subject for log email.
protected
get_subject() : string
Return values
string — subjectshould_handle()
Determine whether handler should handle log.
protected
should_handle(string $level) : bool
Parameters
- $level : string
-
emergency|alert|critical|error|warning|notice|info|debug.