WooCommerce Code Reference

WC_Log_Handler_File extends WC_Log_Handler

Handles log entries by writing to a file.

Tags
class

WC_Log_Handler_File

version
1.0.0

Table of Contents

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

Properties

$cached_logs

Cache logs that could not be written.

protected array<string|int, mixed> $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([int $log_size_limit = null ]) : mixed
Parameters
$log_size_limit : int = null

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

Return values
mixed

delete_logs_before_timestamp()

Delete all logs older than a defined timestamp.

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

Timestamp to delete logs before.

Tags
since
3.4.0
Return values
mixed

get_log_file_name()

Get a log file name.

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

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

Parameters
$handle : string

Log name.

Tags
since
3.3
Return values
bool|stringThe log file name or false if cannot be determined.

get_log_file_path()

Get a log file path.

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

Log name.

Return values
bool|stringThe log file path or false if path cannot be determined.

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>

{ 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
boolFalse if value was not handled and true if value was handled.

add()

Add a log entry to chosen file.

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

Log entry text.

$handle : string

Log entry handle.

Return values
boolTrue if write was successful.

cache_log()

Cache log to write later.

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

Log entry text.

$handle : string

Log entry handle.

Return values
mixed

format_entry()

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

increment_log_infix()

Increment a log file suffix.

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

Log handle.

$number : null|int = null

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

Return values
boolTrue if increment was successful, otherwise false.

log_rotate()

Rotate log files.

protected log_rotate(string $handle) : 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

open()

Open log file for writing.

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

Log handle.

$mode : string = 'a'

Optional. File mode. Default 'a'.

Return values
boolSuccess.

should_rotate()

Check if log file should be rotated.

protected should_rotate(string $handle) : bool

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

Parameters
$handle : string

Log handle.

Return values
boolTrue if if should be rotated.