woocommerce/includes/log-handlers/class-wc-log-handler-file.php

219 lines
5.2 KiB
PHP
Raw Normal View History

2016-11-12 18:34:10 +00:00
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'WC_Log_Handler' ) ) {
include_once( dirname( dirname( __FILE__ ) ) . '/abstracts/abstract-wc-log-handler.php' );
}
2016-11-12 18:34:10 +00:00
/**
* Handles log entries by writing to a file.
*
* @class WC_Log_Handler_File
* @version 1.0.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
class WC_Log_Handler_File extends WC_Log_Handler {
2016-11-12 18:34:10 +00:00
/**
* Stores open file handles.
*
* @var array
* @access private
*/
private $handles = array();
2016-11-12 18:34:10 +00:00
/**
* Destructor.
*/
public function __destruct() {
foreach ( $this->handles as $handle ) {
2016-11-12 18:34:10 +00:00
if ( is_resource( $handle ) ) {
fclose( $handle );
}
}
}
/**
* Handle a log entry.
*
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
* @param string $message
* @param array $context {
* Array with additional information
2016-11-12 18:34:10 +00:00
*
* @type string $tag Optional. The tag will be used to determine which file an entry will be written to.
* }
*
* @return bool log entry should bubble to further loggers.
*/
public function handle( $level, $timestamp, $message, $context ) {
2016-11-12 18:34:10 +00:00
if ( ! $this->should_handle( $level ) ) {
return true;
}
2016-11-14 21:06:14 +00:00
if ( isset( $context['tag'] ) && $context['tag'] ) {
$handle = $context['tag'];
} else {
$handle = 'log';
}
2016-11-12 18:34:10 +00:00
$entry = $this->format_entry( $level, $timestamp, $message, $context );
2016-11-12 18:34:10 +00:00
// Bubble if add is NOT successful
return ! $this->add( $entry, $handle );
}
/**
* Builds a log entry text from level, timestamp and message.
*
* Attempt to ensure backwards compatibility for legacy WC_Logger::add calls
*
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
2016-11-14 20:40:54 +00:00
* @param int $timestamp log entry timestamp
* @param string $message provided log message
* @param array $context {
* Array with additional information
2016-11-14 21:06:14 +00:00
*
* @type string $tag Optional. The tag will be used to determine which file an entry will be written to.
2016-11-14 20:40:54 +00:00
* }
*
* @return string Formatted log entry
*/
public function format_entry( $level, $timestamp, $message, $context ) {
if ( isset( $context['_legacy'] ) && true === $context['_legacy'] ) {
2016-11-14 21:06:14 +00:00
if ( isset( $context['tag'] ) && $context['tag'] ) {
$handle = $context['tag'];
} 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( $level, $timestamp, $message, $context );
}
2016-11-12 18:34:10 +00:00
return $entry;
2016-11-12 18:34:10 +00:00
}
/**
* Open log file for writing.
*
* @param string $handle
* @param string $mode
* @return bool success
*/
protected function open( $handle, $mode = 'a' ) {
if ( isset( $this->handles[ $handle ] ) ) {
2016-11-12 18:34:10 +00:00
return true;
}
if ( ! file_exists( wc_get_log_file_path( $handle ) ) ) {
$temphandle = @fopen( wc_get_log_file_path( $handle ), 'w+' );
@fclose( $temphandle );
if ( defined( 'FS_CHMOD_FILE' ) ) {
@chmod( wc_get_log_file_path( $handle ), FS_CHMOD_FILE );
}
}
if ( $this->handles[ $handle ] = @fopen( wc_get_log_file_path( $handle ), $mode ) ) {
2016-11-12 18:34:10 +00:00
return true;
}
return false;
}
/**
* Close a handle.
*
* @param string $handle
* @return bool success
*/
protected function close( $handle ) {
$result = false;
if ( is_resource( $this->handles[ $handle ] ) ) {
$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 $handle Log entry handle
* @param string $entry Log entry text
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
*/
public function add( $entry, $handle ) {
2016-11-12 18:34:10 +00:00
$result = false;
if ( $this->open( $handle ) && is_resource( $this->handles[ $handle ] ) ) {
$result = fwrite( $this->handles[ $handle ], $entry . "\n" );
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.
*/
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;
$file = wc_get_log_file_path( $handle );
if ( is_file( $file ) && is_writable( $file ) ) {
// Close first to be certain no processes keep it alive after it is unlinked.
$this->close( $handle );
$removed = unlink( $file );
} elseif ( is_file( trailingslashit( WC_LOG_DIR ) . $handle . '.log' ) && is_writable( trailingslashit( WC_LOG_DIR ) . $handle . '.log' ) ) {
$this->close( $handle );
$removed = unlink( trailingslashit( WC_LOG_DIR ) . $handle . '.log' );
}
do_action( 'woocommerce_log_remove', $handle, $removed );
return $removed;
}
}