[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;
}
}
#log-viewer-select {
padding: 10px 0 8px;
line-height: 180%;
}
#log-viewer {
textarea {
width: 100%;
}
}
/* Bulk/Quick edit */
.inline-edit-product.quick-edit-row {

View File

@ -30,8 +30,7 @@ class WC_Admin_Status {
* Handles output of report
*/
public function status_report() {
global $woocommerce, $wpdb;
global $wpdb;
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
*
@ -239,7 +251,6 @@ class WC_Admin_Status {
/**
* Scan the template files
*
* @access public
* @param string $template_path
* @return array
*/
@ -262,6 +273,26 @@ class WC_Admin_Status {
}
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;

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>
<td><?php _e( 'WC Logging','woocommerce' ); ?>:</td>
<td><?php
if ( @fopen( WC()->plugin_path() . '/logs/paypal.txt', 'a' ) )
echo '<mark class="yes">' . __( 'Log directory is writable.', 'woocommerce' ) . '</mark>';
else
echo '<mark class="error">' . __( 'Log directory (<code>woocommerce/logs/</code>) is not writable. Logging will not be possible.', 'woocommerce' ) . '</mark>';
if ( @fopen( WC_LOG_DIR . 'test-log.log', 'a' ) ) {
printf( '<mark class="yes">' . __( 'Log directory (%s) is writable.', 'woocommerce' ) . '</mark>', WC_LOG_DIR );
} else {
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>
</tr>
<tr>

View File

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

View File

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

View File

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

View File

@ -34,8 +34,9 @@ class WC_Logger {
* @return void
*/
public function __destruct() {
foreach ( $this->_handles as $handle )
foreach ( $this->_handles as $handle ) {
@fclose( escapeshellarg( $handle ) );
}
}
@ -47,12 +48,13 @@ class WC_Logger {
* @return bool success
*/
private function open( $handle ) {
if ( isset( $this->_handles[ $handle ] ) )
if ( isset( $this->_handles[ $handle ] ) ) {
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 false;
}
@ -68,7 +70,7 @@ class WC_Logger {
*/
public function add( $handle, $message ) {
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" );
}
}
@ -82,9 +84,9 @@ class WC_Logger {
* @return void
*/
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 );
}
}

View File

@ -234,7 +234,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
'type' => 'checkbox',
'label' => __( 'Enable logging', 'woocommerce' ),
'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 user ID to shipping packages.
* 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 - Simplified attribute name sanitisation which maintains UTF8 char integrity.
* Refactor - Country class return methods.

View File

@ -234,19 +234,24 @@ final class WooCommerce {
if ( ! defined( 'WC_TEMPLATE_PATH' ) ) {
define( 'WC_TEMPLATE_PATH', $this->template_path() );
}
if ( ! defined( 'WC_ROUNDING_PRECISION' ) ) {
define( 'WC_ROUNDING_PRECISION', 4 );
}
// 1 = PHP_ROUND_HALF_UP, 2 = PHP_ROUND_HALF_DOWN
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 );
}
if ( ! defined( '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/' );
}
}
/**