class-wc-log-handler-file.php
<?php
use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Utilities\LoggingUtil;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WC_Log_Handler_File extends WC_Log_Handler {
protected $handles = array();
protected $log_size_limit;
protected $cached_logs = array();
public function __construct( $log_size_limit = null ) {
if ( null === $log_size_limit ) {
$log_size_limit = 5 * 1024 * 1024;
}
$this->log_size_limit = apply_filters( 'woocommerce_log_file_size_limit', $log_size_limit );
add_action( 'plugins_loaded', array( $this, 'write_cached_logs' ) );
}
public function __destruct() {
foreach ( $this->handles as $handle ) {
if ( is_resource( $handle ) ) {
fclose( $handle );
}
}
}
public function handle( $timestamp, $level, $message, $context ) {
if ( isset( $context['source'] ) && $context['source'] ) {
$handle = $context['source'];
} else {
$handle = 'log';
}
$entry = self::format_entry( $timestamp, $level, $message, $context );
return $this->add( $entry, $handle );
}
protected static function format_entry( $timestamp, $level, $message, $context ) {
if ( isset( $context['_legacy'] ) && true === $context['_legacy'] ) {
if ( isset( $context['source'] ) && $context['source'] ) {
$handle = $context['source'];
} else {
$handle = 'log';
}
$message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
$time = date_i18n( 'm-d-Y @ H:i:s' );
$entry = "{$time} - {$message}";
} else {
$entry = parent::format_entry( $timestamp, $level, $message, $context );
}
return $entry;
}
protected function open( $handle, $mode = 'a' ) {
if ( $this->is_open( $handle ) ) {
return true;
}
$file = self::get_log_file_path( $handle );
if ( $file ) {
if ( ! file_exists( $file ) ) {
$temphandle = @fopen( $file, 'w+' );
if ( is_resource( $temphandle ) ) {
@fclose( $temphandle );
if ( Constants::is_defined( 'FS_CHMOD_FILE' ) ) {
@chmod( $file, FS_CHMOD_FILE );
}
}
}
$resource = @fopen( $file, $mode );
if ( $resource ) {
$this->handles[ $handle ] = $resource;
return true;
}
}
return false;
}
protected function is_open( $handle ) {
return array_key_exists( $handle, $this->handles ) && is_resource( $this->handles[ $handle ] );
}
protected function close( $handle ) {
$result = false;
if ( $this->is_open( $handle ) ) {
$result = fclose( $this->handles[ $handle ] );
unset( $this->handles[ $handle ] );
}
return $result;
}
protected function add( $entry, $handle ) {
$result = false;
if ( $this->should_rotate( $handle ) ) {
$this->log_rotate( $handle );
}
if ( $this->open( $handle ) && is_resource( $this->handles[ $handle ] ) ) {
$result = fwrite( $this->handles[ $handle ], $entry . PHP_EOL );
} else {
$this->cache_log( $entry, $handle );
}
return false !== $result;
}
public function clear( $handle ) {
$result = false;
$this->close( $handle );
if ( $this->open( $handle, 'w' ) && is_resource( $this->handles[ $handle ] ) ) {
$result = true;
}
do_action( 'woocommerce_log_clear', $handle );
return $result;
}
public function remove( $handle ) {
$removed = false;
$logs = $this->get_log_files();
$log_directory = LoggingUtil::get_log_directory();
$handle = sanitize_title( $handle );
if ( isset( $logs[ $handle ] ) && $logs[ $handle ] ) {
$file = realpath( trailingslashit( $log_directory ) . $logs[ $handle ] );
if ( 0 === stripos( $file, realpath( trailingslashit( $log_directory ) ) ) && is_file( $file ) && is_writable( $file ) ) {
$this->close( $file );
$removed = unlink( $file );
}
do_action( 'woocommerce_log_remove', $handle, $removed );
}
return $removed;
}
protected function should_rotate( $handle ) {
$file = self::get_log_file_path( $handle );
if ( $file ) {
if ( $this->is_open( $handle ) ) {
$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;
}
} else {
return false;
}
}
protected function log_rotate( $handle ) {
for ( $i = 8; $i >= 0; $i-- ) {
$this->increment_log_infix( $handle, $i );
}
$this->increment_log_infix( $handle );
}
protected function increment_log_infix( $handle, $number = null ) {
if ( null === $number ) {
$suffix = '';
$next_suffix = '.0';
} else {
$suffix = '.' . $number;
$next_suffix = '.' . ( $number + 1 );
}
$rename_from = self::get_log_file_path( "{$handle}{$suffix}" );
$rename_to = self::get_log_file_path( "{$handle}{$next_suffix}" );
if ( $this->is_open( $rename_from ) ) {
$this->close( $rename_from );
}
if ( is_writable( $rename_from ) ) {
return rename( $rename_from, $rename_to );
} else {
return false;
}
}
public static function get_log_file_path( $handle ) {
$log_directory = LoggingUtil::get_log_directory();
if ( function_exists( 'wp_hash' ) ) {
return trailingslashit( $log_directory ) . self::get_log_file_name( $handle );
} else {
wc_doing_it_wrong( __METHOD__, __( 'This method should not be called before plugins_loaded.', 'woocommerce' ), '3.0' );
return false;
}
}
public static function get_log_file_name( $handle ) {
if ( function_exists( 'wp_hash' ) ) {
$date_suffix = date( 'Y-m-d', time() );
$hash_suffix = wp_hash( $handle );
return sanitize_file_name( implode( '-', array( $handle, $date_suffix, $hash_suffix ) ) . '.log' );
} else {
wc_doing_it_wrong( __METHOD__, __( 'This method should not be called before plugins_loaded.', 'woocommerce' ), '3.3' );
return false;
}
}
protected function cache_log( $entry, $handle ) {
$this->cached_logs[] = array(
'entry' => $entry,
'handle' => $handle,
);
}
public function write_cached_logs() {
foreach ( $this->cached_logs as $log ) {
$this->add( $log['entry'], $log['handle'] );
}
}
public static function delete_logs_before_timestamp( $timestamp = 0 ) {
if ( ! $timestamp ) {
return;
}
$log_files = self::get_log_files();
$log_directory = LoggingUtil::get_log_directory();
foreach ( $log_files as $log_file ) {
$last_modified = filemtime( trailingslashit( $log_directory ) . $log_file );
if ( $last_modified < $timestamp ) {
@unlink( trailingslashit( $log_directory ) . $log_file );
}
}
}
public static function get_log_files() {
$log_directory = LoggingUtil::get_log_directory();
$files = @scandir( $log_directory );
$result = array();
if ( ! empty( $files ) ) {
foreach ( $files as $key => $value ) {
if ( ! in_array( $value, array( '.', '..' ), true ) ) {
if ( ! is_dir( $value ) && strstr( $value, '.log' ) ) {
$result[ sanitize_title( $value ) ] = $value;
}
}
}
}
return $result;
}
}