[2.2] Log changes Closes #4926

Added WC_LOG_DIR constant
Added log viewer
Changed default logging directory to 1 up from ABSPATH inside wc-logs
This commit is contained in:
Mike Jolley 2014-05-28 11:12:35 +01:00
parent 3fbd036b81
commit 1ed0938581
12 changed files with 105 additions and 24 deletions

File diff suppressed because one or more lines are too long

View File

@ -181,6 +181,15 @@ table.wc_status_table {
outline: 0; outline: 0;
} }
} }
#log-viewer-select {
padding: 10px 0 8px;
line-height: 180%;
}
#log-viewer {
textarea {
width: 100%;
}
}
/* Bulk/Quick edit */ /* Bulk/Quick edit */
.inline-edit-product.quick-edit-row { .inline-edit-product.quick-edit-row {

View File

@ -30,8 +30,7 @@ class WC_Admin_Status {
* Handles output of report * Handles output of report
*/ */
public function status_report() { public function status_report() {
global $woocommerce, $wpdb; global $wpdb;
include_once( 'views/html-admin-page-status-report.php' ); include_once( 'views/html-admin-page-status-report.php' );
} }
@ -209,6 +208,19 @@ class WC_Admin_Status {
) ); ) );
} }
/**
* Show the logs page
*/
public function status_logs() {
$logs = $this->scan_log_files();
if ( ! empty( $_POST['log_file'] ) && isset( $logs[ sanitize_title( $_POST['log_file'] ) ] ) ) {
$viewed_log = $logs[ sanitize_title( $_POST['log_file'] ) ];
} elseif ( $logs ) {
$viewed_log = current( $logs );
}
include_once( 'views/html-admin-page-status-logs.php' );
}
/** /**
* Retrieve metadata from a file. Based on WP Core's get_file_data function * Retrieve metadata from a file. Based on WP Core's get_file_data function
* *
@ -239,7 +251,6 @@ class WC_Admin_Status {
/** /**
* Scan the template files * Scan the template files
* *
* @access public
* @param string $template_path * @param string $template_path
* @return array * @return array
*/ */
@ -262,6 +273,26 @@ class WC_Admin_Status {
} }
return $result; return $result;
} }
/**
* Scan the log files
*
* @return array
*/
public function scan_log_files() {
$files = scandir( WC_LOG_DIR );
$result = array();
if ( $files ) {
foreach ( $files as $key => $value ) {
if ( ! in_array( $value, array( ".",".." ) ) ) {
if ( ! is_dir( $value ) && strstr( $value, '.log' ) ) {
$result[ sanitize_title( $value ) ] = $value;
}
}
}
}
return $result;
}
} }
endif; endif;

View File

@ -0,0 +1,23 @@
<?php if ( $logs ) : ?>
<div id="log-viewer-select">
<div class="alignleft">
<h3><?php printf( __( 'Viewing %s (%s)', 'woocommerce' ), esc_html( $viewed_log ), date_i18n( get_option( 'date_format') . ' ' . get_option( 'time_format'), filemtime( WC_LOG_DIR . $viewed_log ) ) ); ?></h3>
</div>
<div class="alignright">
<form action="<?php echo admin_url( 'admin.php?page=wc-status&tab=logs' ); ?>" method="post">
<select name="log_file">
<?php foreach ( $logs as $log_key => $log_file ) : ?>
<option value="<?php echo esc_attr( $log_key ); ?>" <?php selected( sanitize_title( $viewed_log ), $log_key ); ?>><?php echo esc_html( $log_file ); ?> (<?php echo date_i18n( get_option( 'date_format') . ' ' . get_option( 'time_format'), filemtime( WC_LOG_DIR . $log_file ) ); ?>)</option>
<?php endforeach; ?>
</select>
<input type="submit" class="button" value="<?php esc_attr_e( 'View', 'woocommerce' ); ?>" />
</form>
</div>
<div class="clear"></div>
</div>
<div id="log-viewer">
<textarea cols="70" rows="25"><?php echo esc_textarea( file_get_contents( WC_LOG_DIR . $viewed_log ) ); ?></textarea>
</div>
<?php else : ?>
<div class="updated woocommerce-message below-h2"><p><?php _e( 'There are currently no logs to view.', 'woocommerce' ); ?></p></div>
<?php endif; ?>

View File

@ -100,10 +100,11 @@
<tr> <tr>
<td><?php _e( 'WC Logging','woocommerce' ); ?>:</td> <td><?php _e( 'WC Logging','woocommerce' ); ?>:</td>
<td><?php <td><?php
if ( @fopen( WC()->plugin_path() . '/logs/paypal.txt', 'a' ) ) if ( @fopen( WC_LOG_DIR . 'test-log.log', 'a' ) ) {
echo '<mark class="yes">' . __( 'Log directory is writable.', 'woocommerce' ) . '</mark>'; printf( '<mark class="yes">' . __( 'Log directory (%s) is writable.', 'woocommerce' ) . '</mark>', WC_LOG_DIR );
else } else {
echo '<mark class="error">' . __( 'Log directory (<code>woocommerce/logs/</code>) is not writable. Logging will not be possible.', 'woocommerce' ) . '</mark>'; printf( '<mark class="error">' . __( 'Log directory (<code>%s</code>) is not writable. To allow logging, make this writable or define a custom <code>WC_LOG_DIR</code>.', 'woocommerce' ) . '</mark>', WC_LOG_DIR );
}
?></td> ?></td>
</tr> </tr>
<tr> <tr>

View File

@ -4,6 +4,7 @@
$tabs = array( $tabs = array(
'status' => __( 'System Status', 'woocommerce' ), 'status' => __( 'System Status', 'woocommerce' ),
'tools' => __( 'Tools', 'woocommerce' ), 'tools' => __( 'Tools', 'woocommerce' ),
'logs' => __( 'Logs', 'woocommerce' ),
); );
foreach ( $tabs as $name => $label ) { foreach ( $tabs as $name => $label ) {
echo '<a href="' . admin_url( 'admin.php?page=wc-status&tab=' . $name ) . '" class="nav-tab '; echo '<a href="' . admin_url( 'admin.php?page=wc-status&tab=' . $name ) . '" class="nav-tab ';
@ -17,6 +18,9 @@
case "tools" : case "tools" :
$this->status_tools(); $this->status_tools();
break; break;
case "logs" :
$this->status_logs();
break;
default : default :
$this->status_report(); $this->status_report();
break; break;

View File

@ -310,10 +310,13 @@ class WC_Countries {
// Load only the state files the shop owner wants/needs // Load only the state files the shop owner wants/needs
$allowed = array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() ); $allowed = array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() );
if ( $allowed ) if ( $allowed ) {
foreach ( $allowed as $CC => $country ) foreach ( $allowed as $code => $country ) {
if ( ! isset( $states[ $CC ] ) && file_exists( WC()->plugin_path() . '/i18n/states/' . $CC . '.php' ) ) if ( ! isset( $states[ $code ] ) && file_exists( WC()->plugin_path() . '/i18n/states/' . $code . '.php' ) ) {
include( WC()->plugin_path() . '/i18n/states/' . $CC . '.php' ); include( WC()->plugin_path() . '/i18n/states/' . $code . '.php' );
}
}
}
$this->states = apply_filters( 'woocommerce_states', $states ); $this->states = apply_filters( 'woocommerce_states', $states );
} }

View File

@ -608,12 +608,12 @@ class WC_Install {
'content' => '' 'content' => ''
), ),
array( array(
'base' => WP_PLUGIN_DIR . "/" . plugin_basename( dirname( dirname( __FILE__ ) ) ) . '/logs', 'base' => WC_LOG_DIR,
'file' => '.htaccess', 'file' => '.htaccess',
'content' => 'deny from all' 'content' => 'deny from all'
), ),
array( array(
'base' => WP_PLUGIN_DIR . "/" . plugin_basename( dirname( dirname( __FILE__ ) ) ) . '/logs', 'base' => WC_LOG_DIR,
'file' => 'index.html', 'file' => 'index.html',
'content' => '' 'content' => ''
) )

View File

@ -34,9 +34,10 @@ class WC_Logger {
* @return void * @return void
*/ */
public function __destruct() { public function __destruct() {
foreach ( $this->_handles as $handle ) foreach ( $this->_handles as $handle ) {
@fclose( escapeshellarg( $handle ) ); @fclose( escapeshellarg( $handle ) );
} }
}
/** /**
@ -47,12 +48,13 @@ class WC_Logger {
* @return bool success * @return bool success
*/ */
private function open( $handle ) { private function open( $handle ) {
if ( isset( $this->_handles[ $handle ] ) ) {
if ( isset( $this->_handles[ $handle ] ) )
return true; return true;
}
if ( $this->_handles[ $handle ] = @fopen( WC()->plugin_path() . '/logs/' . $this->file_name( $handle ) . '.txt', 'a' ) ) if ( $this->_handles[ $handle ] = @fopen( WC_LOG_DIR . $this->file_name( $handle ) . '.log', 'a' ) ) {
return true; return true;
}
return false; return false;
} }
@ -68,7 +70,7 @@ class WC_Logger {
*/ */
public function add( $handle, $message ) { public function add( $handle, $message ) {
if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) { if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) {
$time = date_i18n( 'm-d-Y @ H:i:s -' ); //Grab Time $time = date_i18n( 'm-d-Y @ H:i:s -' ); // Grab Time
@fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" ); @fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" );
} }
} }
@ -82,10 +84,10 @@ class WC_Logger {
* @return void * @return void
*/ */
public function clear( $handle ) { public function clear( $handle ) {
if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) {
if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) )
@ftruncate( $this->_handles[ $handle ], 0 ); @ftruncate( $this->_handles[ $handle ], 0 );
} }
}
/** /**

View File

@ -234,7 +234,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
'type' => 'checkbox', 'type' => 'checkbox',
'label' => __( 'Enable logging', 'woocommerce' ), 'label' => __( 'Enable logging', 'woocommerce' ),
'default' => 'no', 'default' => 'no',
'description' => sprintf( __( 'Log PayPal events, such as IPN requests, inside <code>woocommerce/logs/paypal-%s.txt</code>', 'woocommerce' ), sanitize_file_name( wp_hash( 'paypal' ) ) ), 'description' => sprintf( __( 'Log PayPal events, such as IPN requests, inside <code>%s/paypal-%s.log</code>', 'woocommerce' ), WC_LOG_DIR, sanitize_file_name( wp_hash( 'paypal' ) ) ),
) )
); );
} }

View File

@ -151,6 +151,9 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Dev - Add new 'wc_admin_reports_path' filter to reports. * Dev - Add new 'wc_admin_reports_path' filter to reports.
* Dev - Add user ID to shipping packages. * Dev - Add user ID to shipping packages.
* Dev - Added product id parameter to related posts filters. * Dev - Added product id parameter to related posts filters.
* Dev - WC_LOG_DIR constant for defining the log directory.
* Dev - Moved default logging directory 1 level above WordPress, rather than in the plugin folder.
* Dev - Added log viewer in System Status.
* Refactor - Update stock amounts with DB queries. * Refactor - Update stock amounts with DB queries.
* Refactor - Simplified attribute name sanitisation which maintains UTF8 char integrity. * Refactor - Simplified attribute name sanitisation which maintains UTF8 char integrity.
* Refactor - Country class return methods. * Refactor - Country class return methods.

View File

@ -239,14 +239,19 @@ final class WooCommerce {
define( 'WC_ROUNDING_PRECISION', 4 ); define( 'WC_ROUNDING_PRECISION', 4 );
} }
// 1 = PHP_ROUND_HALF_UP, 2 = PHP_ROUND_HALF_DOWN
if ( ! defined( 'WC_TAX_ROUNDING_MODE' ) ) { if ( ! defined( 'WC_TAX_ROUNDING_MODE' ) ) {
// 1 = PHP_ROUND_HALF_UP, 2 = PHP_ROUND_HALF_DOWN
define( 'WC_TAX_ROUNDING_MODE', get_option( 'woocommerce_prices_include_tax' ) === 'yes' ? 2 : 1 ); define( 'WC_TAX_ROUNDING_MODE', get_option( 'woocommerce_prices_include_tax' ) === 'yes' ? 2 : 1 );
} }
if ( ! defined( 'WC_DELIMITER' ) ) { if ( ! defined( 'WC_DELIMITER' ) ) {
define( 'WC_DELIMITER', '|' ); define( 'WC_DELIMITER', '|' );
} }
if ( ! defined( 'WC_LOG_DIR' ) ) {
// Absolute path to the folder for logs. Defaults to 1 level above WordPress.
define( 'WC_LOG_DIR', dirname( ABSPATH ) . '/wc-logs/' );
}
} }
/** /**