WC_Log_Handler_File
extends WC_Log_Handler
in package
Handles log entries by writing to a file.
Tags
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.
- get_backtrace() : array<string|int, mixed>
- Get a backtrace that shows where the logging function was called.
- 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.
$handles
Stores open file handles.
protected
array<string|int, mixed>
$handles
= array()
$log_size_limit
File size limit for log files in bytes.
protected
int
$log_size_limit
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 —__destruct()
Destructor.
public
__destruct() : mixed
Cleans up open file handles.
Return values
mixed —clear()
Clear entries from chosen file.
public
clear(string $handle) : bool
Parameters
- $handle : string
-
Log handle.
Return values
bool —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
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
Return values
bool|string — The 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|string — The log file path or false if path cannot be determined.get_log_files()
Get all log files in the log directory.
public
static get_log_files() : array<string|int, mixed>
Tags
Return values
array<string|int, 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>
-
{ 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.remove()
Remove/delete the chosen file.
public
remove(string $handle) : bool
Parameters
- $handle : string
-
Log handle.
Return values
bool —write_cached_logs()
Write cached logs.
public
write_cached_logs() : mixed
Return values
mixed —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
bool — True 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 —close()
Close a handle.
protected
close(string $handle) : bool
Parameters
- $handle : string
-
Log handle.
Return values
bool — successformat_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
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> —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
bool — True if increment was successful, otherwise false.is_open()
Check if a handle is open.
protected
is_open(string $handle) : bool
Parameters
- $handle : string
-
Log handle.
Return values
bool — True if $handle is open.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
bool — Success.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.