2016-11-12 18:34:10 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles log entries by writing to a file.
|
|
|
|
*
|
|
|
|
* @class WC_Log_Handler_File
|
|
|
|
* @version 1.0.0
|
2016-11-20 14:18:06 +00:00
|
|
|
* @package WooCommerce/Classes/Log_Handlers
|
2016-11-12 18:34:10 +00:00
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2016-11-12 19:46:58 +00:00
|
|
|
class WC_Log_Handler_File extends WC_Log_Handler {
|
2016-11-12 18:34:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores open file handles.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-12-16 20:34:15 +00:00
|
|
|
protected $handles = array();
|
2016-11-12 18:34:10 +00:00
|
|
|
|
2016-11-19 21:29:43 +00:00
|
|
|
/**
|
|
|
|
* File size limit for log files in bytes.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-12-16 20:34:15 +00:00
|
|
|
protected $log_size_limit;
|
2016-11-19 21:29:43 +00:00
|
|
|
|
2016-12-19 20:54:54 +00:00
|
|
|
/**
|
|
|
|
* Cache logs that could not be written.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $cached_logs = array();
|
|
|
|
|
2016-11-19 21:29:43 +00:00
|
|
|
/**
|
|
|
|
* Constructor for the logger.
|
|
|
|
*
|
2016-12-11 11:02:40 +00:00
|
|
|
* @param int $log_size_limit Optional. Size limit for log files. Default 5mb.
|
2016-11-19 21:29:43 +00:00
|
|
|
*/
|
2016-12-11 11:02:40 +00:00
|
|
|
public function __construct( $log_size_limit = null ) {
|
2016-11-19 21:29:43 +00:00
|
|
|
|
2016-12-11 11:02:40 +00:00
|
|
|
if ( null === $log_size_limit ) {
|
|
|
|
$log_size_limit = 5 * 1024 * 1024;
|
|
|
|
}
|
2016-11-19 21:29:43 +00:00
|
|
|
|
2016-12-11 11:02:40 +00:00
|
|
|
$this->log_size_limit = $log_size_limit;
|
2016-12-19 20:54:54 +00:00
|
|
|
|
|
|
|
add_action( 'plugins_loaded', array( $this, 'write_cached_logs' ) );
|
2016-11-19 21:29:43 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 18:34:10 +00:00
|
|
|
/**
|
|
|
|
* Destructor.
|
2016-11-19 21:29:43 +00:00
|
|
|
*
|
|
|
|
* Cleans up open file handles.
|
2016-11-12 18:34:10 +00:00
|
|
|
*/
|
|
|
|
public function __destruct() {
|
2016-11-16 19:19:20 +00:00
|
|
|
foreach ( $this->handles as $handle ) {
|
2016-11-12 18:34:10 +00:00
|
|
|
if ( is_resource( $handle ) ) {
|
|
|
|
fclose( $handle );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a log entry.
|
|
|
|
*
|
2016-11-20 13:32:46 +00:00
|
|
|
* @param int $timestamp Log timestamp.
|
2016-11-12 18:34:10 +00:00
|
|
|
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
|
2016-11-20 13:32:46 +00:00
|
|
|
* @param string $message Log message.
|
2016-11-12 18:34:10 +00:00
|
|
|
* @param array $context {
|
2016-11-20 13:32:46 +00:00
|
|
|
* Additional information for log handlers.
|
2016-11-12 18:34:10 +00:00
|
|
|
*
|
2016-12-21 19:15:19 +00:00
|
|
|
* @type string $source Optional. Determines log file to write to. Default 'log'.
|
2016-12-18 20:44:56 +00:00
|
|
|
* @type bool $_legacy Optional. Default false. True to use outdated log format
|
2017-03-28 17:58:51 +00:00
|
|
|
* originally used in deprecated WC_Logger::add calls.
|
2016-11-12 18:34:10 +00:00
|
|
|
* }
|
|
|
|
*
|
2016-12-15 14:34:48 +00:00
|
|
|
* @return bool False if value was not handled and true if value was handled.
|
2016-11-12 18:34:10 +00:00
|
|
|
*/
|
2016-11-20 13:32:46 +00:00
|
|
|
public function handle( $timestamp, $level, $message, $context ) {
|
2016-11-12 18:34:10 +00:00
|
|
|
|
2016-12-21 19:15:19 +00:00
|
|
|
if ( isset( $context['source'] ) && $context['source'] ) {
|
|
|
|
$handle = $context['source'];
|
2016-11-14 21:06:14 +00:00
|
|
|
} else {
|
|
|
|
$handle = 'log';
|
|
|
|
}
|
2016-11-12 18:34:10 +00:00
|
|
|
|
2016-12-16 19:44:09 +00:00
|
|
|
$entry = self::format_entry( $timestamp, $level, $message, $context );
|
2016-11-12 19:46:58 +00:00
|
|
|
|
2016-12-10 21:21:41 +00:00
|
|
|
return $this->add( $entry, $handle );
|
2016-11-12 19:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-20 13:32:46 +00:00
|
|
|
* Builds a log entry text from timestamp, level and message.
|
2016-11-12 19:46:58 +00:00
|
|
|
*
|
2016-11-20 13:32:46 +00:00
|
|
|
* @param int $timestamp Log timestamp.
|
2016-11-12 19:46:58 +00:00
|
|
|
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
|
2016-11-20 13:32:46 +00:00
|
|
|
* @param string $message Log message.
|
|
|
|
* @param array $context Additional information for log handlers.
|
2016-11-14 21:06:14 +00:00
|
|
|
*
|
2016-11-20 13:32:46 +00:00
|
|
|
* @return string Formatted log entry.
|
2016-11-12 19:46:58 +00:00
|
|
|
*/
|
2016-12-16 20:34:15 +00:00
|
|
|
protected static function format_entry( $timestamp, $level, $message, $context ) {
|
2016-11-12 19:46:58 +00:00
|
|
|
|
|
|
|
if ( isset( $context['_legacy'] ) && true === $context['_legacy'] ) {
|
2016-12-21 19:15:19 +00:00
|
|
|
if ( isset( $context['source'] ) && $context['source'] ) {
|
|
|
|
$handle = $context['source'];
|
2016-11-14 21:06:14 +00:00
|
|
|
} else {
|
|
|
|
$handle = 'log';
|
|
|
|
}
|
2016-11-12 19:46:58 +00:00
|
|
|
$message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
|
2016-11-19 16:47:29 +00:00
|
|
|
$time = date_i18n( 'm-d-Y @ H:i:s' );
|
|
|
|
$entry = "{$time} - {$message}";
|
2016-11-12 19:46:58 +00:00
|
|
|
} else {
|
2016-11-20 13:32:46 +00:00
|
|
|
$entry = parent::format_entry( $timestamp, $level, $message, $context );
|
2016-11-12 19:46:58 +00:00
|
|
|
}
|
2016-11-12 18:34:10 +00:00
|
|
|
|
2016-11-12 19:46:58 +00:00
|
|
|
return $entry;
|
2016-11-12 18:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open log file for writing.
|
|
|
|
*
|
2016-11-20 13:32:46 +00:00
|
|
|
* @param string $handle Log handle.
|
|
|
|
* @param string $mode Optional. File mode. Default 'a'.
|
|
|
|
* @return bool Success.
|
2016-11-12 18:34:10 +00:00
|
|
|
*/
|
|
|
|
protected function open( $handle, $mode = 'a' ) {
|
2016-11-19 21:29:43 +00:00
|
|
|
if ( $this->is_open( $handle ) ) {
|
2016-11-12 18:34:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-19 20:28:55 +00:00
|
|
|
$file = self::get_log_file_path( $handle );
|
2016-11-12 18:34:10 +00:00
|
|
|
|
2016-12-19 20:28:55 +00:00
|
|
|
if ( $file ) {
|
|
|
|
if ( ! file_exists( $file ) ) {
|
|
|
|
$temphandle = @fopen( $file, 'w+' );
|
|
|
|
@fclose( $temphandle );
|
|
|
|
|
|
|
|
if ( defined( 'FS_CHMOD_FILE' ) ) {
|
|
|
|
@chmod( $file, FS_CHMOD_FILE );
|
|
|
|
}
|
2016-11-12 18:34:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:37:18 +00:00
|
|
|
if ( $resource = @fopen( $file, $mode ) ) {
|
|
|
|
$this->handles[ $handle ] = $resource;
|
2016-12-19 20:28:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-12 18:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-19 21:29:43 +00:00
|
|
|
/**
|
|
|
|
* Check if a handle is open.
|
|
|
|
*
|
|
|
|
* @param string $handle Log handle.
|
|
|
|
* @return bool True if $handle is open.
|
|
|
|
*/
|
|
|
|
protected function is_open( $handle ) {
|
2017-04-20 15:57:33 +00:00
|
|
|
return array_key_exists( $handle, $this->handles ) && is_resource( $this->handles[ $handle ] );
|
2016-11-19 21:29:43 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 18:34:10 +00:00
|
|
|
/**
|
|
|
|
* Close a handle.
|
|
|
|
*
|
|
|
|
* @param string $handle
|
|
|
|
* @return bool success
|
|
|
|
*/
|
|
|
|
protected function close( $handle ) {
|
|
|
|
$result = false;
|
|
|
|
|
2017-04-20 15:53:44 +00:00
|
|
|
if ( $this->is_open( $handle ) ) {
|
2016-11-16 19:19:20 +00:00
|
|
|
$result = fclose( $this->handles[ $handle ] );
|
|
|
|
unset( $this->handles[ $handle ] );
|
2016-11-12 18:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a log entry to chosen file.
|
|
|
|
*
|
2016-11-14 20:55:21 +00:00
|
|
|
* @param string $entry Log entry text
|
2016-12-19 20:28:55 +00:00
|
|
|
* @param string $handle Log entry handle
|
2016-11-12 18:34:10 +00:00
|
|
|
*
|
2016-11-14 20:55:21 +00:00
|
|
|
* @return bool True if write was successful.
|
2016-11-12 18:34:10 +00:00
|
|
|
*/
|
2016-12-16 20:34:15 +00:00
|
|
|
protected function add( $entry, $handle ) {
|
2016-11-12 18:34:10 +00:00
|
|
|
$result = false;
|
|
|
|
|
2016-11-19 21:29:43 +00:00
|
|
|
if ( $this->should_rotate( $handle ) ) {
|
|
|
|
$this->log_rotate( $handle );
|
|
|
|
}
|
|
|
|
|
2016-11-16 19:19:20 +00:00
|
|
|
if ( $this->open( $handle ) && is_resource( $this->handles[ $handle ] ) ) {
|
2016-11-20 13:34:26 +00:00
|
|
|
$result = fwrite( $this->handles[ $handle ], $entry . PHP_EOL );
|
2016-12-19 20:54:54 +00:00
|
|
|
} else {
|
|
|
|
$this->cache_log( $entry, $handle );
|
2016-11-12 18:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false !== $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear entries from chosen file.
|
|
|
|
*
|
|
|
|
* @param string $handle
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function clear( $handle ) {
|
|
|
|
$result = false;
|
|
|
|
|
|
|
|
// Close the file if it's already open.
|
|
|
|
$this->close( $handle );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $this->open( $handle, 'w' ) == Open the file for writing only. Place the file pointer at
|
|
|
|
* the beginning of the file, and truncate the file to zero length.
|
|
|
|
*/
|
2016-11-16 19:19:20 +00:00
|
|
|
if ( $this->open( $handle, 'w' ) && is_resource( $this->handles[ $handle ] ) ) {
|
2016-11-12 18:34:10 +00:00
|
|
|
$result = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
do_action( 'woocommerce_log_clear', $handle );
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove/delete the chosen file.
|
|
|
|
*
|
|
|
|
* @param string $handle
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function remove( $handle ) {
|
|
|
|
$removed = false;
|
2016-12-19 20:28:55 +00:00
|
|
|
$file = self::get_log_file_path( $handle );
|
|
|
|
|
|
|
|
if ( $file ) {
|
|
|
|
if ( is_file( $file ) && is_writable( $file ) ) {
|
2017-02-09 15:42:25 +00:00
|
|
|
$this->close( $handle ); // Close first to be certain no processes keep it alive after it is unlinked.
|
2016-12-19 20:28:55 +00:00
|
|
|
$removed = unlink( $file );
|
|
|
|
}
|
|
|
|
do_action( 'woocommerce_log_remove', $handle, $removed );
|
|
|
|
}
|
2016-11-12 18:34:10 +00:00
|
|
|
|
|
|
|
return $removed;
|
|
|
|
}
|
2016-11-19 18:28:12 +00:00
|
|
|
|
2016-11-19 21:29:43 +00:00
|
|
|
/**
|
|
|
|
* Check if log file should be rotated.
|
|
|
|
*
|
|
|
|
* Compares the size of the log file to determine whether it is over the size limit.
|
|
|
|
*
|
|
|
|
* @param string $handle Log handle
|
|
|
|
* @return bool True if if should be rotated.
|
|
|
|
*/
|
|
|
|
protected function should_rotate( $handle ) {
|
2016-12-19 20:28:55 +00:00
|
|
|
$file = self::get_log_file_path( $handle );
|
|
|
|
if ( $file ) {
|
2017-04-20 15:53:44 +00:00
|
|
|
if ( $this->is_open( $handle ) ) {
|
2016-12-19 20:28:55 +00:00
|
|
|
$file_stat = fstat( $this->handles[ $handle ] );
|
|
|
|
return $file_stat['size'] > $this->log_size_limit;
|
|
|
|
} elseif ( file_exists( $file ) ) {
|
|
|
|
return filesize( $file ) > $this->log_size_limit;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-19 21:29:43 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-19 18:28:12 +00:00
|
|
|
}
|
2016-11-19 21:29:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotate log files.
|
|
|
|
*
|
|
|
|
* Logs are rotatated 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
|
|
|
|
*
|
|
|
|
* @param string $handle Log handle
|
|
|
|
*/
|
|
|
|
protected function log_rotate( $handle ) {
|
|
|
|
for ( $i = 8; $i >= 0; $i-- ) {
|
|
|
|
$this->increment_log_infix( $handle, $i );
|
|
|
|
}
|
|
|
|
$this->increment_log_infix( $handle );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increment a log file suffix.
|
|
|
|
*
|
|
|
|
* @param string $handle Log handle
|
|
|
|
* @param null|int $number Optional. Default null. Log suffix number to be incremented.
|
|
|
|
* @return bool True if increment was successful, otherwise false.
|
|
|
|
*/
|
|
|
|
protected function increment_log_infix( $handle, $number = null ) {
|
|
|
|
if ( null === $number ) {
|
|
|
|
$suffix = '';
|
|
|
|
$next_suffix = '.0';
|
|
|
|
} else {
|
|
|
|
$suffix = '.' . $number;
|
|
|
|
$next_suffix = '.' . ($number + 1);
|
|
|
|
}
|
|
|
|
|
2016-12-19 20:28:55 +00:00
|
|
|
$rename_from = self::get_log_file_path( "{$handle}{$suffix}" );
|
|
|
|
$rename_to = self::get_log_file_path( "{$handle}{$next_suffix}" );
|
2016-11-19 21:29:43 +00:00
|
|
|
|
|
|
|
if ( $this->is_open( $rename_from ) ) {
|
|
|
|
$this->close( $rename_from );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_writable( $rename_from ) ) {
|
|
|
|
return rename( $rename_from, $rename_to );
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-19 20:28:55 +00:00
|
|
|
/**
|
|
|
|
* Get a log file path.
|
|
|
|
*
|
|
|
|
* @param string $handle Log name.
|
|
|
|
* @return bool|string The log file path or false if path cannot be determined.
|
|
|
|
*/
|
|
|
|
public static function get_log_file_path( $handle ) {
|
|
|
|
if ( function_exists( 'wp_hash' ) ) {
|
2017-02-09 15:42:25 +00:00
|
|
|
return trailingslashit( WC_LOG_DIR ) . sanitize_file_name( $handle . '-' . wp_hash( $handle ) . '.log' );
|
2017-01-12 20:57:37 +00:00
|
|
|
} else {
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_doing_it_wrong( __METHOD__, __( 'This method should not be called before plugins_loaded.', 'woocommerce' ), '3.0' );
|
2017-01-12 20:57:37 +00:00
|
|
|
return false;
|
2016-12-19 20:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 20:54:54 +00:00
|
|
|
/**
|
|
|
|
* Cache log to write later.
|
|
|
|
*
|
|
|
|
* @param string $entry Log entry text
|
|
|
|
* @param string $handle Log entry handle
|
|
|
|
*/
|
|
|
|
protected function cache_log( $entry, $handle ) {
|
|
|
|
$this->cached_logs[] = array(
|
|
|
|
'entry' => $entry,
|
|
|
|
'handle' => $handle,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write cached logs.
|
|
|
|
*/
|
|
|
|
public function write_cached_logs() {
|
|
|
|
foreach ( $this->cached_logs as $log ) {
|
|
|
|
$this->add( $log['entry'], $log['handle'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 18:34:10 +00:00
|
|
|
}
|