Skip to content

Commit

Permalink
Replace log tag with log source
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Dec 29, 2016
1 parent 03889f7 commit e4d5886
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 89 deletions.
16 changes: 8 additions & 8 deletions includes/admin/class-wc-admin-log-table-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function get_columns() {
'timestamp' => __( 'Timestamp', 'woocommerce' ),
'level' => __( 'Level', 'woocommerce' ),
'message' => __( 'Message', 'woocommerce' ),
'tag' => __( 'Tag', 'woocommerce' ),
'source' => __( 'Source', 'woocommerce' ),
);
}

Expand Down Expand Up @@ -133,13 +133,13 @@ public function column_message( $log ) {
}

/**
* Tag column.
* Source column.
*
* @param array $log
* @return string
*/
public function column_tag( $log ) {
return esc_html( $log['tag'] );
public function column_source( $log ) {
return esc_html( $log['source'] );
}

/**
Expand All @@ -162,7 +162,7 @@ protected function get_sortable_columns() {
return array(
'timestamp' => array( 'timestamp', true ),
'level' => array( 'level', true ),
'tag' => array( 'tag', true ),
'source' => array( 'source', true ),
);
}

Expand Down Expand Up @@ -197,15 +197,15 @@ public function prepare_items() {

$search = '';
if ( ! empty( $_REQUEST['s'] ) ) {
$search = "AND tag LIKE '%" . esc_sql( $wpdb->esc_like( wc_clean( $_REQUEST['s'] ) ) ) . "%' ";
$search = "AND source LIKE '%" . esc_sql( $wpdb->esc_like( wc_clean( $_REQUEST['s'] ) ) ) . "%' ";
}

if ( ! empty( $_REQUEST['orderby'] ) ) {
switch ( $_REQUEST['orderby'] ) {

// Intentional cascade, these are valid values.
case 'timestamp':
case 'tag':
case 'source':
case 'level':
$order_by = $_REQUEST['orderby'];
break;
Expand All @@ -226,7 +226,7 @@ public function prepare_items() {
}

$this->items = $wpdb->get_results(
"SELECT log_id, timestamp, level, message, tag
"SELECT log_id, timestamp, level, message, source
FROM {$wpdb->prefix}woocommerce_log
WHERE 1 = 1 {$level_filter} {$search}
ORDER BY {$order_by} {$order_order} " .
Expand Down
10 changes: 5 additions & 5 deletions includes/class-wc-background-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function dispatch() {
if ( is_wp_error( $dispatched ) ) {
$logger->error(
sprintf( 'Unable to dispatch WooCommerce updater: %s', $dispatched->get_error_message() ),
array( 'tag' => 'wc_db_updates' )
array( 'source' => 'wc_db_updates' )
);
}
}
Expand Down Expand Up @@ -105,11 +105,11 @@ protected function task( $callback ) {
include_once( dirname( __FILE__ ) . '/wc-update-functions.php' );

if ( is_callable( $callback ) ) {
$logger->info( sprintf( 'Running %s callback', $callback ), array( 'tag' => 'wc_db_updates' ) );
$logger->info( sprintf( 'Running %s callback', $callback ), array( 'source' => 'wc_db_updates' ) );
call_user_func( $callback );
$logger->info( sprintf( 'Finished %s callback', $callback ), array( 'tag' => 'wc_db_updates' ) );
$logger->info( sprintf( 'Finished %s callback', $callback ), array( 'source' => 'wc_db_updates' ) );
} else {
$logger->notice( sprintf( 'Could not find %s callback', $callback ), array( 'tag' => 'wc_db_updates' ) );
$logger->notice( sprintf( 'Could not find %s callback', $callback ), array( 'source' => 'wc_db_updates' ) );
}

return false;
Expand All @@ -123,7 +123,7 @@ protected function task( $callback ) {
*/
protected function complete() {
$logger = wc_get_logger();
$logger->info( 'Data update complete', array( 'tag' => 'wc_db_updates' ) );
$logger->info( 'Data update complete', array( 'source' => 'wc_db_updates' ) );
WC_Install::update_db_version();
parent::complete();
}
Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-geo-ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ public static function log( $message, $level = 'info' ) {
if ( empty( self::$log ) ) {
self::$log = wc_get_logger();
}
self::$log->log( $level, $message, array( 'tag' => 'geoip' ) );
self::$log->log( $level, $message, array( 'source' => 'geoip' ) );
}

/**
Expand Down
6 changes: 3 additions & 3 deletions includes/class-wc-geolocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public static function update_database() {
$logger = wc_get_logger();

if ( ! is_callable( 'gzopen' ) ) {
$logger->notice( 'Server does not support gzopen', array( 'tag' => 'geolocation' ) );
$logger->notice( 'Server does not support gzopen', array( 'source' => 'geolocation' ) );
return;
}

Expand All @@ -208,13 +208,13 @@ public static function update_database() {
gzclose( $gzhandle );
fclose( $handle );
} else {
$logger->notice( 'Unable to open database file', array( 'tag' => 'geolocation' ) );
$logger->notice( 'Unable to open database file', array( 'source' => 'geolocation' ) );
}
@unlink( $tmp_database_path );
} else {
$logger->notice(
'Unable to download GeoIP Database: ' . $tmp_database_path->get_error_message(),
array( 'tag' => 'geolocation' )
array( 'source' => 'geolocation' )
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions includes/class-wc-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private static function update() {
foreach ( $update_callbacks as $update_callback ) {
$logger->info(
sprintf( 'Queuing %s - %s', $version, $update_callback ),
array( 'tag' => 'wc_db_updates' )
array( 'source' => 'wc_db_updates' )
);
self::$background_updater->push_to_queue( $update_callback );
$update_queued = true;
Expand Down Expand Up @@ -609,7 +609,7 @@ private static function get_schema() {
log_id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
timestamp datetime NOT NULL,
level smallint(4) NOT NULL,
tag varchar(255) NOT NULL,
source varchar(255) NOT NULL,
message longtext NOT NULL,
context longtext NULL,
PRIMARY KEY (log_id),
Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function should_handle( $level ) {
public function add( $handle, $message ) {
wc_deprecated_function( 'WC_Logger::add', '2.7', 'WC_Logger::log' );
$message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
$this->log( WC_Log_Levels::NOTICE, $message, array( 'tag' => $handle, '_legacy' => true ) );
$this->log( WC_Log_Levels::NOTICE, $message, array( 'source' => $handle, '_legacy' => true ) );
wc_do_deprecated_action( 'woocommerce_log_add', array( $handle, $message ), '2.7', 'This action has been deprecated with no alternative.' );
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion includes/emails/class-wc-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public function style_inline( $content ) {
$content = $emogrifier->emogrify();
} catch ( Exception $e ) {
$logger = wc_get_logger();
$logger->error( $e->getMessage(), array( 'tag' => 'emogrifier' ) );
$logger->error( $e->getMessage(), array( 'source' => 'emogrifier' ) );
}
}
return $content;
Expand Down
2 changes: 1 addition & 1 deletion includes/gateways/paypal/class-wc-gateway-paypal.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static function log( $message, $level = 'info' ) {
if ( empty( self::$log ) ) {
self::$log = wc_get_logger();
}
self::$log->log( $level, $message, array( 'tag' => 'paypal' ) );
self::$log->log( $level, $message, array( 'source' => 'paypal' ) );
}
}

Expand Down
16 changes: 8 additions & 8 deletions includes/log-handlers/class-wc-log-handler-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ class WC_Log_Handler_DB extends WC_Log_Handler {
* @param array $context {
* Additional information for log handlers.
*
* @type string $tag Optional. Tag will be available in log table. Default '';
* @type string $source Optional. Source will be available in log table. Default '';
* }
*
* @return bool False if value was not handled and true if value was handled.
*/
public function handle( $timestamp, $level, $message, $context ) {

if ( isset( $context['tag'] ) && $context['tag'] ) {
$tag = $context['tag'];
if ( isset( $context['source'] ) && $context['source'] ) {
$source = $context['source'];
} else {
$tag = '';
$source = '';
}

return $this->add( $timestamp, $level, $message, $tag, $context );
return $this->add( $timestamp, $level, $message, $source, $context );
}

/**
Expand All @@ -45,21 +45,21 @@ public function handle( $timestamp, $level, $message, $context ) {
* @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 string $source Log source. Useful for filtering and sorting.
* @param array $context {
* Context will be serialized and stored in database.
* }
*
* @return bool True if write was successful.
*/
protected static function add( $timestamp, $level, $message, $tag, $context ) {
protected static function add( $timestamp, $level, $message, $source, $context ) {
global $wpdb;

$insert = array(
'timestamp' => date( 'Y-m-d H:i:s', $timestamp ),
'level' => WC_Log_Levels::get_level_severity( $level ),
'message' => $message,
'tag' => $tag,
'source' => $source,
);

$format = array(
Expand Down
10 changes: 5 additions & 5 deletions includes/log-handlers/class-wc-log-handler-file.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function __destruct() {
* @param array $context {
* Additional information for log handlers.
*
* @type string $tag Optional. Determines log file to write to. Default 'log'.
* @type string $source Optional. Determines log file to write to. Default 'log'.
* @type bool $_legacy Optional. Default false. True to use outdated log format
* orignally used in deprecated WC_Logger::add calls.
* }
Expand All @@ -85,8 +85,8 @@ public function __destruct() {
*/
public function handle( $timestamp, $level, $message, $context ) {

if ( isset( $context['tag'] ) && $context['tag'] ) {
$handle = $context['tag'];
if ( isset( $context['source'] ) && $context['source'] ) {
$handle = $context['source'];
} else {
$handle = 'log';
}
Expand All @@ -109,8 +109,8 @@ public function handle( $timestamp, $level, $message, $context ) {
protected static function format_entry( $timestamp, $level, $message, $context ) {

if ( isset( $context['_legacy'] ) && true === $context['_legacy'] ) {
if ( isset( $context['tag'] ) && $context['tag'] ) {
$handle = $context['tag'];
if ( isset( $context['source'] ) && $context['source'] ) {
$handle = $context['source'];
} else {
$handle = 'log';
}
Expand Down
56 changes: 28 additions & 28 deletions tests/unit-tests/log/log-handler-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,82 +24,82 @@ public function test_handle() {
$time = time();
$context = array( 1, 2, 'a', 'b', 'key' => 'value' );

$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' ) );
$handler->handle( $time, 'debug', 'msg_debug', array( 'source' => 'source_debug' ) );
$handler->handle( $time, 'info', 'msg_info', array( 'source' => 'source_info' ) );
$handler->handle( $time, 'notice', 'msg_notice', array( 'source' => 'source_notice' ) );
$handler->handle( $time, 'warning', 'msg_warning', array( 'source' => 'source_warning' ) );
$handler->handle( $time, 'error', 'msg_error', array( 'source' => 'source_error' ) );
$handler->handle( $time, 'critical', 'msg_critical', array( 'source' => 'source_critical' ) );
$handler->handle( $time, 'alert', 'msg_alert', array( 'source' => 'source_alert' ) );
$handler->handle( $time, 'emergency', 'msg_emergency', array( 'source' => 'source_emergency' ) );

$handler->handle( $time, 'debug', 'context_test', $context );

$log_entries = $wpdb->get_results( "SELECT timestamp, level, message, tag, context FROM {$wpdb->prefix}woocommerce_log", ARRAY_A );
$log_entries = $wpdb->get_results( "SELECT timestamp, level, message, source, context FROM {$wpdb->prefix}woocommerce_log", ARRAY_A );

$expected_ts = date( 'Y-m-d H:i:s', $time );
$expected = array(
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'debug' ),
'message' => 'msg_debug',
'tag' => 'tag_debug',
'context' => serialize( array( 'tag' => 'tag_debug' ) ),
'source' => 'source_debug',
'context' => serialize( array( 'source' => 'source_debug' ) ),
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'info' ),
'message' => 'msg_info',
'tag' => 'tag_info',
'context' => serialize( array( 'tag' => 'tag_info' ) ),
'source' => 'source_info',
'context' => serialize( array( 'source' => 'source_info' ) ),
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'notice' ),
'message' => 'msg_notice',
'tag' => 'tag_notice',
'context' => serialize( array( 'tag' => 'tag_notice' ) ),
'source' => 'source_notice',
'context' => serialize( array( 'source' => 'source_notice' ) ),
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'warning' ),
'message' => 'msg_warning',
'tag' => 'tag_warning',
'context' => serialize( array( 'tag' => 'tag_warning' ) ),
'source' => 'source_warning',
'context' => serialize( array( 'source' => 'source_warning' ) ),
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'error' ),
'message' => 'msg_error',
'tag' => 'tag_error',
'context' => serialize( array( 'tag' => 'tag_error' ) ),
'source' => 'source_error',
'context' => serialize( array( 'source' => 'source_error' ) ),
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'critical' ),
'message' => 'msg_critical',
'tag' => 'tag_critical',
'context' => serialize( array( 'tag' => 'tag_critical' ) ),
'source' => 'source_critical',
'context' => serialize( array( 'source' => 'source_critical' ) ),
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'alert' ),
'message' => 'msg_alert',
'tag' => 'tag_alert',
'context' => serialize( array( 'tag' => 'tag_alert' ) ),
'source' => 'source_alert',
'context' => serialize( array( 'source' => 'source_alert' ) ),
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'emergency' ),
'message' => 'msg_emergency',
'tag' => 'tag_emergency',
'context' => serialize( array( 'tag' => 'tag_emergency' ) ),
'source' => 'source_emergency',
'context' => serialize( array( 'source' => 'source_emergency' ) ),
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'debug' ),
'message' => 'context_test',
'tag' => '',
'source' => '',
'context' => serialize( $context ),
),
);
Expand All @@ -121,12 +121,12 @@ public function test_flush() {

$handler->handle( $time, 'debug', '', array() );

$log_entries = $wpdb->get_results( "SELECT timestamp, level, message, tag FROM {$wpdb->prefix}woocommerce_log" );
$log_entries = $wpdb->get_results( "SELECT timestamp, level, message, source 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" );
$log_entries = $wpdb->get_results( "SELECT timestamp, level, message, source FROM {$wpdb->prefix}woocommerce_log" );
$this->assertCount( 0, $log_entries );
}

Expand Down
Loading

0 comments on commit e4d5886

Please sign in to comment.