WooCommerce Code Reference

Search results

WC_Log_Handler_File extends WC_Log_Handler

Handles log entries by writing to a file.

Table of Contents

$handles Stores open file handles. array
$log_size_limit File size limit for log files in bytes. int
$cached_logs Cache logs that could not be written. array
__construct() Constructor for the logger. mixed
__destruct() Destructor. mixed
handle() Handle a log entry. bool
format_entry() Builds a log entry text from timestamp, level and message. string
open() Open log file for writing. bool
is_open() Check if a handle is open. bool
close() Close a handle. bool
add() Add a log entry to chosen file. bool
clear() Clear entries from chosen file. bool
remove() Remove/delete the chosen file. bool
should_rotate() Check if log file should be rotated. bool
log_rotate() Rotate log files. mixed
increment_log_infix() Increment a log file suffix. bool
get_log_file_path() Get a log file path. bool|string
get_log_file_name() Get a log file name. bool|string
cache_log() Cache log to write later. mixed
write_cached_logs() Write cached logs. mixed
delete_logs_before_timestamp() Delete all logs older than a defined timestamp. mixed
get_log_files() Get all log files in the log directory. array
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

$cached_logs

Cache logs that could not be written.

protected array $cached_logs = array()
If a log is written too early in the request, pluggable functions may be unavailable. These logs will be cached and written on 'plugins_loaded' action.

Methods

__construct()

Constructor for the logger.

public __construct( [ $log_size_limit : int = null ] ) : mixed
Parameters
$log_size_limit : int = null

Optional. Size limit for log files. Default 5mb.

Return values
mixed

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

{ Additional information for log handlers.

@type string $source Optional. Determines log file to write to. Default 'log'.
@type bool $_legacy Optional. Default false. True to use outdated log format
    originally used in deprecated WC_Logger::add calls.

}

Return values
bool

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

format_entry()

Builds a log entry text from timestamp, level 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.

open()

Open log file for writing.

protected open( $handle : string [, $mode : string = 'a' ] ) : bool
Parameters
$handle : string

Log handle.

$mode : string = 'a'

Optional. File mode. Default 'a'.

Return values
bool

Success.

is_open()

Check if a handle is open.

protected is_open( $handle : string ) : bool
Parameters
$handle : string

Log handle.

Return values
bool

True if $handle is open.

close()

Close a handle.

protected close( $handle : string ) : bool
Parameters
$handle : string

Log handle.

Return values
bool

success

add()

Add a log entry to chosen file.

protected add( $entry : string , $handle : string ) : bool
Parameters
$entry : string

Log entry text.

$handle : string

Log entry handle.

Return values
bool

True if write was successful.

clear()

Clear entries from chosen file.

public clear( $handle : string ) : bool
Parameters
$handle : string

Log handle.

Return values
bool

remove()

Remove/delete the chosen file.

public remove( $handle : string ) : bool
Parameters
$handle : string

Log handle.

Return values
bool

should_rotate()

Check if log file should be rotated.

protected should_rotate( $handle : string ) : bool

Compares the size of the log file to determine whether it is over the size limit.

Parameters
$handle : string

Log handle.

Return values
bool

True if if should be rotated.

log_rotate()

Rotate log files.

protected log_rotate( $handle : string ) : mixed

Logs are rotated by prepending '.x' to the '.log' suffix. The current log plus 10 historical logs are maintained. For example: base.9.log -> [ REMOVED ] base.8.log -> base.9.log ... base.0.log -> base.1.log base.log -> base.0.log

Parameters
$handle : string

Log handle.

Return values
mixed

increment_log_infix()

Increment a log file suffix.

protected increment_log_infix( $handle : string [, $number : null|int = null ] ) : bool
Parameters
$handle : string

Log handle.

$number : null|int = null

Optional. Default null. Log suffix number to be incremented.

Return values
bool

True if increment was successful, otherwise false.

get_log_file_path()

Get a log file path.

public static get_log_file_path( $handle : string ) : bool|string
Parameters
$handle : string

Log name.

Return values
bool|string

The log file path or false if path cannot be determined.

get_log_file_name()

Get a log file name.

public static get_log_file_name( $handle : string ) : bool|string

File names consist of the handle, followed by the date, followed by a hash, .log.

Parameters
$handle : string

Log name.

Tags
since
Return values
bool|string

The log file name or false if cannot be determined.

cache_log()

Cache log to write later.

protected cache_log( $entry : string , $handle : string ) : mixed
Parameters
$entry : string

Log entry text.

$handle : string

Log entry handle.

Return values
mixed

delete_logs_before_timestamp()

Delete all logs older than a defined timestamp.

public static delete_logs_before_timestamp( $timestamp : int ) : mixed
Parameters
$timestamp : int

Timestamp to delete logs before.

Tags
since
Return values
mixed

get_log_files()

Get all log files in the log directory.

public static get_log_files( ) : array
Tags
since
Return values
array

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