From e922efb0f78e653164bcbde10e967465bb7c3fce Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 12 Nov 2016 19:34:10 +0100
Subject: [PATCH 001/734] Extract file log handler
---
.../class-wc-log-handler-file.php | 186 ++++++++++++++++++
1 file changed, 186 insertions(+)
create mode 100644 includes/log-handlers/class-wc-log-handler-file.php
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
new file mode 100644
index 00000000000..70bdd19ce1a
--- /dev/null
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -0,0 +1,186 @@
+_handles = array();
+ }
+
+ /**
+ * Destructor.
+ */
+ public function __destruct() {
+ foreach ( $this->_handles as $handle ) {
+ 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 {
+ * Optional. Array with additional information
+ *
+ * @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, $message, $context ) {
+
+ if ( isset($context['tag']) && $context['tag'] ) {
+ $handle = $context['tag'];
+ } else {
+ $handle = 'log';
+ }
+
+ // Bubble if add is NOT successful
+ return ! $this->add( $level, $message, $handle );
+
+ }
+
+ /**
+ * 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 ] ) ) {
+ 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 ) ) {
+ 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 ] );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Add a log entry to chosen file.
+ *
+ * @param string $handle
+ * @param string $message
+ *
+ * @return bool
+ */
+ public function add( $level, $message, $handle ) {
+ $result = false;
+
+ if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) {
+ $message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
+ $time = date_i18n( 'm-d-Y @ H:i:s -' ); // Grab Time
+ $result = fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" );
+ }
+
+ 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 ] ) ) {
+ $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;
+ }
+}
From d9dcabf8f241d3dd0944d451fe7436932f2cfd39 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 12 Nov 2016 19:38:25 +0100
Subject: [PATCH 002/734] WC_Logger registers and delegates logging to handlers
---
includes/class-wc-logger.php | 221 ++++++++++++++++++-----------------
1 file changed, 117 insertions(+), 104 deletions(-)
diff --git a/includes/class-wc-logger.php b/includes/class-wc-logger.php
index bd517721495..db134605863 100644
--- a/includes/class-wc-logger.php
+++ b/includes/class-wc-logger.php
@@ -5,10 +5,10 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
- * Allows log files to be written to for debugging purposes
+ * Provides logging capabilities for debugging purposes.
*
* @class WC_Logger
- * @version 1.6.4
+ * @version 2.0.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
@@ -16,78 +16,39 @@ if ( ! defined( 'ABSPATH' ) ) {
class WC_Logger {
/**
- * Stores open file _handles.
+ * Log Levels
+ *
+ * @see @link {https://tools.ietf.org/html/rfc5424}
+ */
+ const EMERGENCY = 'emergency';
+ const ALERT = 'alert';
+ const CRITICAL = 'critical';
+ const ERROR = 'error';
+ const WARNING = 'warning';
+ const NOTICE = 'notice';
+ const INFO = 'info';
+ const DEBUG = 'debug';
+
+ /**
+ * Stores registered log handlers.
*
* @var array
* @access private
*/
- private $_handles;
+ private $_handlers;
/**
* Constructor for the logger.
*/
public function __construct() {
- $this->_handles = array();
+ $handlers = apply_filters( 'woocommerce_register_log_handlers', array() );
+ $this->_handlers = $handlers;
}
/**
- * Destructor.
- */
- public function __destruct() {
- foreach ( $this->_handles as $handle ) {
- if ( is_resource( $handle ) ) {
- fclose( $handle );
- }
- }
- }
-
- /**
- * Open log file for writing.
+ * Add a log entry.
*
- * @param string $handle
- * @param string $mode
- * @return bool success
- */
- protected function open( $handle, $mode = 'a' ) {
- if ( isset( $this->_handles[ $handle ] ) ) {
- 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 ) ) {
- 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 ] );
- }
-
- return $result;
- }
-
- /**
- * Add a log entry to chosen file.
+ * @deprecated since 2.0.0
*
* @param string $handle
* @param string $message
@@ -95,67 +56,119 @@ class WC_Logger {
* @return bool
*/
public function add( $handle, $message ) {
- $result = false;
-
- if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) {
- $message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
- $time = date_i18n( 'm-d-Y @ H:i:s -' ); // Grab Time
- $result = fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" );
- }
-
- do_action( 'woocommerce_log_add', $handle, $message );
-
- return false !== $result;
+ _deprecated_function( 'WC_Logger::add', '2.8', 'WC_Logger::log' );
+ $this->log( self::INFO, $message, array( 'tag' => $handle ) );
+ wc_do_deprecated_action( 'woocommerce_log_add', $handle, $message, '2.8');
+ return true;
}
/**
- * Clear entries from chosen file.
+ * Add a log entry.
*
- * @param string $handle
+ * @param string $level emergency|alert|critical|error|warning|notice|info|debug
+ * @param string $message
+ * @param array $context {
+ * Optional. Additional information for log handlers.
*
- * @return bool
+ * @type string $tag Optional. May be used by log handlers to sort messages.
+ * }
*/
- public function clear( $handle ) {
- $result = false;
+ public function log( $level, $message, $context=array() ) {
- // Close the file if it's already open.
- $this->close( $handle );
+ foreach ( $this->_handlers as $handler ) {
+ $continue = $handler->handle( $level, $message, $context );
- /**
- * $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 ] ) ) {
- $result = true;
+ if ( false === $continue ) {
+ break;
+ }
}
- do_action( 'woocommerce_log_clear', $handle );
-
- return $result;
}
/**
- * Remove/delete the chosen file.
+ * Adds an emergency level message.
*
- * @param string $handle
+ * @see WC_Logger::log
*
- * @return bool
*/
- public function remove( $handle ) {
- $removed = false;
- $file = wc_get_log_file_path( $handle );
+ public function emergency( $message, $context=array() ) {
+ $this->log( self::EMERGENCY, $message, $context );
+ }
- 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' );
- }
+ /**
+ * Adds an alert level message.
+ *
+ * @see WC_Logger::log
+ *
+ */
+ public function alert( $message, $context=array() ) {
+ $this->log( self::ALERT, $message, $context );
+ }
- do_action( 'woocommerce_log_remove', $handle, $removed );
+ /**
+ * Adds a critical level message.
+ *
+ * @see WC_Logger::log
+ *
+ */
+ public function critical( $message, $context=array() ) {
+ $this->log( self::CRITICAL, $message, $context );
+ }
- return $removed;
+ /**
+ * Adds an error level message.
+ *
+ * @see WC_Logger::log
+ *
+ */
+ public function error( $message, $context=array() ) {
+ $this->log( self::ERROR, $message, $context );
+ }
+
+ /**
+ * Adds a warning level message.
+ *
+ * @see WC_Logger::log
+ *
+ */
+ public function warning( $message, $context=array() ) {
+ $this->log( self::WARNING, $message, $context );
+ }
+
+ /**
+ * Adds a notice level message.
+ *
+ * @see WC_Logger::log
+ *
+ */
+ public function notice( $message, $context=array() ) {
+ $this->log( self::NOTICE, $message, $context );
+ }
+
+ /**
+ * Adds a info level message.
+ *
+ * @see WC_Logger::log
+ *
+ */
+ public function info( $message, $context=array() ) {
+ $this->log( self::INFO, $message, $context );
+ }
+
+ /**
+ * Adds a debug level message.
+ *
+ * @see WC_Logger::log
+ *
+ */
+ public function debug( $message, $context=array() ) {
+ $this->log( self::DEBUG, $message, $context );
+ }
+
+ /**
+ * @deprecated since 2.0.0
+ */
+ public function clear() {
+ _deprecated_function( 'WC_Logger::clear', '2.8' );
}
}
From a2cce1471144df4d95f78848cb665aed3ae7b9bf Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 12 Nov 2016 19:42:32 +0100
Subject: [PATCH 003/734] Register file log handler by default
Add wc_register_file_log_handler function.
Calls add_filter with register function to include handler by default
---
includes/wc-core-functions.php | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php
index 43f0d6fbfeb..8969fee070a 100644
--- a/includes/wc-core-functions.php
+++ b/includes/wc-core-functions.php
@@ -1423,6 +1423,24 @@ function wc_get_logger() {
return new $class;
}
+/**
+ * Registers the included file log handler.
+ *
+ * @since 2.8
+ * @param array $handlers
+ * @return array
+ */
+function wc_register_file_log_handler( $handlers ) {
+ if ( ! class_exists( 'WC_Log_Handler_File' ) ) {
+ include_once( dirname( __FILE__ ) . '/log-handlers/class-wc-log-handler-file.php' );
+ }
+
+ array_push( $handlers, new WC_Log_Handler_File() );
+
+ return $handlers;
+}
+add_filter( 'woocommerce_register_log_handlers', 'wc_register_file_log_handler', 0 );
+
/**
* Store user agents. Used for tracker.
* @since 2.7.0
From 7e79746302f60d778ae08ab04d242558e28c3bcd Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 12 Nov 2016 19:42:54 +0100
Subject: [PATCH 004/734] Update tests for WC_Logger
---
tests/unit-tests/util/log.php | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/tests/unit-tests/util/log.php b/tests/unit-tests/util/log.php
index 3e77ccf83b4..5df601e035a 100644
--- a/tests/unit-tests/util/log.php
+++ b/tests/unit-tests/util/log.php
@@ -14,6 +14,8 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
* Test add().
*
* @since 2.4
+ *
+ * @expectedDeprecated WC_Logger::add
*/
public function test_add() {
$log = wc_get_logger();
@@ -28,13 +30,12 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
* Test clear().
*
* @since 2.4
+ *
+ * @expectedDeprecated WC_Logger::clear
*/
public function test_clear() {
$log = wc_get_logger();
- $log->add( 'unit-tests', 'this is a message' );
- $log->clear( 'unit-tests' );
-
- $this->assertEquals( '', $this->read_content( 'unit-tests' ) );
+ $log->clear();
}
}
From 9a0efcfe5524ee87ea6d59c55f55f60699ae2897 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 12 Nov 2016 20:46:58 +0100
Subject: [PATCH 005/734] Add abstract WC_Log_Handler class
Abstract base class is implemented with default formatting implementations.
`$timestamp` has been added to the `WC_Log_Handler::handle` implementation. This ensures all handlers are passed the same timestamp for the same message.
`_legacy` context has been added to `WC_Log_Handler_File` to ensure handle deprecated `WC_Logger::add` provides the same log entries as before.
Fix CS problems.
---
.../abstracts/abstract-wc-log-handler.php | 54 +++++++++++++++++++
includes/class-wc-logger.php | 26 ++++-----
.../class-wc-log-handler-file.php | 52 ++++++++++++++----
3 files changed, 110 insertions(+), 22 deletions(-)
create mode 100644 includes/abstracts/abstract-wc-log-handler.php
diff --git a/includes/abstracts/abstract-wc-log-handler.php b/includes/abstracts/abstract-wc-log-handler.php
new file mode 100644
index 00000000000..659906db4e6
--- /dev/null
+++ b/includes/abstracts/abstract-wc-log-handler.php
@@ -0,0 +1,54 @@
+format_time( $timestamp );
+ $level_string = to_uppercase( $level );
+ return "[{$time}] {$level_string}: {$message}";
+ }
+}
diff --git a/includes/class-wc-logger.php b/includes/class-wc-logger.php
index db134605863..17c26df60ea 100644
--- a/includes/class-wc-logger.php
+++ b/includes/class-wc-logger.php
@@ -57,8 +57,8 @@ class WC_Logger {
*/
public function add( $handle, $message ) {
_deprecated_function( 'WC_Logger::add', '2.8', 'WC_Logger::log' );
- $this->log( self::INFO, $message, array( 'tag' => $handle ) );
- wc_do_deprecated_action( 'woocommerce_log_add', $handle, $message, '2.8');
+ $this->log( self::INFO, $message, array( 'tag' => $handle, '_legacy' => true ) );
+ wc_do_deprecated_action( 'woocommerce_log_add', $handle, $message, '2.8' );
return true;
}
@@ -73,10 +73,12 @@ class WC_Logger {
* @type string $tag Optional. May be used by log handlers to sort messages.
* }
*/
- public function log( $level, $message, $context=array() ) {
+ public function log( $level, $message, $context = array() ) {
+
+ $timestamp = current_time( 'timestamp' );
foreach ( $this->_handlers as $handler ) {
- $continue = $handler->handle( $level, $message, $context );
+ $continue = $handler->handle( $level, $timestamp, $message, $context );
if ( false === $continue ) {
break;
@@ -91,7 +93,7 @@ class WC_Logger {
* @see WC_Logger::log
*
*/
- public function emergency( $message, $context=array() ) {
+ public function emergency( $message, $context = array() ) {
$this->log( self::EMERGENCY, $message, $context );
}
@@ -101,7 +103,7 @@ class WC_Logger {
* @see WC_Logger::log
*
*/
- public function alert( $message, $context=array() ) {
+ public function alert( $message, $context = array() ) {
$this->log( self::ALERT, $message, $context );
}
@@ -111,7 +113,7 @@ class WC_Logger {
* @see WC_Logger::log
*
*/
- public function critical( $message, $context=array() ) {
+ public function critical( $message, $context = array() ) {
$this->log( self::CRITICAL, $message, $context );
}
@@ -121,7 +123,7 @@ class WC_Logger {
* @see WC_Logger::log
*
*/
- public function error( $message, $context=array() ) {
+ public function error( $message, $context = array() ) {
$this->log( self::ERROR, $message, $context );
}
@@ -131,7 +133,7 @@ class WC_Logger {
* @see WC_Logger::log
*
*/
- public function warning( $message, $context=array() ) {
+ public function warning( $message, $context = array() ) {
$this->log( self::WARNING, $message, $context );
}
@@ -141,7 +143,7 @@ class WC_Logger {
* @see WC_Logger::log
*
*/
- public function notice( $message, $context=array() ) {
+ public function notice( $message, $context = array() ) {
$this->log( self::NOTICE, $message, $context );
}
@@ -151,7 +153,7 @@ class WC_Logger {
* @see WC_Logger::log
*
*/
- public function info( $message, $context=array() ) {
+ public function info( $message, $context = array() ) {
$this->log( self::INFO, $message, $context );
}
@@ -161,7 +163,7 @@ class WC_Logger {
* @see WC_Logger::log
*
*/
- public function debug( $message, $context=array() ) {
+ public function debug( $message, $context = array() ) {
$this->log( self::DEBUG, $message, $context );
}
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index 70bdd19ce1a..9c640af7308 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -1,9 +1,12 @@
add( $level, $message, $handle );
+ $entry = $this->format_entry( $level, $timestamp, $message, $context );
+ // Bubble if add is NOT successful
+ return ! $this->add( $entry, $handle );
+
+ }
+
+ /**
+ * Builds a log entry text from level, timestamp and message.
+ *
+ * Attempt to ensure backwords compatibility for legacy WC_Logger::add calls
+ *
+ * @param string $level emergency|alert|critical|error|warning|notice|info|debug
+ * @param int $timestamp
+ * @param string $message
+ *
+ * @return string Formatted log entry
+ */
+ public function format_entry( $level, $timestamp, $message, $context ) {
+
+ if ( isset( $context['_legacy'] ) && true === $context['_legacy'] ) {
+ if ( isset( $context['tag'] ) && true === $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 );
+ }
+
+ return $entry;
}
/**
@@ -121,13 +155,11 @@ class WC_Log_Handler_File {
*
* @return bool
*/
- public function add( $level, $message, $handle ) {
+ public function add( $entry, $handle ) {
$result = false;
if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) {
- $message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
- $time = date_i18n( 'm-d-Y @ H:i:s -' ); // Grab Time
- $result = fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" );
+ $result = fwrite( $this->_handles[ $handle ], $entry . "\n" );
}
return false !== $result;
From 0e0433195df69f2b2c8ae4b28f90bd5778cd0288 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 13 Nov 2016 13:46:06 +0100
Subject: [PATCH 006/734] Add logic for log level filtering to
`WC_Log_Handler`.
---
.../abstracts/abstract-wc-log-handler.php | 80 +++++++++++++++++++
includes/class-wc-logger.php | 28 +++++--
.../class-wc-log-handler-file.php | 12 ++-
includes/wc-core-functions.php | 5 +-
4 files changed, 113 insertions(+), 12 deletions(-)
diff --git a/includes/abstracts/abstract-wc-log-handler.php b/includes/abstracts/abstract-wc-log-handler.php
index 659906db4e6..8d2ec19b3a1 100644
--- a/includes/abstracts/abstract-wc-log-handler.php
+++ b/includes/abstracts/abstract-wc-log-handler.php
@@ -3,6 +3,10 @@ if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
+if ( ! class_exists( 'WC_Logger' ) ) {
+ include_once( dirname( dirname( __FILE__ ) ) . '/class-wc-logger.php' );
+}
+
/**
* Abstract WC Log Handler Class
*
@@ -13,6 +17,47 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
abstract class WC_Log_Handler {
+ /**
+ * Minimum log level this handler will process.
+ *
+ * @var int 0-8 minimum level severity for handling log entry.
+ * @access private
+ */
+ protected $_threshold;
+
+ /**
+ * Log levels by severity.
+ *
+ * @var array
+ * @access private
+ */
+ protected static $_log_levels = array(
+ WC_Logger::DEBUG => 0,
+ WC_Logger::INFO => 1,
+ WC_Logger::NOTICE => 2,
+ WC_Logger::WARNING => 3,
+ WC_Logger::ERROR => 4,
+ WC_Logger::CRITICAL => 6,
+ WC_Logger::ALERT => 7,
+ WC_Logger::EMERGENCY => 8,
+ );
+
+ /**
+ * Constructor for log handler.
+ *
+ * @param arr $args {
+ * @type string $threshold Optional. Sets the log severity threshold.
+ * emergency|alert|critical|error|warning|notice|info|debug
+ * }
+ */
+ public function __construct( $args = array() ) {
+ if ( isset( $args['threshold'] ) ) {
+ $this->set_threshold( $args['threshold'] );
+ } else {
+ $this->set_threshold( WC_Logger::EMERGENCY );
+ }
+ }
+
/**
* Handle a log entry.
*
@@ -27,6 +72,41 @@ abstract class WC_Log_Handler {
*/
public abstract function handle( $level, $timestamp, $message, $context );
+ /**
+ * Set handler severity threshold.
+ *
+ * @param string $level emergency|alert|critical|error|warning|notice|info|debug
+ */
+ public function set_threshold( $level ) {
+ $level = apply_filters( 'woocommerce_log_handler_set_threshold', $level, __CLASS__ );
+ $this->_threshold = $this->get_level_severity( $level );
+ }
+
+ /**
+ * Decode level string into integer.
+ *
+ * @param string $level emergency|alert|critical|error|warning|notice|info|debug
+ * @return int 0 (debug) - 8 (emergency) or -1 if level is not valid
+ */
+ public static function get_level_severity( $level ) {
+ if ( array_key_exists( $level, self::$_log_levels ) ) {
+ $severity = self::$_log_levels[ $level ];
+ } else {
+ $severity = -1;
+ }
+ return $severity;
+ }
+
+ /**
+ * Should this handler process this log?
+ *
+ * @param string $level emergency|alert|critical|error|warning|notice|info|debug
+ * @return bool true if the log should be handled.
+ */
+ public function should_handle( $level ) {
+ return $this->_threshold <= $this->get_level_severity( $level );
+ }
+
/**
* Formats a timestamp for use in log messages.
*
diff --git a/includes/class-wc-logger.php b/includes/class-wc-logger.php
index 17c26df60ea..05c9cb3380e 100644
--- a/includes/class-wc-logger.php
+++ b/includes/class-wc-logger.php
@@ -20,14 +20,14 @@ class WC_Logger {
*
* @see @link {https://tools.ietf.org/html/rfc5424}
*/
- const EMERGENCY = 'emergency';
- const ALERT = 'alert';
- const CRITICAL = 'critical';
- const ERROR = 'error';
- const WARNING = 'warning';
- const NOTICE = 'notice';
- const INFO = 'info';
const DEBUG = 'debug';
+ const INFO = 'info';
+ const NOTICE = 'notice';
+ const WARNING = 'warning';
+ const ERROR = 'error';
+ const CRITICAL = 'critical';
+ const ALERT = 'alert';
+ const EMERGENCY = 'emergency';
/**
* Stores registered log handlers.
@@ -37,6 +37,17 @@ class WC_Logger {
*/
private $_handlers;
+ private static $_valid_levels = array(
+ self::DEBUG,
+ self::INFO,
+ self::NOTICE,
+ self::WARNING,
+ self::ERROR,
+ self::CRITICAL,
+ self::ALERT,
+ self::EMERGENCY,
+ );
+
/**
* Constructor for the logger.
*/
@@ -74,6 +85,9 @@ class WC_Logger {
* }
*/
public function log( $level, $message, $context = array() ) {
+ if ( ! in_array( $level, self::$_valid_levels ) ) {
+ _doing_it_wrong( __FUNCTION__, sprintf( __( 'WC_Logger::log was called with an invalid level "%s"', 'woocommerce' ), $level ), '2.8' );
+ }
$timestamp = current_time( 'timestamp' );
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index 9c640af7308..ae4313e470c 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -28,8 +28,11 @@ class WC_Log_Handler_File extends WC_Log_Handler {
/**
* Constructor for the logger.
+ *
+ * @param $args additional args. @see WC_Log_Handler::__construct
*/
- public function __construct() {
+ public function __construct( $args ) {
+ parent::__construct( $args );
$this->_handles = array();
}
@@ -60,6 +63,10 @@ class WC_Log_Handler_File extends WC_Log_Handler {
*/
public function handle( $level, $timestamp, $message, $context ) {
+ if ( ! $this->should_handle( $level ) ) {
+ return true;
+ }
+
if ( isset( $context['tag'] ) && $context['tag'] ) {
$handle = $context['tag'];
} else {
@@ -70,13 +77,12 @@ class WC_Log_Handler_File extends WC_Log_Handler {
// Bubble if add is NOT successful
return ! $this->add( $entry, $handle );
-
}
/**
* Builds a log entry text from level, timestamp and message.
*
- * Attempt to ensure backwords compatibility for legacy WC_Logger::add calls
+ * Attempt to ensure backwards compatibility for legacy WC_Logger::add calls
*
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
* @param int $timestamp
diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php
index 8969fee070a..757d1a7a785 100644
--- a/includes/wc-core-functions.php
+++ b/includes/wc-core-functions.php
@@ -1428,14 +1428,15 @@ function wc_get_logger() {
*
* @since 2.8
* @param array $handlers
+ * @param string $threshold Optional. Threshold for log handler. Defaults to 'info'.
* @return array
*/
-function wc_register_file_log_handler( $handlers ) {
+function wc_register_file_log_handler( $handlers, $threshold = 'info' ) {
if ( ! class_exists( 'WC_Log_Handler_File' ) ) {
include_once( dirname( __FILE__ ) . '/log-handlers/class-wc-log-handler-file.php' );
}
- array_push( $handlers, new WC_Log_Handler_File() );
+ array_push( $handlers, new WC_Log_Handler_File( array( 'threshold' => $threshold ) ) );
return $handlers;
}
From 44ea69c579298658705a060985487eb262f07022 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 13 Nov 2016 18:45:45 +0100
Subject: [PATCH 007/734] Adds tests for bubbling, etc.
Add tests teardown to cleanup log files
Fix errors in log handlers
---
.../abstracts/abstract-wc-log-handler.php | 4 +-
includes/class-wc-logger.php | 4 +-
.../class-wc-log-handler-file.php | 2 +-
tests/unit-tests/util/log.php | 104 +++++++++++++++++-
tests/unit-tests/util/test_log_expected.txt | 7 ++
5 files changed, 112 insertions(+), 9 deletions(-)
create mode 100644 tests/unit-tests/util/test_log_expected.txt
diff --git a/includes/abstracts/abstract-wc-log-handler.php b/includes/abstracts/abstract-wc-log-handler.php
index 8d2ec19b3a1..cbae4f2cfdb 100644
--- a/includes/abstracts/abstract-wc-log-handler.php
+++ b/includes/abstracts/abstract-wc-log-handler.php
@@ -128,7 +128,7 @@ abstract class WC_Log_Handler {
*/
public function format_entry( $level, $timestamp, $message, $context ) {
$time_string = $this->format_time( $timestamp );
- $level_string = to_uppercase( $level );
- return "[{$time}] {$level_string}: {$message}";
+ $level_string = strtoupper( $level );
+ return "[{$time_string}] {$level_string}: {$message}";
}
}
diff --git a/includes/class-wc-logger.php b/includes/class-wc-logger.php
index 05c9cb3380e..85927f5fc6a 100644
--- a/includes/class-wc-logger.php
+++ b/includes/class-wc-logger.php
@@ -86,7 +86,9 @@ class WC_Logger {
*/
public function log( $level, $message, $context = array() ) {
if ( ! in_array( $level, self::$_valid_levels ) ) {
- _doing_it_wrong( __FUNCTION__, sprintf( __( 'WC_Logger::log was called with an invalid level "%s"', 'woocommerce' ), $level ), '2.8' );
+ $class = __CLASS__;
+ $method = __FUNCTION__;
+ _doing_it_wrong( "{$class}::{$method}", sprintf( __( 'WC_Logger::log was called with an invalid level "%s".', 'woocommerce' ), $level ), '2.8' );
}
$timestamp = current_time( 'timestamp' );
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index ae4313e470c..68976a8c335 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -99,7 +99,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
$handle = 'log';
}
$message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
- $time = date_i18n( 'm-d-Y @ H:i:s -' );
+ $time = date_i18n( 'm-d-Y @ H:i:s -' );
$entry = "{$time} {$message}";
} else {
$entry = parent::format_entry( $level, $timestamp, $message, $context );
diff --git a/tests/unit-tests/util/log.php b/tests/unit-tests/util/log.php
index 5df601e035a..19af559db3f 100644
--- a/tests/unit-tests/util/log.php
+++ b/tests/unit-tests/util/log.php
@@ -6,6 +6,21 @@
* @since 2.3
*/
class WC_Tests_Log extends WC_Unit_Test_Case {
+
+ public function tearDown() {
+ parent::tearDown();
+ $log_files = array(
+ wc_get_log_file_path( 'unit-tests' ),
+ wc_get_log_file_path( 'log' ),
+ );
+
+ foreach ( $log_files as $file ) {
+ if ( file_exists( $file ) && is_writable( $file ) ) {
+ unlink( $file );
+ }
+ }
+ }
+
public function read_content( $handle ) {
return file_get_contents( wc_get_log_file_path( $handle ) );
}
@@ -14,8 +29,6 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
* Test add().
*
* @since 2.4
- *
- * @expectedDeprecated WC_Logger::add
*/
public function test_add() {
$log = wc_get_logger();
@@ -24,18 +37,99 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
$this->assertStringMatchesFormat( '%d-%d-%d @ %d:%d:%d - %s', $this->read_content( 'unit-tests' ) );
$this->assertStringEndsWith( ' - this is a message' . PHP_EOL, $this->read_content( 'unit-tests' ) );
+ $this->setExpectedDeprecated( 'WC_Logger::add' );
}
/**
* Test clear().
*
* @since 2.4
- *
- * @expectedDeprecated WC_Logger::clear
*/
public function test_clear() {
$log = wc_get_logger();
-
$log->clear();
+ $this->setExpectedDeprecated( 'WC_Logger::clear' );
+ }
+
+ /**
+ * Test log() complains for bad levels.
+ */
+ public function test_bad_level() {
+ $log = wc_get_logger();
+ $log->log( 'this-is-an-invalid-level', '' );
+ $this->setExpectedIncorrectUsage( 'WC_Logger::log' );
+ }
+
+ /**
+ * Test log().
+ */
+ public function test_log() {
+ $log = wc_get_logger();
+ $log->log( 'debug', 'debug' );
+ $log->log( 'info', 'info' );
+ $log->log( 'notice', 'notice' );
+ $log->log( 'warning', 'warning' );
+ $log->log( 'error', 'error' );
+ $log->log( 'critical', 'critical' );
+ $log->log( 'alert', 'alert' );
+ $log->log( 'emergency', 'emergency' );
+
+ $log_content = $this->read_content( 'log' );
+ $this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
+ }
+
+ /**
+ * Test consumed logs do not bubble.
+ */
+ public function test_log_entry_is_consumed() {
+ $consumer = $this->createMock( WC_Log_Handler::class );
+ $consumer->method( 'handle' )->willReturn( false );
+
+ $error_thrower = $this->createMock( WC_Log_Handler::class );
+ $error_thrower->method( 'handle' )->will( $this->throwException( new Exception( 'Log was not consumed.' ) ) );
+
+ add_filter( 'woocommerce_register_log_handlers', function() use ( $consumer, $error_thrower ) {
+ return array( $consumer, $error_thrower );
+ } );
+
+ $log = wc_get_logger();
+ $log->log( 'emergency', '' );
+ }
+
+ /**
+ * Test unconsumed logs bubble.
+ */
+ public function test_log_entry_bubbles() {
+ $bubbler = $this->createMock( WC_Log_Handler::class );
+ $bubbler->method( 'handle' )->willReturn( true );
+
+ $should_be_reached = $this->createMock( WC_Log_Handler::class );
+ $should_be_reached->expects( $this->once() )->method( 'handle' );
+
+ add_filter( 'woocommerce_register_log_handlers', function() use ( $bubbler, $should_be_reached ) {
+ return array( $bubbler, $should_be_reached );
+ } );
+
+ $log = wc_get_logger();
+ $log->log( 'emergency', '' );
+ }
+
+ /**
+ * Test WC_Logger->[debug..emergency] methods
+ */
+ public function test_level_methods() {
+ $log = wc_get_logger();
+
+ $log->debug( 'debug' );
+ $log->info( 'info' );
+ $log->notice( 'notice' );
+ $log->warning( 'warning' );
+ $log->error( 'error' );
+ $log->critical( 'critical' );
+ $log->alert( 'alert' );
+ $log->emergency( 'emergency' );
+
+ $log_content = $this->read_content( 'log' );
+ $this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
}
}
diff --git a/tests/unit-tests/util/test_log_expected.txt b/tests/unit-tests/util/test_log_expected.txt
new file mode 100644
index 00000000000..54d3f805818
--- /dev/null
+++ b/tests/unit-tests/util/test_log_expected.txt
@@ -0,0 +1,7 @@
+[%d-%d-%dT%d:%d:%d+%d:%d] INFO: info
+[%d-%d-%dT%d:%d:%d+%d:%d] NOTICE: notice
+[%d-%d-%dT%d:%d:%d+%d:%d] WARNING: warning
+[%d-%d-%dT%d:%d:%d+%d:%d] ERROR: error
+[%d-%d-%dT%d:%d:%d+%d:%d] CRITICAL: critical
+[%d-%d-%dT%d:%d:%d+%d:%d] ALERT: alert
+[%d-%d-%dT%d:%d:%d+%d:%d] EMERGENCY: emergency
From 9dc30a72032d7ad8cfa83ad86918e8197d86c07b Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 13 Nov 2016 20:42:45 +0100
Subject: [PATCH 008/734] Remove 2nd filter parameter that would never be
called
---
includes/wc-core-functions.php | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php
index 757d1a7a785..2d6dbb78340 100644
--- a/includes/wc-core-functions.php
+++ b/includes/wc-core-functions.php
@@ -1424,19 +1424,18 @@ function wc_get_logger() {
}
/**
- * Registers the included file log handler.
+ * Registers the included file log handler with threshold 'info'.
*
* @since 2.8
* @param array $handlers
- * @param string $threshold Optional. Threshold for log handler. Defaults to 'info'.
* @return array
*/
-function wc_register_file_log_handler( $handlers, $threshold = 'info' ) {
+function wc_register_file_log_handler( $handlers ) {
if ( ! class_exists( 'WC_Log_Handler_File' ) ) {
include_once( dirname( __FILE__ ) . '/log-handlers/class-wc-log-handler-file.php' );
}
- array_push( $handlers, new WC_Log_Handler_File( array( 'threshold' => $threshold ) ) );
+ array_push( $handlers, new WC_Log_Handler_File( array( 'threshold' => 'info' ) ) );
return $handlers;
}
From c418623fed75d40e7f5b75b0ef5d1e4409ee795f Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 13 Nov 2016 21:05:41 +0100
Subject: [PATCH 009/734] Add tag context test for file log handler
---
tests/unit-tests/util/log.php | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/tests/unit-tests/util/log.php b/tests/unit-tests/util/log.php
index 19af559db3f..ceafe4a96e8 100644
--- a/tests/unit-tests/util/log.php
+++ b/tests/unit-tests/util/log.php
@@ -132,4 +132,23 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
$log_content = $this->read_content( 'log' );
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
}
+
+ /**
+ * Test 'tag' context determines log file.
+ */
+ public function test_log_file_tag() {
+ $log = wc_get_logger();
+ $context_tag_A = array( 'tag' => 'A' );
+
+ $log->info( 'info', $context_tag_A );
+ $log->notice( 'notice', $context_tag_A );
+ $log->warning( 'warning', $context_tag_A );
+ $log->error( 'error', $context_tag_A );
+ $log->critical( 'critical', $context_tag_A );
+ $log->alert( 'alert', $context_tag_A );
+ $log->emergency( 'emergency', $context_tag_A );
+
+ $log_content = $this->read_content( 'A' );
+ $this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
+ }
}
From 4262a72af62e85825e003d80bd4d209d35f14ff9 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 13 Nov 2016 22:14:11 +0100
Subject: [PATCH 010/734] Use wp_parse_args for default args
---
includes/abstracts/abstract-wc-log-handler.php | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/includes/abstracts/abstract-wc-log-handler.php b/includes/abstracts/abstract-wc-log-handler.php
index cbae4f2cfdb..08621d4d3f8 100644
--- a/includes/abstracts/abstract-wc-log-handler.php
+++ b/includes/abstracts/abstract-wc-log-handler.php
@@ -46,16 +46,17 @@ abstract class WC_Log_Handler {
* Constructor for log handler.
*
* @param arr $args {
- * @type string $threshold Optional. Sets the log severity threshold.
+ * @type string $threshold Optional. Default 'emergency'. Sets the log severity threshold.
* emergency|alert|critical|error|warning|notice|info|debug
* }
*/
public function __construct( $args = array() ) {
- if ( isset( $args['threshold'] ) ) {
- $this->set_threshold( $args['threshold'] );
- } else {
- $this->set_threshold( WC_Logger::EMERGENCY );
- }
+
+ $args = wp_parse_args( $args, array(
+ 'threshold' => WC_Logger::EMERGENCY,
+ ) );
+
+ $this->set_threshold( $args['threshold'] );
}
/**
From 3ef2ae611ddb31908f6fc59fd436ce8e51394041 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 13 Nov 2016 22:14:35 +0100
Subject: [PATCH 011/734] Cleanup "A" test log file
---
tests/unit-tests/util/log.php | 1 +
1 file changed, 1 insertion(+)
diff --git a/tests/unit-tests/util/log.php b/tests/unit-tests/util/log.php
index ceafe4a96e8..d77e75ebcad 100644
--- a/tests/unit-tests/util/log.php
+++ b/tests/unit-tests/util/log.php
@@ -12,6 +12,7 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
$log_files = array(
wc_get_log_file_path( 'unit-tests' ),
wc_get_log_file_path( 'log' ),
+ wc_get_log_file_path( 'A' ),
);
foreach ( $log_files as $file ) {
From 864e65de108ef823c0b29e13f48338dfbc0296b8 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 13 Nov 2016 22:15:40 +0100
Subject: [PATCH 012/734] Remove redundant __constructor
---
includes/log-handlers/class-wc-log-handler-file.php | 13 +------------
1 file changed, 1 insertion(+), 12 deletions(-)
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index 68976a8c335..f181ba3e58b 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -24,17 +24,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
* @var array
* @access private
*/
- private $_handles;
-
- /**
- * Constructor for the logger.
- *
- * @param $args additional args. @see WC_Log_Handler::__construct
- */
- public function __construct( $args ) {
- parent::__construct( $args );
- $this->_handles = array();
- }
+ private $_handles = array();
/**
* Destructor.
@@ -47,7 +37,6 @@ class WC_Log_Handler_File extends WC_Log_Handler {
}
}
-
/**
* Handle a log entry.
*
From 479ef85cd8161be52df92c97ebf14846ba69040f Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 13 Nov 2016 22:15:55 +0100
Subject: [PATCH 013/734] Add email log handler
---
.../class-wc-log-handler-email.php | 96 +++++++++++++++++++
1 file changed, 96 insertions(+)
create mode 100644 includes/log-handlers/class-wc-log-handler-email.php
diff --git a/includes/log-handlers/class-wc-log-handler-email.php b/includes/log-handlers/class-wc-log-handler-email.php
new file mode 100644
index 00000000000..7a4320ad4bc
--- /dev/null
+++ b/includes/log-handlers/class-wc-log-handler-email.php
@@ -0,0 +1,96 @@
+ 'critical',
+ 'to' => get_option( 'admin_email' ),
+ ) );
+
+ parent::__construct( $args );
+
+ if ( is_array( $args['to'] ) ) {
+ foreach ( $args['to'] as $to ) {
+ $this->add_email( $to );
+ }
+ } else {
+ $this->add_email( $args['to'] );
+ }
+
+ }
+
+ /**
+ * Handle a log entry.
+ *
+ * @param string $level emergency|alert|critical|error|warning|notice|info|debug
+ * @param string $message
+ * @param array $context {
+ * Optional. Array with additional information
+ *
+ * @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 ) {
+
+ if ( $this->should_handle( $level ) ) {
+ $subject = $this->get_subject( $level, $timestamp, $message, $context );
+ $body = $this->get_body( $level, $timestamp, $message, $context );
+ wp_mail( $this->_to, $subject, $body );
+ }
+
+ return true;
+ }
+
+ public function get_subject( $level, $timestamp, $message, $context ) {
+ return 'subject';
+ }
+
+ public function get_body( $level, $timestamp, $message, $context ) {
+ $entry = $this->format_entry( $level, $timestamp, $message, $context );
+ return $entry;
+ }
+
+ /**
+ * Adds an email to the list of recipients.
+ */
+ public function add_email( $email ) {
+ array_push( $this->_to, $email );
+ }
+}
From 55963b09a82baa4f6a80bb24a341de3bfe9fbe58 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 13 Nov 2016 22:35:51 +0100
Subject: [PATCH 014/734] Extract anonymous functions to improve test
compatibility.
---
tests/unit-tests/util/log.php | 40 +++++++++++++++++++----------------
1 file changed, 22 insertions(+), 18 deletions(-)
diff --git a/tests/unit-tests/util/log.php b/tests/unit-tests/util/log.php
index d77e75ebcad..63b9c2e049c 100644
--- a/tests/unit-tests/util/log.php
+++ b/tests/unit-tests/util/log.php
@@ -83,15 +83,7 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
* Test consumed logs do not bubble.
*/
public function test_log_entry_is_consumed() {
- $consumer = $this->createMock( WC_Log_Handler::class );
- $consumer->method( 'handle' )->willReturn( false );
-
- $error_thrower = $this->createMock( WC_Log_Handler::class );
- $error_thrower->method( 'handle' )->will( $this->throwException( new Exception( 'Log was not consumed.' ) ) );
-
- add_filter( 'woocommerce_register_log_handlers', function() use ( $consumer, $error_thrower ) {
- return array( $consumer, $error_thrower );
- } );
+ add_filter( 'woocommerce_register_log_handlers', array( $this, '_return_consume_error_handlers' ) );
$log = wc_get_logger();
$log->log( 'emergency', '' );
@@ -101,15 +93,7 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
* Test unconsumed logs bubble.
*/
public function test_log_entry_bubbles() {
- $bubbler = $this->createMock( WC_Log_Handler::class );
- $bubbler->method( 'handle' )->willReturn( true );
-
- $should_be_reached = $this->createMock( WC_Log_Handler::class );
- $should_be_reached->expects( $this->once() )->method( 'handle' );
-
- add_filter( 'woocommerce_register_log_handlers', function() use ( $bubbler, $should_be_reached ) {
- return array( $bubbler, $should_be_reached );
- } );
+ add_filter( 'woocommerce_register_log_handlers', array( $this, '_return_bubble_required_handlers' ) );
$log = wc_get_logger();
$log->log( 'emergency', '' );
@@ -152,4 +136,24 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
$log_content = $this->read_content( 'A' );
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
}
+
+ public function _return_bubble_required_handlers() {
+ $bubble = $this->createMock( WC_Log_Handler::class );
+ $bubble->method( 'handle' )->willReturn( true );
+
+ $required = $this->createMock( WC_Log_Handler::class );
+ $required->expects( $this->once() )->method( 'handle' );
+
+ return array( $bubble, $required );
+ }
+
+ public function _return_consume_error_handlers() {
+ $consume = $this->createMock( WC_Log_Handler::class );
+ $consume->method( 'handle' )->willReturn( false );
+
+ $error = $this->createMock( WC_Log_Handler::class );
+ $error->method( 'handle' )->will( $this->throwException( new Exception( 'Log was not consumed.' ) ) );
+
+ return array( $consume, $error );
+ }
}
From 36873e5097267a4c0d9b9b4f6ebc92c29dab6908 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 13 Nov 2016 23:22:45 +0100
Subject: [PATCH 015/734] Provide basic messages for email log handler
---
.../class-wc-log-handler-email.php | 32 ++++++++++++++++---
1 file changed, 27 insertions(+), 5 deletions(-)
diff --git a/includes/log-handlers/class-wc-log-handler-email.php b/includes/log-handlers/class-wc-log-handler-email.php
index 7a4320ad4bc..5892ebb620f 100644
--- a/includes/log-handlers/class-wc-log-handler-email.php
+++ b/includes/log-handlers/class-wc-log-handler-email.php
@@ -35,7 +35,7 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
* @type string|arr $to Optional. Email or emails to recieve log messages. Default site admin.
* }
*/
- public function __construct( $args ) {
+ public function __construct( $args = array() ) {
$args = wp_parse_args( $args, array(
'threshold' => 'critical',
@@ -61,8 +61,6 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
* @param string $message
* @param array $context {
* Optional. Array with additional information
- *
- * @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.
@@ -78,17 +76,41 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
return true;
}
+ /**
+ * Build subject for log email.
+ *
+ * @param string $level emergency|alert|critical|error|warning|notice|info|debug
+ * @param string $message
+ * @param array $context {
+ * Optional. Array with additional information
+ * }
+ *
+ * @return string subject
+ */
public function get_subject( $level, $timestamp, $message, $context ) {
- return 'subject';
+ return sprintf( __( '[%s] WooCommerce log message', 'woocommerce' ), $level );
}
+ /**
+ * Build body for log email.
+ *
+ * @param string $level emergency|alert|critical|error|warning|notice|info|debug
+ * @param string $message
+ * @param array $context {
+ * Optional. Array with additional information
+ * }
+ *
+ * @return string body
+ */
public function get_body( $level, $timestamp, $message, $context ) {
$entry = $this->format_entry( $level, $timestamp, $message, $context );
- return $entry;
+ return sprintf( __( "You have recieved the following WooCommerce log message: \n\n%s", 'woocommerce' ), $entry );
}
/**
* Adds an email to the list of recipients.
+ *
+ * @param string email Email address to add
*/
public function add_email( $email ) {
array_push( $this->_to, $email );
From fda6ab57bc6d0e030585d683d3615710d0c2330e Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 13 Nov 2016 23:28:38 +0100
Subject: [PATCH 016/734] Fix tests for compatibility with PHP5
---
tests/unit-tests/util/log.php | 24 ++++++++++++------------
1 file changed, 12 insertions(+), 12 deletions(-)
diff --git a/tests/unit-tests/util/log.php b/tests/unit-tests/util/log.php
index 63b9c2e049c..ad267d07b83 100644
--- a/tests/unit-tests/util/log.php
+++ b/tests/unit-tests/util/log.php
@@ -123,35 +123,35 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
*/
public function test_log_file_tag() {
$log = wc_get_logger();
- $context_tag_A = array( 'tag' => 'A' );
+ $context_tag = array( 'tag' => 'A' );
- $log->info( 'info', $context_tag_A );
- $log->notice( 'notice', $context_tag_A );
- $log->warning( 'warning', $context_tag_A );
- $log->error( 'error', $context_tag_A );
- $log->critical( 'critical', $context_tag_A );
- $log->alert( 'alert', $context_tag_A );
- $log->emergency( 'emergency', $context_tag_A );
+ $log->info( 'info', $context_tag );
+ $log->notice( 'notice', $context_tag );
+ $log->warning( 'warning', $context_tag );
+ $log->error( 'error', $context_tag );
+ $log->critical( 'critical', $context_tag );
+ $log->alert( 'alert', $context_tag );
+ $log->emergency( 'emergency', $context_tag );
$log_content = $this->read_content( 'A' );
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
}
public function _return_bubble_required_handlers() {
- $bubble = $this->createMock( WC_Log_Handler::class );
+ $bubble = $this->createMock( 'WC_Log_Handler' );
$bubble->method( 'handle' )->willReturn( true );
- $required = $this->createMock( WC_Log_Handler::class );
+ $required = $this->createMock( 'WC_Log_Handler' );
$required->expects( $this->once() )->method( 'handle' );
return array( $bubble, $required );
}
public function _return_consume_error_handlers() {
- $consume = $this->createMock( WC_Log_Handler::class );
+ $consume = $this->createMock( 'WC_Log_Handler' );
$consume->method( 'handle' )->willReturn( false );
- $error = $this->createMock( WC_Log_Handler::class );
+ $error = $this->createMock( 'WC_Log_Handler' );
$error->method( 'handle' )->will( $this->throwException( new Exception( 'Log was not consumed.' ) ) );
return array( $consume, $error );
From c5c0563f7cb3ebd2e7f0d15b9c98e22d694997f0 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 13 Nov 2016 23:56:11 +0100
Subject: [PATCH 017/734] Restore deprecated functions WC_Logger::clear &
WC_Logger::remove
Also updates clear test to supply required argument.
---
includes/class-wc-logger.php | 26 ++++++++++++++++++++++++--
tests/unit-tests/util/log.php | 2 +-
2 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/includes/class-wc-logger.php b/includes/class-wc-logger.php
index 85927f5fc6a..6be56d0dd7c 100644
--- a/includes/class-wc-logger.php
+++ b/includes/class-wc-logger.php
@@ -68,8 +68,9 @@ class WC_Logger {
*/
public function add( $handle, $message ) {
_deprecated_function( 'WC_Logger::add', '2.8', 'WC_Logger::log' );
+ $message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
$this->log( self::INFO, $message, array( 'tag' => $handle, '_legacy' => true ) );
- wc_do_deprecated_action( 'woocommerce_log_add', $handle, $message, '2.8' );
+ wc_do_deprecated_action( 'woocommerce_log_add', array( $handle, $message ), '2.8', 'This action has been deprecated with no alternative.' );
return true;
}
@@ -184,9 +185,30 @@ class WC_Logger {
}
/**
+ * Clear entries from chosen file.
+ *
* @deprecated since 2.0.0
+ *
+ * @param string $handle
+ *
+ * @return bool
*/
- public function clear() {
+ public function clear( $handle ) {
_deprecated_function( 'WC_Logger::clear', '2.8' );
+ return false;
+ }
+
+ /**
+ * Remove/delete the chosen file.
+ *
+ * @deprecated since 2.0.0
+ *
+ * @param string $handle
+ *
+ * @return bool
+ */
+ public function remove( $handle ) {
+ _deprecated_function( 'WC_Logger::remove', '2.8' );
+ return false;
}
}
diff --git a/tests/unit-tests/util/log.php b/tests/unit-tests/util/log.php
index ad267d07b83..a8a6c1f6579 100644
--- a/tests/unit-tests/util/log.php
+++ b/tests/unit-tests/util/log.php
@@ -48,7 +48,7 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
*/
public function test_clear() {
$log = wc_get_logger();
- $log->clear();
+ $log->clear( 'log' );
$this->setExpectedDeprecated( 'WC_Logger::clear' );
}
From 83aad2ba2e74b6fbf686fa81baad8f2ec9a4ace6 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Mon, 14 Nov 2016 00:00:25 +0100
Subject: [PATCH 018/734] Use older test mock syntax for legacy compatibility.
---
tests/unit-tests/util/log.php | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/tests/unit-tests/util/log.php b/tests/unit-tests/util/log.php
index a8a6c1f6579..e683cfbb6d8 100644
--- a/tests/unit-tests/util/log.php
+++ b/tests/unit-tests/util/log.php
@@ -138,20 +138,20 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
}
public function _return_bubble_required_handlers() {
- $bubble = $this->createMock( 'WC_Log_Handler' );
+ $bubble = $this->getMockBuilder( 'WC_Log_Handler' )->getMock();
$bubble->method( 'handle' )->willReturn( true );
- $required = $this->createMock( 'WC_Log_Handler' );
+ $required = $this->getMockBuilder( 'WC_Log_Handler' )->getMock();
$required->expects( $this->once() )->method( 'handle' );
return array( $bubble, $required );
}
public function _return_consume_error_handlers() {
- $consume = $this->createMock( 'WC_Log_Handler' );
+ $consume = $this->getMockBuilder( 'WC_Log_Handler' )->getMock();
$consume->method( 'handle' )->willReturn( false );
- $error = $this->createMock( 'WC_Log_Handler' );
+ $error = $this->getMockBuilder( 'WC_Log_Handler' )->getMock();
$error->method( 'handle' )->will( $this->throwException( new Exception( 'Log was not consumed.' ) ) );
return array( $consume, $error );
From d5d1ab16ea61579e579f32163c522a3cf35f1e7a Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Mon, 14 Nov 2016 21:40:54 +0100
Subject: [PATCH 019/734] Fix docblocks
---
includes/abstracts/abstract-wc-log-handler.php | 2 +-
includes/log-handlers/class-wc-log-handler-file.php | 7 +++++--
2 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/includes/abstracts/abstract-wc-log-handler.php b/includes/abstracts/abstract-wc-log-handler.php
index 08621d4d3f8..c775e36af04 100644
--- a/includes/abstracts/abstract-wc-log-handler.php
+++ b/includes/abstracts/abstract-wc-log-handler.php
@@ -45,7 +45,7 @@ abstract class WC_Log_Handler {
/**
* Constructor for log handler.
*
- * @param arr $args {
+ * @param array $args {
* @type string $threshold Optional. Default 'emergency'. Sets the log severity threshold.
* emergency|alert|critical|error|warning|notice|info|debug
* }
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index f181ba3e58b..f789c343b3c 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -74,8 +74,11 @@ class WC_Log_Handler_File extends WC_Log_Handler {
* Attempt to ensure backwards compatibility for legacy WC_Logger::add calls
*
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
- * @param int $timestamp
- * @param string $message
+ * @param int $timestamp log entry timestamp
+ * @param string $message provided log message
+ * @param array $context {
+ * Optional. Array with additional information
+ * }
*
* @return string Formatted log entry
*/
From b5cebcab6c60f565dce2399b35ef8768fc226a5e Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Mon, 14 Nov 2016 21:55:21 +0100
Subject: [PATCH 020/734] Fix scrutinizer issues
---
.../abstracts/abstract-wc-log-handler.php | 2 +-
includes/class-wc-logger.php | 8 +---
.../class-wc-log-handler-file.php | 38 ++++++++++++-------
3 files changed, 28 insertions(+), 20 deletions(-)
diff --git a/includes/abstracts/abstract-wc-log-handler.php b/includes/abstracts/abstract-wc-log-handler.php
index c775e36af04..47dc20d6c81 100644
--- a/includes/abstracts/abstract-wc-log-handler.php
+++ b/includes/abstracts/abstract-wc-log-handler.php
@@ -71,7 +71,7 @@ abstract class WC_Log_Handler {
*
* @return bool log entry should bubble to further loggers.
*/
- public abstract function handle( $level, $timestamp, $message, $context );
+ abstract public function handle( $level, $timestamp, $message, $context );
/**
* Set handler severity threshold.
diff --git a/includes/class-wc-logger.php b/includes/class-wc-logger.php
index 6be56d0dd7c..493ef28bc3b 100644
--- a/includes/class-wc-logger.php
+++ b/includes/class-wc-logger.php
@@ -189,11 +189,9 @@ class WC_Logger {
*
* @deprecated since 2.0.0
*
- * @param string $handle
- *
* @return bool
*/
- public function clear( $handle ) {
+ public function clear() {
_deprecated_function( 'WC_Logger::clear', '2.8' );
return false;
}
@@ -203,11 +201,9 @@ class WC_Logger {
*
* @deprecated since 2.0.0
*
- * @param string $handle
- *
* @return bool
*/
- public function remove( $handle ) {
+ public function remove() {
_deprecated_function( 'WC_Logger::remove', '2.8' );
return false;
}
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index f789c343b3c..0390d131ecf 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -56,11 +56,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
return true;
}
- if ( isset( $context['tag'] ) && $context['tag'] ) {
- $handle = $context['tag'];
- } else {
- $handle = 'log';
- }
+ $handle = $this->get_handle( $context );
$entry = $this->format_entry( $level, $timestamp, $message, $context );
@@ -85,11 +81,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
public function format_entry( $level, $timestamp, $message, $context ) {
if ( isset( $context['_legacy'] ) && true === $context['_legacy'] ) {
- if ( isset( $context['tag'] ) && true === $context['tag'] ) {
- $handle = $context['tag'];
- } else {
- $handle = 'log';
- }
+ $handle = $this->get_handle( $context );
$message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
$time = date_i18n( 'm-d-Y @ H:i:s -' );
$entry = "{$time} {$message}";
@@ -100,6 +92,26 @@ class WC_Log_Handler_File extends WC_Log_Handler {
return $entry;
}
+ /**
+ * Determine which handle to use for a log entry.
+ *
+ * @param array $context {
+ * Optional.
+ * @type string $tag Log message tag
+ * }
+ *
+ * @return string Handle name
+ */
+ protected function get_handle( $context ) {
+ if ( isset( $context['tag'] ) && true === $context['tag'] ) {
+ $handle = $context['tag'];
+ } else {
+ $handle = 'log';
+ }
+
+ return $handle;
+ }
+
/**
* Open log file for writing.
*
@@ -148,10 +160,10 @@ class WC_Log_Handler_File extends WC_Log_Handler {
/**
* Add a log entry to chosen file.
*
- * @param string $handle
- * @param string $message
+ * @param string $handle Log entry handle
+ * @param string $entry Log entry text
*
- * @return bool
+ * @return bool True if write was successful.
*/
public function add( $entry, $handle ) {
$result = false;
From 6ec75d8c2e28614c00a0e419489b80d38a0558f7 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Mon, 14 Nov 2016 21:56:00 +0100
Subject: [PATCH 021/734] Change defualt log handler threshold to DEBUG
following discussion with team members.
---
includes/abstracts/abstract-wc-log-handler.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/includes/abstracts/abstract-wc-log-handler.php b/includes/abstracts/abstract-wc-log-handler.php
index 47dc20d6c81..5ec449b6fa3 100644
--- a/includes/abstracts/abstract-wc-log-handler.php
+++ b/includes/abstracts/abstract-wc-log-handler.php
@@ -53,7 +53,7 @@ abstract class WC_Log_Handler {
public function __construct( $args = array() ) {
$args = wp_parse_args( $args, array(
- 'threshold' => WC_Logger::EMERGENCY,
+ 'threshold' => WC_Logger::DEBUG,
) );
$this->set_threshold( $args['threshold'] );
From b2935f35eddcfc88b8af14e85f5120a3b3919fef Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Mon, 14 Nov 2016 22:06:14 +0100
Subject: [PATCH 022/734] Fix file handle regression
---
.../class-wc-log-handler-file.php | 34 +++++++------------
1 file changed, 12 insertions(+), 22 deletions(-)
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index 0390d131ecf..965baa67fcc 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -56,7 +56,11 @@ class WC_Log_Handler_File extends WC_Log_Handler {
return true;
}
- $handle = $this->get_handle( $context );
+ if ( isset( $context['tag'] ) && $context['tag'] ) {
+ $handle = $context['tag'];
+ } else {
+ $handle = 'log';
+ }
$entry = $this->format_entry( $level, $timestamp, $message, $context );
@@ -74,6 +78,8 @@ class WC_Log_Handler_File extends WC_Log_Handler {
* @param string $message provided log message
* @param array $context {
* Optional. Array with additional information
+ *
+ * @type string $tag Optional. The tag will be used to determine which file an entry will be written to.
* }
*
* @return string Formatted log entry
@@ -81,7 +87,11 @@ class WC_Log_Handler_File extends WC_Log_Handler {
public function format_entry( $level, $timestamp, $message, $context ) {
if ( isset( $context['_legacy'] ) && true === $context['_legacy'] ) {
- $handle = $this->get_handle( $context );
+ 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}";
@@ -92,26 +102,6 @@ class WC_Log_Handler_File extends WC_Log_Handler {
return $entry;
}
- /**
- * Determine which handle to use for a log entry.
- *
- * @param array $context {
- * Optional.
- * @type string $tag Log message tag
- * }
- *
- * @return string Handle name
- */
- protected function get_handle( $context ) {
- if ( isset( $context['tag'] ) && true === $context['tag'] ) {
- $handle = $context['tag'];
- } else {
- $handle = 'log';
- }
-
- return $handle;
- }
-
/**
* Open log file for writing.
*
From 227651df777d08cc3b541f17c9ce20e98a74ed74 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Tue, 15 Nov 2016 20:45:26 +0100
Subject: [PATCH 023/734] Add `setMethods` to test mocks
This may improve php 5.2 test failures
---
tests/unit-tests/util/log.php | 46 ++++++++++++++++++++++++++++++-----
1 file changed, 40 insertions(+), 6 deletions(-)
diff --git a/tests/unit-tests/util/log.php b/tests/unit-tests/util/log.php
index e683cfbb6d8..8894cc86703 100644
--- a/tests/unit-tests/util/log.php
+++ b/tests/unit-tests/util/log.php
@@ -137,22 +137,56 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
}
+ /**
+ * Helper for log handler comsume test.
+ *
+ * Returns an array of 2 mocked log hanlders.
+ * The first handler always bubbles.
+ * The second handler expects to recieve exactly 8 messages (1 for each level).
+ *
+ * @return WC_Log_Handler[] array of mocked log handlers
+ */
public function _return_bubble_required_handlers() {
- $bubble = $this->getMockBuilder( 'WC_Log_Handler' )->getMock();
+ $bubble = $this
+ ->getMockBuilder( 'WC_Log_Handler' )
+ ->setMethods( array( 'handle' ) )
+ ->getMock();
$bubble->method( 'handle' )->willReturn( true );
- $required = $this->getMockBuilder( 'WC_Log_Handler' )->getMock();
- $required->expects( $this->once() )->method( 'handle' );
+
+ $required = $this
+ ->getMockBuilder( 'WC_Log_Handler' )
+ ->setMethods( array( 'handle' ) )
+ ->getMock();
+
+ $required->expects( $this->exactly( 8 ) )->method( 'handle' );
return array( $bubble, $required );
}
+ /**
+ * Helper for log handler comsume test.
+ *
+ * Returns an array of 2 mocked log hanlders.
+ * The first handler never bubbles.
+ * The second handler expects to never be called.
+ *
+ * @return WC_Log_Handler[] array of mocked log handlers
+ */
public function _return_consume_error_handlers() {
- $consume = $this->getMockBuilder( 'WC_Log_Handler' )->getMock();
+ $consume = $this
+ ->getMockBuilder( 'WC_Log_Handler' )
+ ->setMethods( array( 'handle' ) )
+ ->getMock();
+
$consume->method( 'handle' )->willReturn( false );
- $error = $this->getMockBuilder( 'WC_Log_Handler' )->getMock();
- $error->method( 'handle' )->will( $this->throwException( new Exception( 'Log was not consumed.' ) ) );
+ $error = $this
+ ->getMockBuilder( 'WC_Log_Handler' )
+ ->setMethods( array( 'handle' ) )
+ ->getMock();
+
+ $error->expects( $this->never() )->method( 'handle' );
return array( $consume, $error );
}
From 7ba54d8617427de314fa41df9afb36312d770e0e Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Tue, 15 Nov 2016 21:04:48 +0100
Subject: [PATCH 024/734] Add test for log() === [level_short_method]()
---
tests/unit-tests/util/log.php | 54 +++++++++++++++++++++++++++++++++--
1 file changed, 52 insertions(+), 2 deletions(-)
diff --git a/tests/unit-tests/util/log.php b/tests/unit-tests/util/log.php
index 8894cc86703..e44a74bf0da 100644
--- a/tests/unit-tests/util/log.php
+++ b/tests/unit-tests/util/log.php
@@ -79,6 +79,39 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
}
+ /**
+ * Test log( 'level', ... ) === level( ... ).
+ */
+ public function test_log_short_methods() {
+ $log = wc_get_logger();
+
+ $ctx_a = array( 'tag' => 'A' );
+ $ctx_b = array( 'tag' => 'B' );
+
+ $log->log( 'debug', 'debug', $ctx_a );
+ $log->log( 'info', 'info', $ctx_a );
+ $log->log( 'notice', 'notice', $ctx_a );
+ $log->log( 'warning', 'warning', $ctx_a );
+ $log->log( 'error', 'error', $ctx_a );
+ $log->log( 'critical', 'critical', $ctx_a );
+ $log->log( 'alert', 'alert', $ctx_a );
+ $log->log( 'emergency', 'emergency', $ctx_a );
+
+ $log->debug( 'debug', $ctx_b );
+ $log->info( 'info', $ctx_b );
+ $log->notice( 'notice', $ctx_b );
+ $log->warning( 'warning', $ctx_b );
+ $log->error( 'error', $ctx_b );
+ $log->critical( 'critical', $ctx_b );
+ $log->alert( 'alert', $ctx_b );
+ $log->emergency( 'emergency', $ctx_b );
+
+ $log_content_a = $this->read_content( 'A' );
+ $log_content_b = $this->read_content( 'B' );
+
+ $this->assertEquals( $log_content_a, $log_content_b );
+ }
+
/**
* Test consumed logs do not bubble.
*/
@@ -86,7 +119,15 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
add_filter( 'woocommerce_register_log_handlers', array( $this, '_return_consume_error_handlers' ) );
$log = wc_get_logger();
- $log->log( 'emergency', '' );
+
+ $log->debug( 'debug' );
+ $log->info( 'info' );
+ $log->notice( 'notice' );
+ $log->warning( 'warning' );
+ $log->error( 'error' );
+ $log->critical( 'critical' );
+ $log->alert( 'alert' );
+ $log->emergency( 'emergency' );
}
/**
@@ -96,7 +137,16 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
add_filter( 'woocommerce_register_log_handlers', array( $this, '_return_bubble_required_handlers' ) );
$log = wc_get_logger();
- $log->log( 'emergency', '' );
+
+ $log->debug( 'debug' );
+ $log->info( 'info' );
+ $log->notice( 'notice' );
+ $log->warning( 'warning' );
+ $log->error( 'error' );
+ $log->critical( 'critical' );
+ $log->alert( 'alert' );
+ $log->emergency( 'emergency' );
+
}
/**
From fe02d44e31c874ec517ad57b51e7bf2d50b8137c Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Tue, 15 Nov 2016 21:08:00 +0100
Subject: [PATCH 025/734] Defauly log file level is DEBUG
---
includes/wc-core-functions.php | 4 ++--
tests/unit-tests/util/log.php | 1 +
tests/unit-tests/util/test_log_expected.txt | 1 +
3 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php
index 2d6dbb78340..49f9e079650 100644
--- a/includes/wc-core-functions.php
+++ b/includes/wc-core-functions.php
@@ -1424,7 +1424,7 @@ function wc_get_logger() {
}
/**
- * Registers the included file log handler with threshold 'info'.
+ * Registers the included file log handler with threshold 'debug'.
*
* @since 2.8
* @param array $handlers
@@ -1435,7 +1435,7 @@ function wc_register_file_log_handler( $handlers ) {
include_once( dirname( __FILE__ ) . '/log-handlers/class-wc-log-handler-file.php' );
}
- array_push( $handlers, new WC_Log_Handler_File( array( 'threshold' => 'info' ) ) );
+ array_push( $handlers, new WC_Log_Handler_File( array( 'threshold' => 'debug' ) ) );
return $handlers;
}
diff --git a/tests/unit-tests/util/log.php b/tests/unit-tests/util/log.php
index e44a74bf0da..dc7944b7a24 100644
--- a/tests/unit-tests/util/log.php
+++ b/tests/unit-tests/util/log.php
@@ -175,6 +175,7 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
$log = wc_get_logger();
$context_tag = array( 'tag' => 'A' );
+ $log->debug( 'debug', $context_tag );
$log->info( 'info', $context_tag );
$log->notice( 'notice', $context_tag );
$log->warning( 'warning', $context_tag );
diff --git a/tests/unit-tests/util/test_log_expected.txt b/tests/unit-tests/util/test_log_expected.txt
index 54d3f805818..697950790d5 100644
--- a/tests/unit-tests/util/test_log_expected.txt
+++ b/tests/unit-tests/util/test_log_expected.txt
@@ -1,3 +1,4 @@
+[%d-%d-%dT%d:%d:%d+%d:%d] DEBUG: debug
[%d-%d-%dT%d:%d:%d+%d:%d] INFO: info
[%d-%d-%dT%d:%d:%d+%d:%d] NOTICE: notice
[%d-%d-%dT%d:%d:%d+%d:%d] WARNING: warning
From 8600ccc6de6846f96a31ed20fac9cf8c7d8907b0 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Tue, 15 Nov 2016 21:19:00 +0100
Subject: [PATCH 026/734] Restructure log tests to own directory
Also moves parent::tearDown to end of tearDown
Fix phpcs problems.
---
tests/unit-tests/{util => log}/log.php | 17 ++++++++---------
.../{util => log}/test_log_expected.txt | 0
2 files changed, 8 insertions(+), 9 deletions(-)
rename tests/unit-tests/{util => log}/log.php (95%)
rename tests/unit-tests/{util => log}/test_log_expected.txt (100%)
diff --git a/tests/unit-tests/util/log.php b/tests/unit-tests/log/log.php
similarity index 95%
rename from tests/unit-tests/util/log.php
rename to tests/unit-tests/log/log.php
index dc7944b7a24..f191a505696 100644
--- a/tests/unit-tests/util/log.php
+++ b/tests/unit-tests/log/log.php
@@ -8,7 +8,6 @@
class WC_Tests_Log extends WC_Unit_Test_Case {
public function tearDown() {
- parent::tearDown();
$log_files = array(
wc_get_log_file_path( 'unit-tests' ),
wc_get_log_file_path( 'log' ),
@@ -20,6 +19,7 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
unlink( $file );
}
}
+ parent::tearDown();
}
public function read_content( $handle ) {
@@ -97,13 +97,13 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
$log->log( 'alert', 'alert', $ctx_a );
$log->log( 'emergency', 'emergency', $ctx_a );
- $log->debug( 'debug', $ctx_b );
- $log->info( 'info', $ctx_b );
- $log->notice( 'notice', $ctx_b );
- $log->warning( 'warning', $ctx_b );
- $log->error( 'error', $ctx_b );
- $log->critical( 'critical', $ctx_b );
- $log->alert( 'alert', $ctx_b );
+ $log->debug( 'debug', $ctx_b );
+ $log->info( 'info', $ctx_b );
+ $log->notice( 'notice', $ctx_b );
+ $log->warning( 'warning', $ctx_b );
+ $log->error( 'error', $ctx_b );
+ $log->critical( 'critical', $ctx_b );
+ $log->alert( 'alert', $ctx_b );
$log->emergency( 'emergency', $ctx_b );
$log_content_a = $this->read_content( 'A' );
@@ -204,7 +204,6 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
->getMock();
$bubble->method( 'handle' )->willReturn( true );
-
$required = $this
->getMockBuilder( 'WC_Log_Handler' )
->setMethods( array( 'handle' ) )
diff --git a/tests/unit-tests/util/test_log_expected.txt b/tests/unit-tests/log/test_log_expected.txt
similarity index 100%
rename from tests/unit-tests/util/test_log_expected.txt
rename to tests/unit-tests/log/test_log_expected.txt
From e722870aa478663648039ec30adbce20a4a02b87 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Tue, 15 Nov 2016 23:25:04 +0100
Subject: [PATCH 027/734] Fix tests for PHP 5.2
---
tests/unit-tests/log/log.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tests/unit-tests/log/log.php b/tests/unit-tests/log/log.php
index f191a505696..9b2ecc3e40d 100644
--- a/tests/unit-tests/log/log.php
+++ b/tests/unit-tests/log/log.php
@@ -202,7 +202,7 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
->getMockBuilder( 'WC_Log_Handler' )
->setMethods( array( 'handle' ) )
->getMock();
- $bubble->method( 'handle' )->willReturn( true );
+ $bubble->expects( $this->any() )->method( 'handle' )->will( $this->returnValue( true ) );
$required = $this
->getMockBuilder( 'WC_Log_Handler' )
@@ -229,7 +229,7 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
->setMethods( array( 'handle' ) )
->getMock();
- $consume->method( 'handle' )->willReturn( false );
+ $consume->expects( $this->any() )->method( 'handle' )->will( $this->returnValue( false ) );
$error = $this
->getMockBuilder( 'WC_Log_Handler' )
From 722b288b79b84c5615793f11d42a8813424d3f06 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Wed, 16 Nov 2016 20:19:20 +0100
Subject: [PATCH 028/734] Remove _ from private/protected properties
---
.../abstracts/abstract-wc-log-handler.php | 12 +++++------
includes/class-wc-logger.php | 10 +++++-----
.../class-wc-log-handler-email.php | 6 +++---
.../class-wc-log-handler-file.php | 20 +++++++++----------
4 files changed, 24 insertions(+), 24 deletions(-)
diff --git a/includes/abstracts/abstract-wc-log-handler.php b/includes/abstracts/abstract-wc-log-handler.php
index 5ec449b6fa3..0c6d0377d7b 100644
--- a/includes/abstracts/abstract-wc-log-handler.php
+++ b/includes/abstracts/abstract-wc-log-handler.php
@@ -23,7 +23,7 @@ abstract class WC_Log_Handler {
* @var int 0-8 minimum level severity for handling log entry.
* @access private
*/
- protected $_threshold;
+ protected $threshold;
/**
* Log levels by severity.
@@ -31,7 +31,7 @@ abstract class WC_Log_Handler {
* @var array
* @access private
*/
- protected static $_log_levels = array(
+ protected static $log_levels = array(
WC_Logger::DEBUG => 0,
WC_Logger::INFO => 1,
WC_Logger::NOTICE => 2,
@@ -80,7 +80,7 @@ abstract class WC_Log_Handler {
*/
public function set_threshold( $level ) {
$level = apply_filters( 'woocommerce_log_handler_set_threshold', $level, __CLASS__ );
- $this->_threshold = $this->get_level_severity( $level );
+ $this->threshold = $this->get_level_severity( $level );
}
/**
@@ -90,8 +90,8 @@ abstract class WC_Log_Handler {
* @return int 0 (debug) - 8 (emergency) or -1 if level is not valid
*/
public static function get_level_severity( $level ) {
- if ( array_key_exists( $level, self::$_log_levels ) ) {
- $severity = self::$_log_levels[ $level ];
+ if ( array_key_exists( $level, self::$log_levels ) ) {
+ $severity = self::$log_levels[ $level ];
} else {
$severity = -1;
}
@@ -105,7 +105,7 @@ abstract class WC_Log_Handler {
* @return bool true if the log should be handled.
*/
public function should_handle( $level ) {
- return $this->_threshold <= $this->get_level_severity( $level );
+ return $this->threshold <= $this->get_level_severity( $level );
}
/**
diff --git a/includes/class-wc-logger.php b/includes/class-wc-logger.php
index 493ef28bc3b..26bbe9ea39f 100644
--- a/includes/class-wc-logger.php
+++ b/includes/class-wc-logger.php
@@ -35,9 +35,9 @@ class WC_Logger {
* @var array
* @access private
*/
- private $_handlers;
+ private $handlers;
- private static $_valid_levels = array(
+ private static $valid_levels = array(
self::DEBUG,
self::INFO,
self::NOTICE,
@@ -53,7 +53,7 @@ class WC_Logger {
*/
public function __construct() {
$handlers = apply_filters( 'woocommerce_register_log_handlers', array() );
- $this->_handlers = $handlers;
+ $this->handlers = $handlers;
}
/**
@@ -86,7 +86,7 @@ class WC_Logger {
* }
*/
public function log( $level, $message, $context = array() ) {
- if ( ! in_array( $level, self::$_valid_levels ) ) {
+ if ( ! in_array( $level, self::$valid_levels ) ) {
$class = __CLASS__;
$method = __FUNCTION__;
_doing_it_wrong( "{$class}::{$method}", sprintf( __( 'WC_Logger::log was called with an invalid level "%s".', 'woocommerce' ), $level ), '2.8' );
@@ -94,7 +94,7 @@ class WC_Logger {
$timestamp = current_time( 'timestamp' );
- foreach ( $this->_handlers as $handler ) {
+ foreach ( $this->handlers as $handler ) {
$continue = $handler->handle( $level, $timestamp, $message, $context );
if ( false === $continue ) {
diff --git a/includes/log-handlers/class-wc-log-handler-email.php b/includes/log-handlers/class-wc-log-handler-email.php
index 5892ebb620f..8ef34466ef7 100644
--- a/includes/log-handlers/class-wc-log-handler-email.php
+++ b/includes/log-handlers/class-wc-log-handler-email.php
@@ -24,7 +24,7 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
* @var array
* @access private
*/
- private $_to = array();
+ private $to = array();
/**
* Constructor for the logger.
@@ -70,7 +70,7 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
if ( $this->should_handle( $level ) ) {
$subject = $this->get_subject( $level, $timestamp, $message, $context );
$body = $this->get_body( $level, $timestamp, $message, $context );
- wp_mail( $this->_to, $subject, $body );
+ wp_mail( $this->to, $subject, $body );
}
return true;
@@ -113,6 +113,6 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
* @param string email Email address to add
*/
public function add_email( $email ) {
- array_push( $this->_to, $email );
+ array_push( $this->to, $email );
}
}
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index 965baa67fcc..971c10e0fcb 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -24,13 +24,13 @@ class WC_Log_Handler_File extends WC_Log_Handler {
* @var array
* @access private
*/
- private $_handles = array();
+ private $handles = array();
/**
* Destructor.
*/
public function __destruct() {
- foreach ( $this->_handles as $handle ) {
+ foreach ( $this->handles as $handle ) {
if ( is_resource( $handle ) ) {
fclose( $handle );
}
@@ -110,7 +110,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
* @return bool success
*/
protected function open( $handle, $mode = 'a' ) {
- if ( isset( $this->_handles[ $handle ] ) ) {
+ if ( isset( $this->handles[ $handle ] ) ) {
return true;
}
@@ -123,7 +123,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
}
}
- if ( $this->_handles[ $handle ] = @fopen( wc_get_log_file_path( $handle ), $mode ) ) {
+ if ( $this->handles[ $handle ] = @fopen( wc_get_log_file_path( $handle ), $mode ) ) {
return true;
}
@@ -139,9 +139,9 @@ class WC_Log_Handler_File extends WC_Log_Handler {
protected function close( $handle ) {
$result = false;
- if ( is_resource( $this->_handles[ $handle ] ) ) {
- $result = fclose( $this->_handles[ $handle ] );
- unset( $this->_handles[ $handle ] );
+ if ( is_resource( $this->handles[ $handle ] ) ) {
+ $result = fclose( $this->handles[ $handle ] );
+ unset( $this->handles[ $handle ] );
}
return $result;
@@ -158,8 +158,8 @@ class WC_Log_Handler_File extends WC_Log_Handler {
public function add( $entry, $handle ) {
$result = false;
- if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) {
- $result = fwrite( $this->_handles[ $handle ], $entry . "\n" );
+ if ( $this->open( $handle ) && is_resource( $this->handles[ $handle ] ) ) {
+ $result = fwrite( $this->handles[ $handle ], $entry . "\n" );
}
return false !== $result;
@@ -182,7 +182,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
* $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 ] ) ) {
+ if ( $this->open( $handle, 'w' ) && is_resource( $this->handles[ $handle ] ) ) {
$result = true;
}
From e4c08aa03a69b215ab06b4e854eda6dd8c44350a Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Wed, 16 Nov 2016 20:22:00 +0100
Subject: [PATCH 029/734] Remove optional from required log handler context
array
---
includes/log-handlers/class-wc-log-handler-email.php | 6 +++---
includes/log-handlers/class-wc-log-handler-file.php | 4 ++--
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/includes/log-handlers/class-wc-log-handler-email.php b/includes/log-handlers/class-wc-log-handler-email.php
index 8ef34466ef7..b7ef85cd909 100644
--- a/includes/log-handlers/class-wc-log-handler-email.php
+++ b/includes/log-handlers/class-wc-log-handler-email.php
@@ -60,7 +60,7 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
* @param string $message
* @param array $context {
- * Optional. Array with additional information
+ * Array with additional information
* }
*
* @return bool log entry should bubble to further loggers.
@@ -82,7 +82,7 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
* @param string $message
* @param array $context {
- * Optional. Array with additional information
+ * Array with additional information
* }
*
* @return string subject
@@ -97,7 +97,7 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
* @param string $message
* @param array $context {
- * Optional. Array with additional information
+ * Array with additional information
* }
*
* @return string body
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index 971c10e0fcb..4e376ce5dc1 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -43,7 +43,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
* @param string $message
* @param array $context {
- * Optional. Array with additional information
+ * Array with additional information
*
* @type string $tag Optional. The tag will be used to determine which file an entry will be written to.
* }
@@ -77,7 +77,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
* @param int $timestamp log entry timestamp
* @param string $message provided log message
* @param array $context {
- * Optional. Array with additional information
+ * Array with additional information
*
* @type string $tag Optional. The tag will be used to determine which file an entry will be written to.
* }
From f0508a0fc630363c8def13f4cdf486ee1b280f73 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 19 Nov 2016 17:33:31 +0100
Subject: [PATCH 030/734] Improve log email message format.
Suggestion via https://github.com/woocommerce/woocommerce/pull/12340#pullrequestreview-8895028
---
includes/log-handlers/class-wc-log-handler-email.php | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/includes/log-handlers/class-wc-log-handler-email.php b/includes/log-handlers/class-wc-log-handler-email.php
index b7ef85cd909..4de63fcfa1b 100644
--- a/includes/log-handlers/class-wc-log-handler-email.php
+++ b/includes/log-handlers/class-wc-log-handler-email.php
@@ -104,7 +104,8 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
*/
public function get_body( $level, $timestamp, $message, $context ) {
$entry = $this->format_entry( $level, $timestamp, $message, $context );
- return sprintf( __( "You have recieved the following WooCommerce log message: \n\n%s", 'woocommerce' ), $entry );
+ return __( 'You have recieved the following WooCommerce log message:', 'woocommerce' )
+ . PHP_EOL . PHP_EOL . $entry;
}
/**
From 611577320f1cd4b4701319b0e373ef72a1807ded Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 19 Nov 2016 17:34:27 +0100
Subject: [PATCH 031/734] Include site name in log email message
Suggestion via https://github.com/woocommerce/woocommerce/pull/12340#discussion_r88213040
---
includes/log-handlers/class-wc-log-handler-email.php | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/includes/log-handlers/class-wc-log-handler-email.php b/includes/log-handlers/class-wc-log-handler-email.php
index 4de63fcfa1b..8d4d8f729d1 100644
--- a/includes/log-handlers/class-wc-log-handler-email.php
+++ b/includes/log-handlers/class-wc-log-handler-email.php
@@ -88,7 +88,8 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
* @return string subject
*/
public function get_subject( $level, $timestamp, $message, $context ) {
- return sprintf( __( '[%s] WooCommerce log message', 'woocommerce' ), $level );
+ $site_name = get_bloginfo( 'name' );
+ return sprintf( __( '[%1$s] WooCommerce log message from %2$s', 'woocommerce' ), strtoupper($level), $site_name );
}
/**
From ad03597a3d84a223d41edcde68e13f6e7c9ee7dc Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 19 Nov 2016 17:38:05 +0100
Subject: [PATCH 032/734] Simplify default log entry format
Remove punctuation. Space separate values:
TIMESTAMP LEVEL MESSAGE
---
includes/abstracts/abstract-wc-log-handler.php | 2 +-
tests/unit-tests/log/test_log_expected.txt | 16 ++++++++--------
2 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/includes/abstracts/abstract-wc-log-handler.php b/includes/abstracts/abstract-wc-log-handler.php
index 0c6d0377d7b..adee326938a 100644
--- a/includes/abstracts/abstract-wc-log-handler.php
+++ b/includes/abstracts/abstract-wc-log-handler.php
@@ -130,6 +130,6 @@ abstract class WC_Log_Handler {
public function format_entry( $level, $timestamp, $message, $context ) {
$time_string = $this->format_time( $timestamp );
$level_string = strtoupper( $level );
- return "[{$time_string}] {$level_string}: {$message}";
+ return "{$time_string} {$level_string} {$message}";
}
}
diff --git a/tests/unit-tests/log/test_log_expected.txt b/tests/unit-tests/log/test_log_expected.txt
index 697950790d5..0f660d65d76 100644
--- a/tests/unit-tests/log/test_log_expected.txt
+++ b/tests/unit-tests/log/test_log_expected.txt
@@ -1,8 +1,8 @@
-[%d-%d-%dT%d:%d:%d+%d:%d] DEBUG: debug
-[%d-%d-%dT%d:%d:%d+%d:%d] INFO: info
-[%d-%d-%dT%d:%d:%d+%d:%d] NOTICE: notice
-[%d-%d-%dT%d:%d:%d+%d:%d] WARNING: warning
-[%d-%d-%dT%d:%d:%d+%d:%d] ERROR: error
-[%d-%d-%dT%d:%d:%d+%d:%d] CRITICAL: critical
-[%d-%d-%dT%d:%d:%d+%d:%d] ALERT: alert
-[%d-%d-%dT%d:%d:%d+%d:%d] EMERGENCY: emergency
+%d-%d-%dT%d:%d:%d+%d:%d DEBUG debug
+%d-%d-%dT%d:%d:%d+%d:%d INFO info
+%d-%d-%dT%d:%d:%d+%d:%d NOTICE notice
+%d-%d-%dT%d:%d:%d+%d:%d WARNING warning
+%d-%d-%dT%d:%d:%d+%d:%d ERROR error
+%d-%d-%dT%d:%d:%d+%d:%d CRITICAL critical
+%d-%d-%dT%d:%d:%d+%d:%d ALERT alert
+%d-%d-%dT%d:%d:%d+%d:%d EMERGENCY emergency
From b645fa06b782345a406d16f970749ad9392f061f Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 19 Nov 2016 17:47:29 +0100
Subject: [PATCH 033/734] Improve logic for legacy `->add` message formatting
Suggestion via https://github.com/woocommerce/woocommerce/pull/12340#discussion_r88318220
---
includes/log-handlers/class-wc-log-handler-file.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index 4e376ce5dc1..55b3edbdd15 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -93,8 +93,8 @@ class WC_Log_Handler_File extends WC_Log_Handler {
$handle = 'log';
}
$message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
- $time = date_i18n( 'm-d-Y @ H:i:s -' );
- $entry = "{$time} {$message}";
+ $time = date_i18n( 'm-d-Y @ H:i:s' );
+ $entry = "{$time} - {$message}";
} else {
$entry = parent::format_entry( $level, $timestamp, $message, $context );
}
From 9adb64b6b95c60eec4e23475cee5cd2cb891e115 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 19 Nov 2016 18:10:55 +0100
Subject: [PATCH 034/734] Restore log deletion from admin log viewer page
WC_Logger no longer controls logging and has no knowledge of files.
Correctly handle `remove_log` action via file log handler.
---
includes/admin/class-wc-admin-status.php | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/includes/admin/class-wc-admin-status.php b/includes/admin/class-wc-admin-status.php
index b065b871424..8f09658d374 100644
--- a/includes/admin/class-wc-admin-status.php
+++ b/includes/admin/class-wc-admin-status.php
@@ -250,8 +250,13 @@ class WC_Admin_Status {
}
if ( ! empty( $_REQUEST['handle'] ) ) {
- $logger = wc_get_logger();
- $logger->remove( $_REQUEST['handle'] );
+
+ if ( ! class_exists( 'WC_Log_Handler_File' ) ) {
+ include_once( dirname( dirname( __FILE__ ) ) . '/log-handlers/class-wc-log-handler-file.php' );
+ }
+
+ $log_handler = new WC_Log_Handler_File();
+ $log_handler->remove( $_REQUEST['handle'] );
}
wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) );
From 75c3e5e8c61382724ccc59c74d1d6bec5abb1620 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 19 Nov 2016 18:16:09 +0100
Subject: [PATCH 035/734] Fix phpcs warning
---
includes/log-handlers/class-wc-log-handler-email.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/includes/log-handlers/class-wc-log-handler-email.php b/includes/log-handlers/class-wc-log-handler-email.php
index 8d4d8f729d1..2c01f1142dc 100644
--- a/includes/log-handlers/class-wc-log-handler-email.php
+++ b/includes/log-handlers/class-wc-log-handler-email.php
@@ -89,7 +89,7 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
*/
public function get_subject( $level, $timestamp, $message, $context ) {
$site_name = get_bloginfo( 'name' );
- return sprintf( __( '[%1$s] WooCommerce log message from %2$s', 'woocommerce' ), strtoupper($level), $site_name );
+ return sprintf( __( '[%1$s] WooCommerce log message from %2$s', 'woocommerce' ), strtoupper( $level ), $site_name );
}
/**
From 2a6c95b4904fdadd91d07d3053b7308fbae87a3d Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 19 Nov 2016 19:28:12 +0100
Subject: [PATCH 036/734] Prevent possible undefined index warning
---
includes/log-handlers/class-wc-log-handler-file.php | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index 55b3edbdd15..300daf6a8ae 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -139,7 +139,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
protected function close( $handle ) {
$result = false;
- if ( is_resource( $this->handles[ $handle ] ) ) {
+ if ( array_key_exists( $handle, $this->handles ) && is_resource( $this->handles[ $handle ] ) ) {
$result = fclose( $this->handles[ $handle ] );
unset( $this->handles[ $handle ] );
}
@@ -215,4 +215,8 @@ class WC_Log_Handler_File extends WC_Log_Handler {
return $removed;
}
+
+ protected static function log_rotate( $handle ) {
+ $base_log_file = wc_get_log_file_path( $handle );
+ }
}
From 7359cca4a9f47a738031030e052579ad5247625b Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 19 Nov 2016 19:29:41 +0100
Subject: [PATCH 037/734] Add specific file log handler tests
---
tests/unit-tests/log/log-handler-file.php | 125 ++++++++++++++++++++++
tests/unit-tests/log/log.php | 21 +---
2 files changed, 126 insertions(+), 20 deletions(-)
create mode 100644 tests/unit-tests/log/log-handler-file.php
diff --git a/tests/unit-tests/log/log-handler-file.php b/tests/unit-tests/log/log-handler-file.php
new file mode 100644
index 00000000000..e2f8749831a
--- /dev/null
+++ b/tests/unit-tests/log/log-handler-file.php
@@ -0,0 +1,125 @@
+ 'debug' ) );
+
+ $handler->handle( 'info', time(), 'this is a message', array( 'tag' => 'unit-tests', '_legacy' => true ) );
+
+ $this->assertStringMatchesFormat( '%d-%d-%d @ %d:%d:%d - %s', $this->read_content( 'unit-tests' ) );
+ $this->assertStringEndsWith( ' - this is a message' . PHP_EOL, $this->read_content( 'unit-tests' ) );
+ }
+
+ /**
+ * Test clear().
+ *
+ * @since 2.8
+ */
+ public function test_clear() {
+ $handler = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
+ $log_name = '_cleartestfile';
+ $handler->handle( 'debug', time(), 'debug', array( 'tag' => $log_name ) );
+ $handler->clear( $log_name );
+ $this->assertEquals( '', $this->read_content( $log_name ) );
+ }
+
+ /**
+ * Test remove().
+ *
+ * @since 2.8
+ */
+ public function test_remove() {
+ $handler = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
+ $log_name = '_clearremovefile';
+ $handler->handle( 'debug', time(), 'debug', array( 'tag' => $log_name ) );
+ $handler->remove( $log_name );
+ $this->assertFileNotExists( wc_get_log_file_path( $log_name ) );
+ }
+
+ /**
+ * Test handle writes to default file correctly.
+ *
+ * @since 2.8
+ */
+ public function test_writes_file() {
+ $handler = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
+ $time = time();
+
+ $handler->handle( 'debug', $time, 'debug', array() );
+ $handler->handle( 'info', $time, 'info', array() );
+ $handler->handle( 'notice', $time, 'notice', array() );
+ $handler->handle( 'warning', $time, 'warning', array() );
+ $handler->handle( 'error', $time, 'error', array() );
+ $handler->handle( 'critical', $time, 'critical', array() );
+ $handler->handle( 'alert', $time, 'alert', array() );
+ $handler->handle( 'emergency', $time, 'emergency', array() );
+
+ $log_content = $this->read_content( 'log' );
+ $this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
+ }
+
+ /**
+ * Test 'tag' context determines log file.
+ *
+ * @since 2.8
+ */
+ public function test_log_file_tag() {
+ $handler = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
+ $time = time();
+ $context_tag = array( 'tag' => 'A' );
+
+ $handler->handle( 'debug', $time, 'debug', $context_tag );
+ $handler->handle( 'info', $time, 'info', $context_tag );
+ $handler->handle( 'notice', $time, 'notice', $context_tag );
+ $handler->handle( 'warning', $time, 'warning', $context_tag );
+ $handler->handle( 'error', $time, 'error', $context_tag );
+ $handler->handle( 'critical', $time, 'critical', $context_tag );
+ $handler->handle( 'alert', $time, 'alert', $context_tag );
+ $handler->handle( 'emergency', $time, 'emergency', $context_tag );
+
+ $log_content = $this->read_content( 'A' );
+ $this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
+ }
+
+}
diff --git a/tests/unit-tests/log/log.php b/tests/unit-tests/log/log.php
index 9b2ecc3e40d..769d286a671 100644
--- a/tests/unit-tests/log/log.php
+++ b/tests/unit-tests/log/log.php
@@ -12,6 +12,7 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
wc_get_log_file_path( 'unit-tests' ),
wc_get_log_file_path( 'log' ),
wc_get_log_file_path( 'A' ),
+ wc_get_log_file_path( 'B' ),
);
foreach ( $log_files as $file ) {
@@ -168,26 +169,6 @@ class WC_Tests_Log extends WC_Unit_Test_Case {
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
}
- /**
- * Test 'tag' context determines log file.
- */
- public function test_log_file_tag() {
- $log = wc_get_logger();
- $context_tag = array( 'tag' => 'A' );
-
- $log->debug( 'debug', $context_tag );
- $log->info( 'info', $context_tag );
- $log->notice( 'notice', $context_tag );
- $log->warning( 'warning', $context_tag );
- $log->error( 'error', $context_tag );
- $log->critical( 'critical', $context_tag );
- $log->alert( 'alert', $context_tag );
- $log->emergency( 'emergency', $context_tag );
-
- $log_content = $this->read_content( 'A' );
- $this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
- }
-
/**
* Helper for log handler comsume test.
*
From e7d6a9a48bbd44fb47f4668ea4826289884e62b4 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 19 Nov 2016 22:29:43 +0100
Subject: [PATCH 038/734] Rotate logs
WC_Log_Handler_File now includes log rotation.
Add and improve tests.
---
.../class-wc-log-handler-file.php | 121 +++++++++++++++++-
tests/unit-tests/log/log-handler-file.php | 53 +++++++-
2 files changed, 166 insertions(+), 8 deletions(-)
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index 300daf6a8ae..c3b9d05e817 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -26,8 +26,38 @@ class WC_Log_Handler_File extends WC_Log_Handler {
*/
private $handles = array();
+ /**
+ * File size limit for log files in bytes.
+ *
+ * @var int
+ * @access private
+ */
+ private $log_size_limit;
+
+ /**
+ * Constructor for the logger.
+ *
+ * @param $args additional args. {
+ * Optional. @see WC_Log_Handler::__construct.
+ *
+ * @type int $log_size_limit Optional. Size limit for log files. Default 5mb.
+ * }
+ */
+ public function __construct( $args = array() ) {
+
+ $args = wp_parse_args( $args, array(
+ 'log_size_limit' => 5 * 1024 * 1024,
+ ) );
+
+ parent::__construct( $args );
+
+ $this->log_size_limit = $args['log_size_limit'];
+ }
+
/**
* Destructor.
+ *
+ * Cleans up open file handles.
*/
public function __destruct() {
foreach ( $this->handles as $handle ) {
@@ -110,7 +140,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
* @return bool success
*/
protected function open( $handle, $mode = 'a' ) {
- if ( isset( $this->handles[ $handle ] ) ) {
+ if ( $this->is_open( $handle ) ) {
return true;
}
@@ -130,6 +160,16 @@ class WC_Log_Handler_File extends WC_Log_Handler {
return false;
}
+ /**
+ * Check if a handle is open.
+ *
+ * @param string $handle Log handle.
+ * @return bool True if $handle is open.
+ */
+ protected function is_open( $handle ) {
+ return array_key_exists( $handle, $this->handles );
+ }
+
/**
* Close a handle.
*
@@ -139,7 +179,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
protected function close( $handle ) {
$result = false;
- if ( array_key_exists( $handle, $this->handles ) && is_resource( $this->handles[ $handle ] ) ) {
+ if ( $this->is_open( $handle ) && is_resource( $this->handles[ $handle ] ) ) {
$result = fclose( $this->handles[ $handle ] );
unset( $this->handles[ $handle ] );
}
@@ -158,6 +198,10 @@ class WC_Log_Handler_File extends WC_Log_Handler {
public 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 . "\n" );
}
@@ -216,7 +260,76 @@ class WC_Log_Handler_File extends WC_Log_Handler {
return $removed;
}
- protected static function log_rotate( $handle ) {
- $base_log_file = wc_get_log_file_path( $handle );
+ /**
+ * Check if log file should be rotated.
+ *
+ * Compares the size of the log file to determine whether it is over the size limit.
+ *
+ * @param string $handle Log handle
+ * @return bool True if if should be rotated.
+ */
+ protected function should_rotate( $handle ) {
+ $file = wc_get_log_file_path( $handle );
+ 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;
+ }
}
+
+ /**
+ * Rotate log files.
+ *
+ * Logs are rotatated by prepending '.x' to the '.log' suffix.
+ * The current log plus 10 historical logs are maintained.
+ * For example:
+ * base.9.log -> [ REMOVED ]
+ * base.8.log -> base.9.log
+ * ...
+ * base.0.log -> base.1.log
+ * base.log -> base.0.log
+ *
+ * @param string $handle Log handle
+ */
+ protected function log_rotate( $handle ) {
+ for ( $i = 8; $i >= 0; $i-- ) {
+ $this->increment_log_infix( $handle, $i );
+ }
+ $this->increment_log_infix( $handle );
+ }
+
+ /**
+ * Increment a log file suffix.
+ *
+ * @param string $handle Log handle
+ * @param null|int $number Optional. Default null. Log suffix number to be incremented.
+ * @return bool True if increment was successful, otherwise false.
+ */
+ protected function increment_log_infix( $handle, $number = null ) {
+ if ( null === $number ) {
+ $suffix = '';
+ $next_suffix = '.0';
+ } else {
+ $suffix = '.' . $number;
+ $next_suffix = '.' . ($number + 1);
+ }
+
+ $rename_from = wc_get_log_file_path( "{$handle}{$suffix}" );
+ $rename_to = wc_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;
+ }
+
+ }
+
}
diff --git a/tests/unit-tests/log/log-handler-file.php b/tests/unit-tests/log/log-handler-file.php
index e2f8749831a..5e5f344e4d7 100644
--- a/tests/unit-tests/log/log-handler-file.php
+++ b/tests/unit-tests/log/log-handler-file.php
@@ -20,8 +20,19 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
'unit-tests',
'log',
'A',
- '_cleartestfile',
- '_clearremovefile',
+ '_test_clear',
+ '_test_remove',
+ '_test_log_rotate',
+ '_test_log_rotate.0',
+ '_test_log_rotate.1',
+ '_test_log_rotate.2',
+ '_test_log_rotate.3',
+ '_test_log_rotate.4',
+ '_test_log_rotate.5',
+ '_test_log_rotate.6',
+ '_test_log_rotate.7',
+ '_test_log_rotate.8',
+ '_test_log_rotate.9',
);
foreach ( $log_files as $file ) {
@@ -58,7 +69,7 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
*/
public function test_clear() {
$handler = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
- $log_name = '_cleartestfile';
+ $log_name = '_test_clear';
$handler->handle( 'debug', time(), 'debug', array( 'tag' => $log_name ) );
$handler->clear( $log_name );
$this->assertEquals( '', $this->read_content( $log_name ) );
@@ -71,7 +82,7 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
*/
public function test_remove() {
$handler = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
- $log_name = '_clearremovefile';
+ $log_name = '_test_remove';
$handler->handle( 'debug', time(), 'debug', array( 'tag' => $log_name ) );
$handler->remove( $log_name );
$this->assertFileNotExists( wc_get_log_file_path( $log_name ) );
@@ -122,4 +133,38 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
}
+ /**
+ * Test log_rotate()
+ *
+ * @since 2.8
+ */
+ public function test_log_rotate() {
+ $handler = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
+ $time = time();
+ $log_name = '_test_log_rotate';
+ $base_log_file = wc_get_log_file_path( $log_name );
+
+ // Create log file larger than limit (5mb) to ensure log is rotated
+ $handle = fopen( $base_log_file, 'w' );
+ fseek( $handle, 6 * 1024 * 1024 );
+ fwrite( $handle, '_base_log_file_contents_' );
+ fclose( $handle );
+
+ for ( $i = 0; $i < 10; $i++ ) {
+ file_put_contents( wc_get_log_file_path( $log_name . ".{$i}" ), $i );
+ }
+
+ $context_tag = array( 'tag' => $log_name );
+
+ $handler->handle( 'emergency', $time, 'emergency', $context_tag );
+
+ $this->assertFileExists( wc_get_log_file_path( $log_name ) );
+ $this->assertStringEndsWith( 'EMERGENCY emergency' . PHP_EOL, $this->read_content( $log_name ) );
+
+ $this->assertEquals( '_base_log_file_contents_', trim( $this->read_content( $log_name . '.0' ) ) );
+ for ( $i = 1; $i < 10; $i++ ) {
+ $this->assertEquals( $i - 1, $this->read_content( $log_name . ".{$i}" ) );
+ }
+ }
+
}
From 822981c009031ba900353495431681a9fd508443 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 20 Nov 2016 14:32:46 +0100
Subject: [PATCH 039/734] Improve handle signature
The signature of WC_Log_Handler::handle mirrors log messages which is more intuitive.
Method docblocks have also been improved and are more complete.
---
.../abstracts/abstract-wc-log-handler.php | 29 +++++++-------
includes/class-wc-install.php | 10 +++++
includes/class-wc-logger.php | 19 ++-------
.../class-wc-log-handler-email.php | 35 ++++++++--------
.../class-wc-log-handler-file.php | 39 ++++++++----------
tests/unit-tests/log/log-handler-file.php | 40 +++++++++----------
6 files changed, 81 insertions(+), 91 deletions(-)
diff --git a/includes/abstracts/abstract-wc-log-handler.php b/includes/abstracts/abstract-wc-log-handler.php
index adee326938a..e9a300202d2 100644
--- a/includes/abstracts/abstract-wc-log-handler.php
+++ b/includes/abstracts/abstract-wc-log-handler.php
@@ -62,16 +62,14 @@ abstract class WC_Log_Handler {
/**
* Handle a log entry.
*
+ * @param int $timestamp Log timestamp.
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
- * @param int $timestamp
- * @param string $message
- * @param array $context {
- * Optional. Array with additional information.
- * }
+ * @param string $message Log message.
+ * @param array $context Additional information for log handlers.
*
- * @return bool log entry should bubble to further loggers.
+ * @return bool True if log entry should bubble to further loggers.
*/
- abstract public function handle( $level, $timestamp, $message, $context );
+ abstract public function handle( $timestamp, $level, $message, $context );
/**
* Set handler severity threshold.
@@ -99,10 +97,10 @@ abstract class WC_Log_Handler {
}
/**
- * Should this handler process this log?
+ * Determine whether handler should handle log.
*
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
- * @return bool true if the log should be handled.
+ * @return bool True if the log should be handled.
*/
public function should_handle( $level ) {
return $this->threshold <= $this->get_level_severity( $level );
@@ -111,8 +109,8 @@ abstract class WC_Log_Handler {
/**
* Formats a timestamp for use in log messages.
*
- * @param int $timestamp
- * @return string formatted time
+ * @param int $timestamp Log timestamp.
+ * @return string Formatted time for use in log entry.
*/
public static function format_time( $timestamp ) {
return date( 'c', $timestamp );
@@ -121,13 +119,14 @@ abstract class WC_Log_Handler {
/**
* Builds a log entry text from level, timestamp and message.
*
+ * @param int $timestamp Log timestamp.
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
- * @param int $timestamp
- * @param string $message
+ * @param string $message Log message.
+ * @param array $context Additional information for log handlers.
*
- * @return string Formatted log entry
+ * @return string Formatted log entry.
*/
- public function format_entry( $level, $timestamp, $message, $context ) {
+ public function format_entry( $timestamp, $level, $message, $context ) {
$time_string = $this->format_time( $timestamp );
$level_string = strtoupper( $level );
return "{$time_string} {$level_string} {$message}";
diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php
index cca223c9c20..ec43c581e98 100644
--- a/includes/class-wc-install.php
+++ b/includes/class-wc-install.php
@@ -601,6 +601,16 @@ CREATE TABLE {$wpdb->prefix}woocommerce_payment_tokenmeta (
PRIMARY KEY (meta_id),
KEY payment_token_id (payment_token_id),
KEY meta_key (meta_key($max_index_length))
+) $collate;
+CREATE TABLE {$wpdb->prefix}woocommerce_log (
+ log_id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
+ timestamp datetime NOT NULL,
+ level varchar(255) NOT NULL,
+ tag varchar(255) NOT NULL,
+ message longtext NOT NULL,
+ context longtext NULL,
+ PRIMARY KEY (log_id),
+ KEY level (level)
) $collate;
";
diff --git a/includes/class-wc-logger.php b/includes/class-wc-logger.php
index 26bbe9ea39f..d7ea7409128 100644
--- a/includes/class-wc-logger.php
+++ b/includes/class-wc-logger.php
@@ -77,13 +77,10 @@ class WC_Logger {
/**
* Add a log entry.
*
+ * @param int $timestamp Log timestamp.
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
- * @param string $message
- * @param array $context {
- * Optional. Additional information for log handlers.
- *
- * @type string $tag Optional. May be used by log handlers to sort messages.
- * }
+ * @param string $message Log message.
+ * @param array $context Optional. Additional information for log handlers.
*/
public function log( $level, $message, $context = array() ) {
if ( ! in_array( $level, self::$valid_levels ) ) {
@@ -95,7 +92,7 @@ class WC_Logger {
$timestamp = current_time( 'timestamp' );
foreach ( $this->handlers as $handler ) {
- $continue = $handler->handle( $level, $timestamp, $message, $context );
+ $continue = $handler->handle( $timestamp, $level, $message, $context );
if ( false === $continue ) {
break;
@@ -108,7 +105,6 @@ class WC_Logger {
* Adds an emergency level message.
*
* @see WC_Logger::log
- *
*/
public function emergency( $message, $context = array() ) {
$this->log( self::EMERGENCY, $message, $context );
@@ -118,7 +114,6 @@ class WC_Logger {
* Adds an alert level message.
*
* @see WC_Logger::log
- *
*/
public function alert( $message, $context = array() ) {
$this->log( self::ALERT, $message, $context );
@@ -128,7 +123,6 @@ class WC_Logger {
* Adds a critical level message.
*
* @see WC_Logger::log
- *
*/
public function critical( $message, $context = array() ) {
$this->log( self::CRITICAL, $message, $context );
@@ -138,7 +132,6 @@ class WC_Logger {
* Adds an error level message.
*
* @see WC_Logger::log
- *
*/
public function error( $message, $context = array() ) {
$this->log( self::ERROR, $message, $context );
@@ -148,7 +141,6 @@ class WC_Logger {
* Adds a warning level message.
*
* @see WC_Logger::log
- *
*/
public function warning( $message, $context = array() ) {
$this->log( self::WARNING, $message, $context );
@@ -158,7 +150,6 @@ class WC_Logger {
* Adds a notice level message.
*
* @see WC_Logger::log
- *
*/
public function notice( $message, $context = array() ) {
$this->log( self::NOTICE, $message, $context );
@@ -168,7 +159,6 @@ class WC_Logger {
* Adds a info level message.
*
* @see WC_Logger::log
- *
*/
public function info( $message, $context = array() ) {
$this->log( self::INFO, $message, $context );
@@ -178,7 +168,6 @@ class WC_Logger {
* Adds a debug level message.
*
* @see WC_Logger::log
- *
*/
public function debug( $message, $context = array() ) {
$this->log( self::DEBUG, $message, $context );
diff --git a/includes/log-handlers/class-wc-log-handler-email.php b/includes/log-handlers/class-wc-log-handler-email.php
index 2c01f1142dc..40d23b6d777 100644
--- a/includes/log-handlers/class-wc-log-handler-email.php
+++ b/includes/log-handlers/class-wc-log-handler-email.php
@@ -32,7 +32,7 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
* @param $args additional args. {
* Optional. @see WC_Log_Handler::__construct.
*
- * @type string|arr $to Optional. Email or emails to recieve log messages. Default site admin.
+ * @type string|array $to Optional. Email or emails to recieve log messages. Default site admin.
* }
*/
public function __construct( $args = array() ) {
@@ -57,19 +57,18 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
/**
* Handle a log entry.
*
+ * @param int $timestamp Log timestamp.
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
- * @param string $message
- * @param array $context {
- * Array with additional information
- * }
+ * @param string $message Log message.
+ * @param array $context Optional. Additional information for log handlers.
*
* @return bool log entry should bubble to further loggers.
*/
- public function handle( $level, $timestamp, $message, $context ) {
+ public function handle( $timestamp, $level, $message, $context ) {
if ( $this->should_handle( $level ) ) {
- $subject = $this->get_subject( $level, $timestamp, $message, $context );
- $body = $this->get_body( $level, $timestamp, $message, $context );
+ $subject = $this->get_subject( $timestamp, $level, $message, $context );
+ $body = $this->get_body( $timestamp, $level, $message, $context );
wp_mail( $this->to, $subject, $body );
}
@@ -79,15 +78,14 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
/**
* Build subject for log email.
*
+ * @param int $timestamp Log timestamp.
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
- * @param string $message
- * @param array $context {
- * Array with additional information
- * }
+ * @param string $message Log message.
+ * @param array $context Optional. Additional information for log handlers.
*
* @return string subject
*/
- public function get_subject( $level, $timestamp, $message, $context ) {
+ public function get_subject( $timestamp, $level, $message, $context ) {
$site_name = get_bloginfo( 'name' );
return sprintf( __( '[%1$s] WooCommerce log message from %2$s', 'woocommerce' ), strtoupper( $level ), $site_name );
}
@@ -95,16 +93,15 @@ class WC_Log_Handler_Email extends WC_Log_Handler {
/**
* Build body for log email.
*
+ * @param int $timestamp Log timestamp.
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
- * @param string $message
- * @param array $context {
- * Array with additional information
- * }
+ * @param string $message Log message.
+ * @param array $context Optional. Additional information for log handlers.
*
* @return string body
*/
- public function get_body( $level, $timestamp, $message, $context ) {
- $entry = $this->format_entry( $level, $timestamp, $message, $context );
+ public function get_body( $timestamp, $level, $message, $context ) {
+ $entry = $this->format_entry( $timestamp, $level, $message, $context );
return __( 'You have recieved the following WooCommerce log message:', 'woocommerce' )
. PHP_EOL . PHP_EOL . $entry;
}
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index c3b9d05e817..ca11f9fc993 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -70,17 +70,18 @@ class WC_Log_Handler_File extends WC_Log_Handler {
/**
* Handle a log entry.
*
+ * @param int $timestamp Log timestamp.
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
- * @param string $message
+ * @param string $message Log message.
* @param array $context {
- * Array with additional information
+ * Additional information for log handlers.
*
- * @type string $tag Optional. The tag will be used to determine which file an entry will be written to.
+ * @type string $tag Optional. Determines log file to write to. Default 'log'.
* }
*
- * @return bool log entry should bubble to further loggers.
+ * @return bool Log entry should bubble to further loggers.
*/
- public function handle( $level, $timestamp, $message, $context ) {
+ public function handle( $timestamp, $level, $message, $context ) {
if ( ! $this->should_handle( $level ) ) {
return true;
@@ -92,29 +93,23 @@ class WC_Log_Handler_File extends WC_Log_Handler {
$handle = 'log';
}
- $entry = $this->format_entry( $level, $timestamp, $message, $context );
+ $entry = $this->format_entry( $timestamp, $level, $message, $context );
// 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
+ * Builds a log entry text from timestamp, level and message.
*
+ * @param int $timestamp Log timestamp.
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
- * @param int $timestamp log entry timestamp
- * @param string $message provided log message
- * @param array $context {
- * Array with additional information
+ * @param string $message Log message.
+ * @param array $context Additional information for log handlers.
*
- * @type string $tag Optional. The tag will be used to determine which file an entry will be written to.
- * }
- *
- * @return string Formatted log entry
+ * @return string Formatted log entry.
*/
- public function format_entry( $level, $timestamp, $message, $context ) {
+ public function format_entry( $timestamp, $level, $message, $context ) {
if ( isset( $context['_legacy'] ) && true === $context['_legacy'] ) {
if ( isset( $context['tag'] ) && $context['tag'] ) {
@@ -126,7 +121,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
$time = date_i18n( 'm-d-Y @ H:i:s' );
$entry = "{$time} - {$message}";
} else {
- $entry = parent::format_entry( $level, $timestamp, $message, $context );
+ $entry = parent::format_entry( $timestamp, $level, $message, $context );
}
return $entry;
@@ -135,9 +130,9 @@ class WC_Log_Handler_File extends WC_Log_Handler {
/**
* Open log file for writing.
*
- * @param string $handle
- * @param string $mode
- * @return bool success
+ * @param string $handle Log handle.
+ * @param string $mode Optional. File mode. Default 'a'.
+ * @return bool Success.
*/
protected function open( $handle, $mode = 'a' ) {
if ( $this->is_open( $handle ) ) {
diff --git a/tests/unit-tests/log/log-handler-file.php b/tests/unit-tests/log/log-handler-file.php
index 5e5f344e4d7..b2cb7ea5f2c 100644
--- a/tests/unit-tests/log/log-handler-file.php
+++ b/tests/unit-tests/log/log-handler-file.php
@@ -56,7 +56,7 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
public function test_legacy_format() {
$handler = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
- $handler->handle( 'info', time(), 'this is a message', array( 'tag' => 'unit-tests', '_legacy' => true ) );
+ $handler->handle( time(), 'info', 'this is a message', array( 'tag' => 'unit-tests', '_legacy' => true ) );
$this->assertStringMatchesFormat( '%d-%d-%d @ %d:%d:%d - %s', $this->read_content( 'unit-tests' ) );
$this->assertStringEndsWith( ' - this is a message' . PHP_EOL, $this->read_content( 'unit-tests' ) );
@@ -70,7 +70,7 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
public function test_clear() {
$handler = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
$log_name = '_test_clear';
- $handler->handle( 'debug', time(), 'debug', array( 'tag' => $log_name ) );
+ $handler->handle( time(), 'debug', 'debug', array( 'tag' => $log_name ) );
$handler->clear( $log_name );
$this->assertEquals( '', $this->read_content( $log_name ) );
}
@@ -83,7 +83,7 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
public function test_remove() {
$handler = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
$log_name = '_test_remove';
- $handler->handle( 'debug', time(), 'debug', array( 'tag' => $log_name ) );
+ $handler->handle( time(), 'debug', 'debug', array( 'tag' => $log_name ) );
$handler->remove( $log_name );
$this->assertFileNotExists( wc_get_log_file_path( $log_name ) );
}
@@ -97,14 +97,14 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
$handler = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
$time = time();
- $handler->handle( 'debug', $time, 'debug', array() );
- $handler->handle( 'info', $time, 'info', array() );
- $handler->handle( 'notice', $time, 'notice', array() );
- $handler->handle( 'warning', $time, 'warning', array() );
- $handler->handle( 'error', $time, 'error', array() );
- $handler->handle( 'critical', $time, 'critical', array() );
- $handler->handle( 'alert', $time, 'alert', array() );
- $handler->handle( 'emergency', $time, 'emergency', array() );
+ $handler->handle( $time, 'debug', 'debug', array() );
+ $handler->handle( $time, 'info', 'info', array() );
+ $handler->handle( $time, 'notice', 'notice', array() );
+ $handler->handle( $time, 'warning', 'warning', array() );
+ $handler->handle( $time, 'error', 'error', array() );
+ $handler->handle( $time, 'critical', 'critical', array() );
+ $handler->handle( $time, 'alert', 'alert', array() );
+ $handler->handle( $time, 'emergency', 'emergency', array() );
$log_content = $this->read_content( 'log' );
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
@@ -120,14 +120,14 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
$time = time();
$context_tag = array( 'tag' => 'A' );
- $handler->handle( 'debug', $time, 'debug', $context_tag );
- $handler->handle( 'info', $time, 'info', $context_tag );
- $handler->handle( 'notice', $time, 'notice', $context_tag );
- $handler->handle( 'warning', $time, 'warning', $context_tag );
- $handler->handle( 'error', $time, 'error', $context_tag );
- $handler->handle( 'critical', $time, 'critical', $context_tag );
- $handler->handle( 'alert', $time, 'alert', $context_tag );
- $handler->handle( 'emergency', $time, 'emergency', $context_tag );
+ $handler->handle( $time, 'debug', 'debug', $context_tag );
+ $handler->handle( $time, 'info', 'info', $context_tag );
+ $handler->handle( $time, 'notice', 'notice', $context_tag );
+ $handler->handle( $time, 'warning', 'warning', $context_tag );
+ $handler->handle( $time, 'error', 'error', $context_tag );
+ $handler->handle( $time, 'critical', 'critical', $context_tag );
+ $handler->handle( $time, 'alert', 'alert', $context_tag );
+ $handler->handle( $time, 'emergency', 'emergency', $context_tag );
$log_content = $this->read_content( 'A' );
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
@@ -156,7 +156,7 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
$context_tag = array( 'tag' => $log_name );
- $handler->handle( 'emergency', $time, 'emergency', $context_tag );
+ $handler->handle( $time, 'emergency', 'emergency', $context_tag );
$this->assertFileExists( wc_get_log_file_path( $log_name ) );
$this->assertStringEndsWith( 'EMERGENCY emergency' . PHP_EOL, $this->read_content( $log_name ) );
From 09ea8b074e09f6a69fac00d022a87299a4bc305d Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 20 Nov 2016 14:34:26 +0100
Subject: [PATCH 040/734] Prefer PHP_EOL over "\n"
---
includes/log-handlers/class-wc-log-handler-file.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index ca11f9fc993..e0c00bf22e6 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -198,7 +198,7 @@ class WC_Log_Handler_File extends WC_Log_Handler {
}
if ( $this->open( $handle ) && is_resource( $this->handles[ $handle ] ) ) {
- $result = fwrite( $this->handles[ $handle ], $entry . "\n" );
+ $result = fwrite( $this->handles[ $handle ], $entry . PHP_EOL );
}
return false !== $result;
From 5a9db9738f41b7c5ae804c080a7e3425291975d0 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 20 Nov 2016 15:17:55 +0100
Subject: [PATCH 041/734] Add DB log handler
---
.../log-handlers/class-wc-log-handler-db.php | 85 +++++++++++++++++++
1 file changed, 85 insertions(+)
create mode 100644 includes/log-handlers/class-wc-log-handler-db.php
diff --git a/includes/log-handlers/class-wc-log-handler-db.php b/includes/log-handlers/class-wc-log-handler-db.php
new file mode 100644
index 00000000000..c7f7267be00
--- /dev/null
+++ b/includes/log-handlers/class-wc-log-handler-db.php
@@ -0,0 +1,85 @@
+should_handle( $level ) ) {
+ return true;
+ }
+
+ if ( isset( $context['tag'] ) && $context['tag'] ) {
+ $tag = $context['tag'];
+ } else {
+ $tag = '';
+ }
+
+ // Bubble if add is NOT successful
+ return ! $this->add( $timestamp, $level, $message, $tag, $context );
+ }
+
+ /**
+ * Add a log entry to chosen file.
+ *
+ * @param int $timestamp Log timestamp.
+ * @param string $level emergency|alert|critical|error|warning|notice|info|debug
+ * @param string $message Log message.
+ * @param string $tag Log tag. Useful for filtering and sorting.
+ * @param array $context {
+ * Context will be serialized and stored in database.
+ * }
+ *
+ * @return bool True if write was successful.
+ */
+ public function add( $timestamp, $level, $message, $tag, $context ) {
+ global $wpdb;
+
+ $insert = array(
+ 'timestamp' => date( 'Y-m-d H:i:s', $timestamp ),
+ 'level' => $level,
+ 'message' => $message,
+ 'tag' => $tag,
+ );
+
+ if ( ! empty( $context ) ) {
+ $insert['context'] = serialize( $context );
+ }
+
+ $result = $wpdb->insert( "{$wpdb->prefix}woocommerce_log", $insert );
+
+ var_dump( $result );
+
+ return false !== $result;
+ }
+
+}
From f761183363c3d179086a5d8194229198747d8f01 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 20 Nov 2016 15:18:06 +0100
Subject: [PATCH 042/734] Update @package for log handlers
---
includes/log-handlers/class-wc-log-handler-db.php | 6 +-----
includes/log-handlers/class-wc-log-handler-email.php | 2 +-
includes/log-handlers/class-wc-log-handler-file.php | 2 +-
3 files changed, 3 insertions(+), 7 deletions(-)
diff --git a/includes/log-handlers/class-wc-log-handler-db.php b/includes/log-handlers/class-wc-log-handler-db.php
index c7f7267be00..e8441180b15 100644
--- a/includes/log-handlers/class-wc-log-handler-db.php
+++ b/includes/log-handlers/class-wc-log-handler-db.php
@@ -75,11 +75,7 @@ class WC_Log_Handler_DB extends WC_Log_Handler {
$insert['context'] = serialize( $context );
}
- $result = $wpdb->insert( "{$wpdb->prefix}woocommerce_log", $insert );
-
- var_dump( $result );
-
- return false !== $result;
+ return false !== $wpdb->insert( "{$wpdb->prefix}woocommerce_log", $insert );
}
}
diff --git a/includes/log-handlers/class-wc-log-handler-email.php b/includes/log-handlers/class-wc-log-handler-email.php
index 40d23b6d777..2d343e9b7a1 100644
--- a/includes/log-handlers/class-wc-log-handler-email.php
+++ b/includes/log-handlers/class-wc-log-handler-email.php
@@ -12,7 +12,7 @@ if ( ! class_exists( 'WC_Log_Handler' ) ) {
*
* @class WC_Log_Handler_Email
* @version 1.0.0
- * @package WooCommerce/Classes
+ * @package WooCommerce/Classes/Log_Handlers
* @category Class
* @author WooThemes
*/
diff --git a/includes/log-handlers/class-wc-log-handler-file.php b/includes/log-handlers/class-wc-log-handler-file.php
index e0c00bf22e6..b98bed4c1df 100644
--- a/includes/log-handlers/class-wc-log-handler-file.php
+++ b/includes/log-handlers/class-wc-log-handler-file.php
@@ -12,7 +12,7 @@ if ( ! class_exists( 'WC_Log_Handler' ) ) {
*
* @class WC_Log_Handler_File
* @version 1.0.0
- * @package WooCommerce/Classes
+ * @package WooCommerce/Classes/Log_Handlers
* @category Class
* @author WooThemes
*/
From bc3f788507db1eaedfeddeb9dee2614880bb5f1b Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 20 Nov 2016 18:56:12 +0100
Subject: [PATCH 043/734] Implements database log viewer
---
.../admin/class-wc-admin-log-table-list.php | 217 ++++++++++++++++++
includes/admin/class-wc-admin-status.php | 23 ++
.../admin/views/html-admin-page-status.php | 4 +
3 files changed, 244 insertions(+)
create mode 100644 includes/admin/class-wc-admin-log-table-list.php
diff --git a/includes/admin/class-wc-admin-log-table-list.php b/includes/admin/class-wc-admin-log-table-list.php
new file mode 100644
index 00000000000..269374e8d93
--- /dev/null
+++ b/includes/admin/class-wc-admin-log-table-list.php
@@ -0,0 +1,217 @@
+ __( 'log', 'woocommerce' ),
+ 'plural' => __( 'logs', 'woocommerce' ),
+ 'ajax' => false,
+ ) );
+ }
+
+ /**
+ * Get list columns.
+ *
+ * @return array
+ */
+ public function get_columns() {
+ return array(
+ 'cb' => '',
+ 'timestamp' => __( 'Timestamp', 'woocommerce' ),
+ 'level' => __( 'Level', 'woocommerce' ),
+ 'message' => __( 'Message', 'woocommerce' ),
+ 'tag' => __( 'Tag', 'woocommerce' ),
+ );
+ }
+
+ /**
+ * Column cb.
+ *
+ * @param array $log
+ * @return string
+ */
+ public function column_cb( $log ) {
+ return sprintf( '', $log['log_id'] );
+ }
+
+ /**
+ * Timestamp column.
+ *
+ * @param array $log
+ * @return string
+ */
+ public function column_timestamp( $log ) {
+ return esc_html( $log['timestamp'] );
+ }
+
+ /**
+ * Level column.
+ *
+ * @param array $log
+ * @return string
+ */
+ public function column_level( $log ) {
+ $level_key = $log['level'];
+ $levels = array(
+ 'emergency' => _x( 'Emergency', 'Log level', 'woocommerce' ),
+ 'alert' => _x( 'Alert', 'Log level', 'woocommerce' ),
+ 'critical' => _x( 'Critical', 'Log level', 'woocommerce' ),
+ 'error' => _x( 'Error', 'Log level', 'woocommerce' ),
+ 'warning' => _x( 'Warning', 'Log level', 'woocommerce' ),
+ 'notice' => _x( 'Notice', 'Log level', 'woocommerce' ),
+ 'info' => _x( 'Info', 'Log level', 'woocommerce' ),
+ 'debug' => _x( 'Debug', 'Log level', 'woocommerce' ),
+ );
+
+ if ( isset( $levels[ $level_key ] ) ) {
+ return esc_html( $levels[ $level_key ] );
+ } else {
+ return '';
+ }
+ }
+
+ /**
+ * Message column.
+ *
+ * @param array $log
+ * @return string
+ */
+ public function column_message( $log ) {
+ return esc_html( $log['message'] );
+ }
+
+ /**
+ * Tag column.
+ *
+ * @param array $log
+ * @return string
+ */
+ public function column_tag( $log ) {
+ return esc_html( $log['tag'] );
+ }
+
+ /**
+ * Get bulk actions.
+ *
+ * @return array
+ */
+ protected function get_bulk_actions() {
+ return array();
+ }
+
+ /**
+ * Get a list of sortable columns.
+ *
+ * @return array
+ */
+ protected function get_sortable_columns() {
+ return array(
+ 'timestamp' => array( 'timestamp', true ),
+ 'level' => array( 'level', true ),
+ 'tag' => array( 'tag', true ),
+ );
+ }
+
+ /**
+ * Prepare table list items.
+ */
+ public function prepare_items() {
+ global $wpdb;
+
+ $per_page = apply_filters( 'woocommerce_status_log_items_per_page', 10 );
+ $columns = $this->get_columns();
+ $hidden = array();
+ $sortable = $this->get_sortable_columns();
+
+ // Column headers
+ $this->_column_headers = array( $columns, $hidden, $sortable );
+
+ $current_page = $this->get_pagenum();
+ if ( 1 < $current_page ) {
+ $offset = $per_page * ( $current_page - 1 );
+ } else {
+ $offset = 0;
+ }
+
+ $search = '';
+
+ if ( ! empty( $_REQUEST['s'] ) ) {
+ $search = "AND message LIKE '%" . esc_sql( $wpdb->esc_like( wc_clean( $_REQUEST['s'] ) ) ) . "%' ";
+ }
+
+ if ( ! empty( $_REQUEST['orderby'] ) ) {
+ switch ( $_REQUEST['orderby'] ) {
+
+ // Level requires special case to order Emergency -> Debug
+ case 'level':
+ $order_by = "CASE level "
+ . "WHEN 'emergency' THEN 8 "
+ . "WHEN 'alert' THEN 7 "
+ . "WHEN 'critical' THEN 6 "
+ . "WHEN 'error' THEN 5 "
+ . "WHEN 'warning' THEN 4 "
+ . "WHEN 'notice' THEN 3 "
+ . "WHEN 'info' THEN 2 "
+ . "WHEN 'debug' THEN 1 "
+ . "ELSE 0 "
+ . "END";
+ break;
+
+ // Intentional cascade, these are valid values.
+ case 'timestamp':
+ case 'tag':
+ $order_by = $_REQUEST['orderby'];
+ break;
+
+ default:
+ $order_by = 'log_id';
+ break;
+ }
+ } else {
+ $order_by = 'log_id';
+ }
+
+ if ( ! empty( $_REQUEST['order'] ) && 'asc' === strtolower( $_REQUEST['order'] ) ) {
+ $order_order = 'ASC';
+ } else {
+ $order_order = 'DESC';
+ }
+
+
+ $logs = $wpdb->get_results(
+ "SELECT log_id, timestamp, level, message, tag FROM {$wpdb->prefix}woocommerce_log WHERE 1 = 1 {$search}" .
+ $wpdb->prepare( "ORDER BY {$order_by} {$order_order} LIMIT %d OFFSET %d", $per_page, $offset ), ARRAY_A
+ );
+
+ $count = $wpdb->get_var( "SELECT COUNT(log_id) FROM {$wpdb->prefix}woocommerce_log WHERE 1 = 1 {$search};" );
+
+ $this->items = $logs;
+
+ // Set the pagination
+ $this->set_pagination_args( array(
+ 'total_items' => $count,
+ 'per_page' => $per_page,
+ 'total_pages' => ceil( $count / $per_page ),
+ ) );
+ }
+}
diff --git a/includes/admin/class-wc-admin-status.php b/includes/admin/class-wc-admin-status.php
index 8f09658d374..2cf87f4c04d 100644
--- a/includes/admin/class-wc-admin-status.php
+++ b/includes/admin/class-wc-admin-status.php
@@ -95,6 +95,29 @@ class WC_Admin_Status {
include_once( 'views/html-admin-page-status-logs.php' );
}
+ /**
+ * Show db logs
+ *
+ * @todo Make woocommerce base logger configurable, and show page based on configuration.
+ */
+ public static function status_logs_db() {
+ $log_table_list = new WC_Admin_Log_Table_List();
+ $log_table_list->prepare_items();
+
+ echo '';
+ }
+
/**
* Retrieve metadata from a file. Based on WP Core's get_file_data function.
* @since 2.1.1
diff --git a/includes/admin/views/html-admin-page-status.php b/includes/admin/views/html-admin-page-status.php
index 63ec0a37b01..15a451031b7 100644
--- a/includes/admin/views/html-admin-page-status.php
+++ b/includes/admin/views/html-admin-page-status.php
@@ -12,6 +12,7 @@ $tabs = array(
'status' => __( 'System status', 'woocommerce' ),
'tools' => __( 'Tools', 'woocommerce' ),
'logs' => __( 'Logs', 'woocommerce' ),
+ 'logs-db' => __( 'Database logs', 'woocommerce' ),
);
$tabs = apply_filters( 'woocommerce_admin_status_tabs', $tabs );
?>
@@ -34,6 +35,9 @@ $tabs = apply_filters( 'woocommerce_admin_status_tabs', $tabs );
case "logs" :
WC_Admin_Status::status_logs();
break;
+ case "logs-db" :
+ WC_Admin_Status::status_logs_db();
+ break;
default :
if ( array_key_exists( $current_tab, $tabs ) && has_action( 'woocommerce_admin_status_content_' . $current_tab ) ) {
do_action( 'woocommerce_admin_status_content_' . $current_tab );
From 30fdca65a941670e0fc932872b5af5784c59a7ee Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 20 Nov 2016 19:16:18 +0100
Subject: [PATCH 044/734] Log viewer via html template
---
includes/admin/class-wc-admin-status.php | 13 +------------
.../views/html-admin-page-status-logs-db.php | 18 ++++++++++++++++++
2 files changed, 19 insertions(+), 12 deletions(-)
create mode 100644 includes/admin/views/html-admin-page-status-logs-db.php
diff --git a/includes/admin/class-wc-admin-status.php b/includes/admin/class-wc-admin-status.php
index 2cf87f4c04d..a9f2929854f 100644
--- a/includes/admin/class-wc-admin-status.php
+++ b/includes/admin/class-wc-admin-status.php
@@ -104,18 +104,7 @@ class WC_Admin_Status {
$log_table_list = new WC_Admin_Log_Table_List();
$log_table_list->prepare_items();
- echo '';
+ include_once( 'views/html-admin-page-status-logs-db.php' );
}
/**
diff --git a/includes/admin/views/html-admin-page-status-logs-db.php b/includes/admin/views/html-admin-page-status-logs-db.php
new file mode 100644
index 00000000000..be5cbaef1cc
--- /dev/null
+++ b/includes/admin/views/html-admin-page-status-logs-db.php
@@ -0,0 +1,18 @@
+
+
From d6a6b8771de637214660005da3e9c065f08e0eda Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 20 Nov 2016 19:43:26 +0100
Subject: [PATCH 045/734] Add level views to db log table
---
.../admin/class-wc-admin-log-table-list.php | 44 +++++++++++++++++--
.../views/html-admin-page-status-logs-db.php | 2 +-
2 files changed, 41 insertions(+), 5 deletions(-)
diff --git a/includes/admin/class-wc-admin-log-table-list.php b/includes/admin/class-wc-admin-log-table-list.php
index 269374e8d93..3ea004d5e72 100644
--- a/includes/admin/class-wc-admin-log-table-list.php
+++ b/includes/admin/class-wc-admin-log-table-list.php
@@ -29,6 +29,38 @@ class WC_Admin_Log_Table_List extends WP_List_Table {
) );
}
+ /**
+ * Get list views.
+ *
+ * @return array
+ */
+ public function get_views() {
+
+ $current = ! empty( $_REQUEST['level'] ) ? $_REQUEST['level'] : false;
+
+ $views = array();
+
+ $process_levels = array(
+ array( 'query_arg' => false, 'label' => _x( 'All levels', 'Log level', 'woocommerce' ) ),
+ array( 'query_arg' => 'emergency', 'label' => _x( 'Emergency', 'Log level', 'woocommerce' ) ),
+ array( 'query_arg' => 'alert', 'label' => _x( 'Alert', 'Log level', 'woocommerce' ) ),
+ array( 'query_arg' => 'critical', 'label' => _x( 'Critical', 'Log level', 'woocommerce' ) ),
+ array( 'query_arg' => 'error', 'label' => _x( 'Error', 'Log level', 'woocommerce' ) ),
+ array( 'query_arg' => 'warning', 'label' => _x( 'Warning', 'Log level', 'woocommerce' ) ),
+ array( 'query_arg' => 'notice', 'label' => _x( 'Notice', 'Log level', 'woocommerce' ) ),
+ array( 'query_arg' => 'info', 'label' => _x( 'Info', 'Log level', 'woocommerce' ) ),
+ array( 'query_arg' => 'debug', 'label' => _x( 'Debug', 'Log level', 'woocommerce' ) ),
+ );
+
+ foreach ( $process_levels as $level ) {
+ $url = esc_url( add_query_arg( 'level', $level['query_arg'] ) );
+ $class = $current === $level['query_arg'] ? ' class="current"' : '';
+ $views[] = sprintf( '%3$s', $url, $class, esc_html( $level['label'] ) );
+ }
+
+ return $views;
+ }
+
/**
* Get list columns.
*
@@ -153,10 +185,14 @@ class WC_Admin_Log_Table_List extends WP_List_Table {
$offset = 0;
}
- $search = '';
+ $level_filter = '';
+ if ( ! empty( $_REQUEST['level'] ) ) {
+ $level_filter = $wpdb->prepare( 'AND level = %s', array( $_REQUEST['level'] ) );
+ }
+ $search = '';
if ( ! empty( $_REQUEST['s'] ) ) {
- $search = "AND message LIKE '%" . esc_sql( $wpdb->esc_like( wc_clean( $_REQUEST['s'] ) ) ) . "%' ";
+ $search = "AND tag LIKE '%" . esc_sql( $wpdb->esc_like( wc_clean( $_REQUEST['s'] ) ) ) . "%' ";
}
if ( ! empty( $_REQUEST['orderby'] ) ) {
@@ -199,11 +235,11 @@ class WC_Admin_Log_Table_List extends WP_List_Table {
$logs = $wpdb->get_results(
- "SELECT log_id, timestamp, level, message, tag FROM {$wpdb->prefix}woocommerce_log WHERE 1 = 1 {$search}" .
+ "SELECT log_id, timestamp, level, message, tag FROM {$wpdb->prefix}woocommerce_log WHERE 1 = 1 {$level_filter} {$search}" .
$wpdb->prepare( "ORDER BY {$order_by} {$order_order} LIMIT %d OFFSET %d", $per_page, $offset ), ARRAY_A
);
- $count = $wpdb->get_var( "SELECT COUNT(log_id) FROM {$wpdb->prefix}woocommerce_log WHERE 1 = 1 {$search};" );
+ $count = $wpdb->get_var( "SELECT COUNT(log_id) FROM {$wpdb->prefix}woocommerce_log WHERE 1 = 1 {$level_filter} {$search};" );
$this->items = $logs;
diff --git a/includes/admin/views/html-admin-page-status-logs-db.php b/includes/admin/views/html-admin-page-status-logs-db.php
index be5cbaef1cc..de07dde00e0 100644
--- a/includes/admin/views/html-admin-page-status-logs-db.php
+++ b/includes/admin/views/html-admin-page-status-logs-db.php
@@ -10,7 +10,7 @@ if ( ! defined( 'ABSPATH' ) ) {
+
diff --git a/includes/log-handlers/class-wc-log-handler-db.php b/includes/log-handlers/class-wc-log-handler-db.php
index e8441180b15..9210924b115 100644
--- a/includes/log-handlers/class-wc-log-handler-db.php
+++ b/includes/log-handlers/class-wc-log-handler-db.php
@@ -78,4 +78,15 @@ class WC_Log_Handler_DB extends WC_Log_Handler {
return false !== $wpdb->insert( "{$wpdb->prefix}woocommerce_log", $insert );
}
+ /**
+ * Clear all logs from the DB.
+ *
+ * @return bool True if flush was successful.
+ */
+ public static function flush() {
+ global $wpdb;
+
+ return $wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_log" );
+ }
+
}
From 9d8001730866c0e697a5445fd9e22d256295cbd6 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 26 Nov 2016 20:08:25 +0100
Subject: [PATCH 059/734] Mark db handler add method as static
---
includes/log-handlers/class-wc-log-handler-db.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/includes/log-handlers/class-wc-log-handler-db.php b/includes/log-handlers/class-wc-log-handler-db.php
index 9210924b115..fa42edc81bc 100644
--- a/includes/log-handlers/class-wc-log-handler-db.php
+++ b/includes/log-handlers/class-wc-log-handler-db.php
@@ -61,7 +61,7 @@ class WC_Log_Handler_DB extends WC_Log_Handler {
*
* @return bool True if write was successful.
*/
- public function add( $timestamp, $level, $message, $tag, $context ) {
+ public static function add( $timestamp, $level, $message, $tag, $context ) {
global $wpdb;
$insert = array(
From 05146089cd5ebca4df74a4dc8cdfde7ff2d841ce Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 26 Nov 2016 21:45:36 +0100
Subject: [PATCH 060/734] Add DB handler tests
---
tests/unit-tests/log/log-handler-db.php | 123 ++++++++++++++++++++++++
1 file changed, 123 insertions(+)
create mode 100644 tests/unit-tests/log/log-handler-db.php
diff --git a/tests/unit-tests/log/log-handler-db.php b/tests/unit-tests/log/log-handler-db.php
new file mode 100644
index 00000000000..463f8b12f2b
--- /dev/null
+++ b/tests/unit-tests/log/log-handler-db.php
@@ -0,0 +1,123 @@
+ 'debug' ) );
+ $time = time();
+
+ $handler->handle( $time, 'debug', 'msg_debug', array( 'tag' => 'tag_debug' ) );
+ $handler->handle( $time, 'info', 'msg_info', array( 'tag' => 'tag_info' ) );
+ $handler->handle( $time, 'notice', 'msg_notice', array( 'tag' => 'tag_notice' ) );
+ $handler->handle( $time, 'warning', 'msg_warning', array( 'tag' => 'tag_warning' ) );
+ $handler->handle( $time, 'error', 'msg_error', array( 'tag' => 'tag_error' ) );
+ $handler->handle( $time, 'critical', 'msg_critical', array( 'tag' => 'tag_critical' ) );
+ $handler->handle( $time, 'alert', 'msg_alert', array( 'tag' => 'tag_alert' ) );
+ $handler->handle( $time, 'emergency', 'msg_emergency', array( 'tag' => 'tag_emergency' ) );
+
+ $log_entries = $wpdb->get_results( "SELECT timestamp, level, message, tag FROM {$wpdb->prefix}woocommerce_log", ARRAY_A );
+
+ $expected_ts = date( 'Y-m-d H:i:s', $time );
+ $expected = array(
+ array(
+ 'timestamp' => $expected_ts,
+ 'level' => 'debug',
+ 'message' => 'msg_debug',
+ 'tag' => 'tag_debug',
+ ),
+ array(
+ 'timestamp' => $expected_ts,
+ 'level' => 'info',
+ 'message' => 'msg_info',
+ 'tag' => 'tag_info',
+ ),
+ array(
+ 'timestamp' => $expected_ts,
+ 'level' => 'notice',
+ 'message' => 'msg_notice',
+ 'tag' => 'tag_notice',
+ ),
+ array(
+ 'timestamp' => $expected_ts,
+ 'level' => 'warning',
+ 'message' => 'msg_warning',
+ 'tag' => 'tag_warning',
+ ),
+ array(
+ 'timestamp' => $expected_ts,
+ 'level' => 'error',
+ 'message' => 'msg_error',
+ 'tag' => 'tag_error',
+ ),
+ array(
+ 'timestamp' => $expected_ts,
+ 'level' => 'critical',
+ 'message' => 'msg_critical',
+ 'tag' => 'tag_critical',
+ ),
+ array(
+ 'timestamp' => $expected_ts,
+ 'level' => 'alert',
+ 'message' => 'msg_alert',
+ 'tag' => 'tag_alert',
+ ),
+ array(
+ 'timestamp' => $expected_ts,
+ 'level' => 'emergency',
+ 'message' => 'msg_emergency',
+ 'tag' => 'tag_emergency',
+ ),
+ );
+
+ $this->assertEquals( $log_entries, $expected );
+ }
+
+
+ /**
+ * Test flush.
+ *
+ * @since 2.8
+ */
+ public function test_flush() {
+ global $wpdb;
+
+ $handler = new WC_Log_Handler_DB( array( 'threshold' => 'debug' ) );
+ $time = time();
+
+ $handler->handle( $time, 'debug', '', array() );
+
+ $log_entries = $wpdb->get_results( "SELECT timestamp, level, message, tag FROM {$wpdb->prefix}woocommerce_log" );
+ $this->assertCount( 1, $log_entries );
+
+ WC_Log_Handler_DB::flush();
+
+ $log_entries = $wpdb->get_results( "SELECT timestamp, level, message, tag FROM {$wpdb->prefix}woocommerce_log" );
+ $this->assertCount( 0, $log_entries );
+ }
+
+}
From 44ec07902d85a00d1128cc69f855211f2ea81447 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 27 Nov 2016 19:15:13 +0100
Subject: [PATCH 061/734] Add confirmation to db log flush
---
includes/admin/views/html-admin-page-status-logs-db.php | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/includes/admin/views/html-admin-page-status-logs-db.php b/includes/admin/views/html-admin-page-status-logs-db.php
index d0ac9dbd116..6b6af4b24b2 100644
--- a/includes/admin/views/html-admin-page-status-logs-db.php
+++ b/includes/admin/views/html-admin-page-status-logs-db.php
@@ -23,3 +23,12 @@ if ( ! defined( 'ABSPATH' ) ) {
submit_button( __( 'Flush all logs', 'woocommerce' ), 'delete', 'flush-logs' );
?>
+
Date: Sun, 27 Nov 2016 19:34:34 +0100
Subject: [PATCH 062/734] Remove bad logger test
Test comparing equality of logger results could randomly fail depending on execution time.
The coverage was duplicated by other tests, test was redundant and has been removed.
---
tests/unit-tests/log/log.php | 41 +++---------------------------------
1 file changed, 3 insertions(+), 38 deletions(-)
diff --git a/tests/unit-tests/log/log.php b/tests/unit-tests/log/log.php
index edff6c88696..e8c9ca1e38c 100644
--- a/tests/unit-tests/log/log.php
+++ b/tests/unit-tests/log/log.php
@@ -1,11 +1,11 @@
assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
}
- /**
- * Test log( 'level', ... ) === level( ... ).
- *
- * @since 2.8
- */
- public function test_log_short_methods() {
- $log = wc_get_logger();
-
- $ctx_a = array( 'tag' => 'A' );
- $ctx_b = array( 'tag' => 'B' );
-
- $log->log( 'debug', 'debug', $ctx_a );
- $log->log( 'info', 'info', $ctx_a );
- $log->log( 'notice', 'notice', $ctx_a );
- $log->log( 'warning', 'warning', $ctx_a );
- $log->log( 'error', 'error', $ctx_a );
- $log->log( 'critical', 'critical', $ctx_a );
- $log->log( 'alert', 'alert', $ctx_a );
- $log->log( 'emergency', 'emergency', $ctx_a );
-
- $log->debug( 'debug', $ctx_b );
- $log->info( 'info', $ctx_b );
- $log->notice( 'notice', $ctx_b );
- $log->warning( 'warning', $ctx_b );
- $log->error( 'error', $ctx_b );
- $log->critical( 'critical', $ctx_b );
- $log->alert( 'alert', $ctx_b );
- $log->emergency( 'emergency', $ctx_b );
-
- $log_content_a = $this->read_content( 'A' );
- $log_content_b = $this->read_content( 'B' );
-
- $this->assertEquals( $log_content_a, $log_content_b );
- }
-
/**
* Test consumed logs do not bubble.
*
From dce4e7c21ae33eece9c1fbc99f198cffc32f407d Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 27 Nov 2016 19:37:20 +0100
Subject: [PATCH 063/734] Fix misnamed test class
---
tests/unit-tests/log/log-handler-db.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tests/unit-tests/log/log-handler-db.php b/tests/unit-tests/log/log-handler-db.php
index 463f8b12f2b..79716b8541f 100644
--- a/tests/unit-tests/log/log-handler-db.php
+++ b/tests/unit-tests/log/log-handler-db.php
@@ -13,7 +13,7 @@ if ( ! class_exists( 'WC_Log_Handler_DB' ) ) {
);
}
-class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
+class WC_Tests_Log_Handler_DB extends WC_Unit_Test_Case {
public function tearDown() {
WC_Log_Handler_DB::flush();
From dd2474fb3c980a557e5c4d404a90dafb334416d7 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 27 Nov 2016 19:37:56 +0100
Subject: [PATCH 064/734] Rename test file log -> logger
---
tests/unit-tests/log/{log.php => logger.php} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename tests/unit-tests/log/{log.php => logger.php} (100%)
diff --git a/tests/unit-tests/log/log.php b/tests/unit-tests/log/logger.php
similarity index 100%
rename from tests/unit-tests/log/log.php
rename to tests/unit-tests/log/logger.php
From b6304cbcc46a1a049c4ae1cd18ba4429bb38b526 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 27 Nov 2016 19:52:06 +0100
Subject: [PATCH 065/734] Prefer "unit-tests" tag for testing log handler
---
tests/unit-tests/log/log-handler-file.php | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/tests/unit-tests/log/log-handler-file.php b/tests/unit-tests/log/log-handler-file.php
index b2cb7ea5f2c..64eb34b2ca2 100644
--- a/tests/unit-tests/log/log-handler-file.php
+++ b/tests/unit-tests/log/log-handler-file.php
@@ -19,7 +19,6 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
$log_files = array(
'unit-tests',
'log',
- 'A',
'_test_clear',
'_test_remove',
'_test_log_rotate',
@@ -118,7 +117,7 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
public function test_log_file_tag() {
$handler = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
$time = time();
- $context_tag = array( 'tag' => 'A' );
+ $context_tag = array( 'tag' => 'unit-tests' );
$handler->handle( $time, 'debug', 'debug', $context_tag );
$handler->handle( $time, 'info', 'info', $context_tag );
From ceea56e323741b84afefde439fd2974bee12029d Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 27 Nov 2016 19:53:15 +0100
Subject: [PATCH 066/734] Add test for multiple file log handlers writing to
same file.
---
tests/unit-tests/log/log-handler-file.php | 27 ++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/tests/unit-tests/log/log-handler-file.php b/tests/unit-tests/log/log-handler-file.php
index 64eb34b2ca2..edfc0833aba 100644
--- a/tests/unit-tests/log/log-handler-file.php
+++ b/tests/unit-tests/log/log-handler-file.php
@@ -128,7 +128,32 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
$handler->handle( $time, 'alert', 'alert', $context_tag );
$handler->handle( $time, 'emergency', 'emergency', $context_tag );
- $log_content = $this->read_content( 'A' );
+ $log_content = $this->read_content( 'unit-tests' );
+ $this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
+ }
+
+ /**
+ * Test multiple handlers don't conflict on log writing.
+ *
+ * @since 2.8
+ */
+ public function test_multiple_handlers() {
+ $handler_a = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
+ $handler_b = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
+ $time = time();
+ $context_tag = array( 'tag' => 'unit-tests' );
+
+ // Different loggers should not conflict.
+ $handler_a->handle( $time, 'debug', 'debug', $context_tag );
+ $handler_b->handle( $time, 'info', 'info', $context_tag );
+ $handler_a->handle( $time, 'notice', 'notice', $context_tag );
+ $handler_b->handle( $time, 'warning', 'warning', $context_tag );
+ $handler_a->handle( $time, 'error', 'error', $context_tag );
+ $handler_b->handle( $time, 'critical', 'critical', $context_tag );
+ $handler_a->handle( $time, 'alert', 'alert', $context_tag );
+ $handler_b->handle( $time, 'emergency', 'emergency', $context_tag );
+
+ $log_content = $this->read_content( 'unit-tests' );
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
}
From 81223995fc442baa0141fcf042736090c2e68ac4 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sun, 27 Nov 2016 20:19:59 +0100
Subject: [PATCH 067/734] Add basic email logger test
---
tests/unit-tests/log/log-handler-email.php | 47 ++++++++++++++++++++++
1 file changed, 47 insertions(+)
create mode 100644 tests/unit-tests/log/log-handler-email.php
diff --git a/tests/unit-tests/log/log-handler-email.php b/tests/unit-tests/log/log-handler-email.php
new file mode 100644
index 00000000000..0e17023202d
--- /dev/null
+++ b/tests/unit-tests/log/log-handler-email.php
@@ -0,0 +1,47 @@
+ 'debug' ) );
+ $time = time();
+
+ $handler->handle( $time, 'debug', 'msg_debug', array() );
+
+ $mailer = tests_retrieve_phpmailer_instance();
+
+ $this->assertEquals(
+ 'You have recieved the following WooCommerce log message:' . PHP_EOL . PHP_EOL . date( 'c', $time ) . ' DEBUG msg_debug' . PHP_EOL,
+ $mailer->get_sent()->body
+ );
+ }
+
+}
From c633db9d65fc858d6a5279503da412e074d52d78 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 3 Dec 2016 18:23:52 +0100
Subject: [PATCH 068/734] Add woocommerce_default_log_handler setting
Adds configuration to settings page
Show status-logs page based on settings (unify logs & database logs)
Register default log handler by default
---
includes/admin/class-wc-admin-status.php | 15 +++++++++++---
.../settings/class-wc-settings-general.php | 12 +++++++++++
.../admin/views/html-admin-page-status.php | 4 ----
includes/wc-core-functions.php | 20 ++++++++++++-------
4 files changed, 37 insertions(+), 14 deletions(-)
diff --git a/includes/admin/class-wc-admin-status.php b/includes/admin/class-wc-admin-status.php
index 289fb738e29..803ff2046f3 100644
--- a/includes/admin/class-wc-admin-status.php
+++ b/includes/admin/class-wc-admin-status.php
@@ -77,6 +77,17 @@ class WC_Admin_Status {
* Show the logs page.
*/
public static function status_logs() {
+ if ( 'db' === get_option( 'woocommerce_default_log_handler' ) ) {
+ self::status_logs_db();
+ } else {
+ self::status_logs_file();
+ }
+ }
+
+ /**
+ * Show the log page contents for file log handler.
+ */
+ public static function status_logs_file() {
$logs = self::scan_log_files();
@@ -96,9 +107,7 @@ class WC_Admin_Status {
}
/**
- * Show db logs
- *
- * @todo Make woocommerce base logger configurable, and show page based on configuration.
+ * Show the log page contents for db log handler.
*/
public static function status_logs_db() {
diff --git a/includes/admin/settings/class-wc-settings-general.php b/includes/admin/settings/class-wc-settings-general.php
index ad7ad411bf3..5da87917bcb 100644
--- a/includes/admin/settings/class-wc-settings-general.php
+++ b/includes/admin/settings/class-wc-settings-general.php
@@ -133,6 +133,18 @@ class WC_Settings_General extends WC_Settings_Page {
),
),
+ array(
+ 'title' => __( 'Default log handler', 'woocommerce' ),
+ 'id' => 'woocommerce_default_log_handler',
+ 'default' => 'geolocation',
+ 'type' => 'select',
+ 'class' => 'wc-enhanced-select',
+ 'options' => array(
+ 'file' => __( 'File system log handler', 'woocommerce' ),
+ 'db' => __( 'Database log handler', 'woocommerce' ),
+ ),
+ ),
+
array(
'title' => __( 'Enable taxes', 'woocommerce' ),
'desc' => __( 'Enable taxes and tax calculations', 'woocommerce' ),
diff --git a/includes/admin/views/html-admin-page-status.php b/includes/admin/views/html-admin-page-status.php
index 15a451031b7..63ec0a37b01 100644
--- a/includes/admin/views/html-admin-page-status.php
+++ b/includes/admin/views/html-admin-page-status.php
@@ -12,7 +12,6 @@ $tabs = array(
'status' => __( 'System status', 'woocommerce' ),
'tools' => __( 'Tools', 'woocommerce' ),
'logs' => __( 'Logs', 'woocommerce' ),
- 'logs-db' => __( 'Database logs', 'woocommerce' ),
);
$tabs = apply_filters( 'woocommerce_admin_status_tabs', $tabs );
?>
@@ -35,9 +34,6 @@ $tabs = apply_filters( 'woocommerce_admin_status_tabs', $tabs );
case "logs" :
WC_Admin_Status::status_logs();
break;
- case "logs-db" :
- WC_Admin_Status::status_logs_db();
- break;
default :
if ( array_key_exists( $current_tab, $tabs ) && has_action( 'woocommerce_admin_status_content_' . $current_tab ) ) {
do_action( 'woocommerce_admin_status_content_' . $current_tab );
diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php
index c2164769d05..f20520ff4db 100644
--- a/includes/wc-core-functions.php
+++ b/includes/wc-core-functions.php
@@ -1454,22 +1454,28 @@ function wc_safe_dump( $expression ) {
}
/**
- * Registers the included file log handler with threshold 'debug'.
+ * Registers the default log handler.
*
* @since 2.8
* @param array $handlers
* @return array
*/
-function wc_register_file_log_handler( $handlers ) {
- if ( ! class_exists( 'WC_Log_Handler_File' ) ) {
- include_once( dirname( __FILE__ ) . '/log-handlers/class-wc-log-handler-file.php' );
+function wc_register_default_log_handler( $handlers ) {
+ if ( 'db' === get_option( 'woocommerce_default_log_handler' ) ) {
+ if ( ! class_exists( 'WC_Log_Handler_DB' ) ) {
+ include_once( dirname( __FILE__ ) . '/log-handlers/class-wc-log-handler-db.php' );
+ }
+ array_push( $handlers, new WC_Log_Handler_DB( array( 'threshold' => 'debug' ) ) );
+ } else {
+ if ( ! class_exists( 'WC_Log_Handler_File' ) ) {
+ include_once( dirname( __FILE__ ) . '/log-handlers/class-wc-log-handler-file.php' );
+ }
+ array_push( $handlers, new WC_Log_Handler_File( array( 'threshold' => 'debug' ) ) );
}
- array_push( $handlers, new WC_Log_Handler_File( array( 'threshold' => 'debug' ) ) );
-
return $handlers;
}
-add_filter( 'woocommerce_register_log_handlers', 'wc_register_file_log_handler', 0 );
+add_filter( 'woocommerce_register_log_handlers', 'wc_register_default_log_handler', 0 );
/**
* Store user agents. Used for tracker.
From e2f76bcd540db1aefe5ec54f94dee9356b0cf227 Mon Sep 17 00:00:00 2001
From: Jon Surrell
Date: Sat, 3 Dec 2016 22:26:36 +0100
Subject: [PATCH 069/734] Fix logs-db redirects
---
includes/admin/class-wc-admin-status.php | 2 +-
includes/admin/views/html-admin-page-status-logs-db.php | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/includes/admin/class-wc-admin-status.php b/includes/admin/class-wc-admin-status.php
index 803ff2046f3..4f1b76d2c22 100644
--- a/includes/admin/class-wc-admin-status.php
+++ b/includes/admin/class-wc-admin-status.php
@@ -305,7 +305,7 @@ class WC_Admin_Status {
WC_Log_Handler_DB::flush();
}
- wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs-db' ) ) );
+ wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) );
exit();
}
}
diff --git a/includes/admin/views/html-admin-page-status-logs-db.php b/includes/admin/views/html-admin-page-status-logs-db.php
index 6b6af4b24b2..b551793afb8 100644
--- a/includes/admin/views/html-admin-page-status-logs-db.php
+++ b/includes/admin/views/html-admin-page-status-logs-db.php
@@ -14,7 +14,7 @@ if ( ! defined( 'ABSPATH' ) ) {
display(); ?>
-
+
diff --git a/templates/myaccount/form-login.php b/templates/myaccount/form-login.php
index 3e7b469c4e7..bf22fdc1064 100644
--- a/templates/myaccount/form-login.php
+++ b/templates/myaccount/form-login.php
@@ -36,7 +36,7 @@ if ( ! defined( 'ABSPATH' ) ) {
-