woocommerce/includes/class-wc-logger.php

92 lines
1.6 KiB
PHP
Raw Normal View History

<?php
2015-11-06 09:22:19 +00:00
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
2015-11-03 13:53:50 +00:00
* Allows log files to be written to for debugging purposes
*
2012-01-27 16:38:39 +00:00
* @class WC_Logger
2012-08-14 19:42:38 +00:00
* @version 1.6.4
2012-08-14 22:43:48 +00:00
* @package WooCommerce/Classes
2013-02-20 17:14:46 +00:00
* @category Class
2012-08-14 19:42:38 +00:00
* @author WooThemes
*/
2012-01-27 16:38:39 +00:00
class WC_Logger {
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/**
2012-12-14 22:00:04 +00:00
* @var array Stores open file _handles.
2012-08-15 17:08:42 +00:00
* @access private
*/
private $_handles;
2012-08-14 19:42:38 +00:00
/**
2012-08-15 17:08:42 +00:00
* Constructor for the logger.
2012-08-14 19:42:38 +00:00
*/
2012-12-14 21:44:43 +00:00
public function __construct() {
2012-12-14 22:00:04 +00:00
$this->_handles = array();
}
2012-08-14 19:42:38 +00:00
/**
* Destructor.
*/
2012-12-14 21:44:43 +00:00
public function __destruct() {
foreach ( $this->_handles as $handle ) {
@fclose( $handle );
}
}
2012-08-14 19:42:38 +00:00
/**
2012-08-14 19:42:38 +00:00
* Open log file for writing.
*
* @access private
* @param mixed $handle
* @return bool success
*/
private function open( $handle ) {
if ( isset( $this->_handles[ $handle ] ) ) {
2012-08-14 19:42:38 +00:00
return true;
}
2012-08-14 19:42:38 +00:00
if ( $this->_handles[ $handle ] = @fopen( wc_get_log_file_path( $handle ), 'a' ) ) {
2012-08-14 19:42:38 +00:00
return true;
}
2012-08-14 19:42:38 +00:00
return false;
}
2012-08-14 19:42:38 +00:00
/**
2012-08-14 19:42:38 +00:00
* Add a log entry to chosen file.
*
* @param string $handle
* @param string $message
*/
public function add( $handle, $message ) {
2013-01-10 14:56:07 +00:00
if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) {
$time = date_i18n( 'm-d-Y @ H:i:s -' ); // Grab Time
2013-01-10 14:56:38 +00:00
@fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" );
2012-08-14 19:42:38 +00:00
}
2015-07-16 19:55:48 +00:00
do_action( 'woocommerce_log_add', $handle, $message );
}
2012-08-14 19:42:38 +00:00
/**
2013-03-03 17:07:31 +00:00
* Clear entries from chosen file.
2012-08-14 19:42:38 +00:00
*
* @param mixed $handle
*/
public function clear( $handle ) {
if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) {
2013-01-10 14:56:38 +00:00
@ftruncate( $this->_handles[ $handle ], 0 );
}
2015-07-16 19:55:48 +00:00
do_action( 'woocommerce_log_clear', $handle );
}
2012-11-27 16:22:47 +00:00
}