Skip to content

Commit d6a32d0

Browse files
authored
Add feature separated logging (#1304)
* Separate logging instances for “taxes” and “shipping” features.
1 parent 3e12096 commit d6a32d0

File tree

4 files changed

+120
-65
lines changed

4 files changed

+120
-65
lines changed

classes/class-wc-connect-help-view.php

Lines changed: 79 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -350,41 +350,93 @@ protected function get_services_items() {
350350
}
351351

352352
/**
353-
* Gets the last 10 lines from the WooCommerce Services log, if it exists
353+
* Gets the last 10 lines from the WooCommerce Services log by feature, if it exists
354354
*/
355-
protected function get_debug_log_data() {
356-
$data = new stdClass;
357-
$data->key = '';
358-
$data->file = '';
355+
protected function get_debug_log_data( $feature = '' ) {
356+
$data = new stdClass;
357+
$data->key = '';
358+
$data->file = null;
359359
$data->tail = array();
360360

361-
if ( method_exists( 'WC_Admin_Status', 'scan_log_files' ) ) {
362-
$logs = WC_Admin_Status::scan_log_files();
363-
$latest_file_date = 0;
364-
$file = null;
365-
$key = null;
366-
367-
foreach ( $logs as $log_key => $log_file ) {
368-
$log_file = WC_LOG_DIR . $log_file;
369-
$file_date = filemtime( $log_file );
370-
if ( 'wc-services-' === substr( $log_key, 0, 12 ) && $latest_file_date < $file_date ) {
371-
$latest_file_date = $file_date;
372-
$file = $log_file;
373-
$key = $log_key;
374-
}
361+
if ( ! method_exists( 'WC_Admin_Status', 'scan_log_files' ) ) {
362+
return $data;
363+
}
364+
365+
$log_prefix = 'wc\-services';
366+
367+
if ( ! empty( $feature ) ) {
368+
$log_prefix .= '\-' . $feature;
369+
}
370+
371+
$logs = WC_Admin_Status::scan_log_files();
372+
$latest_file_date = 0;
373+
374+
foreach ( $logs as $log_key => $log_file ) {
375+
if ( ! preg_match( '/' . $log_prefix . '\-[0-9a-f]{32}\-log/', $log_key ) ) {
376+
continue;
375377
}
376378

377-
if ( null !== $file ) {
378-
$complete_log = file( $file );
379-
$data->key = $key;
380-
$data->file = $file;
381-
$data->tail = array_slice( $complete_log, -10 );
379+
$log_file_path = WC_LOG_DIR . $log_file;
380+
$file_date = filemtime( $log_file_path );
381+
382+
if ( $latest_file_date < $file_date ) {
383+
$latest_file_date = $file_date;
384+
$data->file = $log_file_path;
385+
$data->key = $log_key;
382386
}
383387
}
384388

389+
if ( null !== $data->file ) {
390+
$complete_log = file( $data->file );
391+
$data->tail = array_slice( $complete_log, -10 );
392+
}
393+
385394
return $data;
386395
}
387396

397+
protected function add_log_view( $title, $feature = '' ) {
398+
// add connect log tail
399+
$log_data = $this->get_debug_log_data( $feature );
400+
$line_count = count( $log_data->tail );
401+
402+
if ( $line_count < 1 ) {
403+
404+
$description = '';
405+
$log_tail = __( 'Log is empty', 'woocommerce-services' );
406+
407+
} else {
408+
409+
$url = add_query_arg(
410+
array(
411+
'page' => 'wc-status',
412+
'tab' => 'logs',
413+
'log_file' => $log_data->key
414+
),
415+
admin_url( 'admin.php' )
416+
);
417+
418+
$description = sprintf(
419+
wp_kses(
420+
__( 'Last %d entries <a href="%s">Show full log</a>', 'woocommerce-services' ),
421+
array( 'a' => array( 'href' => array() ) ) ),
422+
$line_count,
423+
esc_url( $url )
424+
);
425+
426+
$log_tail = implode( $log_data->tail, '' );
427+
428+
}
429+
430+
return (object) array(
431+
'key' => 'wcc_' . $feature . '_log_tail',
432+
'title' => $title,
433+
'type' => 'textarea',
434+
'description' => $description,
435+
'readonly' => true,
436+
'value' => $log_tail,
437+
);
438+
}
439+
388440
protected function get_debug_items() {
389441
$debug_items = array();
390442

@@ -412,39 +464,9 @@ protected function get_debug_items() {
412464
'save_on_toggle' => true,
413465
);
414466

415-
// add connect log tail
416-
$log_data = $this->get_debug_log_data();
417-
$log_tail_line_count = count( $log_data->tail );
418-
if ( $log_tail_line_count < 1 ) {
419-
$description = '';
420-
$log_tail = __( 'Log is empty', 'woocommerce-services' );
421-
} else {
422-
$url = add_query_arg(
423-
array(
424-
'page' => 'wc-status',
425-
'tab' => 'logs',
426-
'log_file' => $log_data->key
427-
),
428-
admin_url( 'admin.php' )
429-
);
430-
$description = sprintf(
431-
wp_kses(
432-
__( 'Last %d entries <a href="%s">Show full log</a>', 'woocommerce-services' ),
433-
array( 'a' => array( 'href' => array() ) ) ),
434-
$log_tail_line_count,
435-
esc_url( $url )
436-
);
437-
$log_tail = implode( $log_data->tail, '' );
438-
}
439-
440-
$debug_items[] = (object) array(
441-
'key' => 'wcc_debug_log_tail',
442-
'title' => __( 'Debug Log', 'woocommerce-services' ),
443-
'type' => 'textarea',
444-
'description' => $description,
445-
'readonly' => true,
446-
'value' => $log_tail
447-
);
467+
$debug_items[] = $this->add_log_view( __( 'Shipping Log', 'woocommerce-services' ), 'shipping' );
468+
$debug_items[] = $this->add_log_view( __( 'Taxes Log', 'woocommerce-services' ), 'taxes' );
469+
$debug_items[] = $this->add_log_view( __( 'Other Log', 'woocommerce-services' ) );
448470

449471
return $debug_items;
450472
}

classes/class-wc-connect-logger.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ class WC_Connect_Logger {
1212
private $is_logging_enabled = false;
1313
private $is_debug_enabled = false;
1414

15-
public function __construct( WC_Logger $logger ) {
15+
private $feature;
1616

17-
$this->logger = $logger;
17+
public function __construct( WC_Logger $logger, $feature = '' ) {
18+
19+
$this->logger = $logger;
20+
$this->feature = strtolower( $feature );
1821

1922
$this->is_logging_enabled = WC_Connect_Options::get_option( 'debug_logging_enabled', false );
2023
$this->is_debug_enabled = WC_Connect_Options::get_option( 'debug_display_enabled', false );
@@ -116,7 +119,14 @@ public function log( $message, $context = '' ) {
116119
return;
117120
}
118121

119-
$this->logger->add( 'wc-services', $log_message );
122+
$log_file = 'wc-services';
123+
124+
if ( ! empty( $this->feature ) ) {
125+
$log_file .= '-' . $this->feature;
126+
}
127+
128+
$this->logger->add( $log_file, $log_message );
129+
120130

121131
}
122132

tests/php/test_woocommerce-connect-client.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected function mockLoader( $store = false, $api_client = false, $logger = fa
2222

2323
$loader = $this->getMockBuilder( 'WC_Connect_Loader' )
2424
->disableOriginalConstructor()
25-
->setMethods( array( 'get_service_schemas_store', 'get_api_client', 'get_logger', 'get_tracks' ) )
25+
->setMethods( array( 'get_service_schemas_store', 'get_api_client', 'get_logger', 'get_shipping_logger', 'get_tracks' ) )
2626
->getMock();
2727

2828
$loader->expects( $this->any() )
@@ -49,6 +49,10 @@ protected function mockLoader( $store = false, $api_client = false, $logger = fa
4949
->method( 'get_logger' )
5050
->will( $this->returnValue( $logger ) );
5151

52+
$loader->expects( $this->any() )
53+
->method( 'get_shipping_logger' )
54+
->will( $this->returnValue( $logger ) );
55+
5256
if ( ! $tracks ) {
5357
$tracks = $this->getMockBuilder( 'WC_Connect_Tracks' )
5458
->disableOriginalConstructor()

woocommerce-services.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class WC_Connect_Loader {
5252
*/
5353
protected $logger;
5454

55+
/**
56+
* @var WC_Connect_Logger
57+
*/
58+
protected $shipping_logger;
59+
5560
/**
5661
* @var WC_Connect_API_Client
5762
*/
@@ -222,6 +227,14 @@ public function set_logger( WC_Connect_Logger $logger ) {
222227
$this->logger = $logger;
223228
}
224229

230+
public function get_shipping_logger() {
231+
return $this->shipping_logger;
232+
}
233+
234+
public function set_shipping_logger( WC_Connect_Logger $logger ) {
235+
$this->shipping_logger = $logger;
236+
}
237+
225238
public function get_api_client() {
226239
return $this->api_client;
227240
}
@@ -571,7 +584,12 @@ public function load_dependencies() {
571584
require_once( plugin_basename( 'classes/class-wc-connect-stripe.php' ) );
572585
require_once( plugin_basename( 'classes/class-wc-connect-paypal-ec.php' ) );
573586

574-
$logger = new WC_Connect_Logger( new WC_Logger() );
587+
$core_logger = new WC_Logger();
588+
$logger = new WC_Connect_Logger( $core_logger );
589+
$taxes_logger = new WC_Connect_Logger( $core_logger, 'taxes' );
590+
$payments_logger = new WC_Connect_Logger( $core_logger, 'payments' );
591+
$shipping_logger = new WC_Connect_Logger( $core_logger, 'shipping' );
592+
575593
$validator = new WC_Connect_Service_Schemas_Validator();
576594
$api_client = new WC_Connect_API_Client( $validator, $this );
577595
$schemas_store = new WC_Connect_Service_Schemas_Store( $api_client, $logger );
@@ -580,12 +598,13 @@ public function load_dependencies() {
580598
$tracks = new WC_Connect_Tracks( $logger, __FILE__ );
581599
$shipping_label = new WC_Connect_Shipping_Label( $api_client, $settings_store, $schemas_store );
582600
$nux = new WC_Connect_Nux( $tracks, $shipping_label );
583-
$taxjar = new WC_Connect_TaxJar_Integration( $api_client, $logger );
601+
$taxjar = new WC_Connect_TaxJar_Integration( $api_client, $taxes_logger );
584602
$options = new WC_Connect_Options();
585-
$stripe = new WC_Connect_Stripe( $api_client, $options, $logger );
603+
$stripe = new WC_Connect_Stripe( $api_client, $options, $payments_logger );
586604
$paypal_ec = new WC_Connect_PayPal_EC( $api_client, $nux );
587605

588606
$this->set_logger( $logger );
607+
$this->set_shipping_logger( $shipping_logger );
589608
$this->set_api_client( $api_client );
590609
$this->set_service_schemas_validator( $validator );
591610
$this->set_service_schemas_store( $schemas_store );
@@ -971,7 +990,7 @@ public function init_service( WC_Connect_Shipping_Method $method, $id_or_instanc
971990
// TODO - make more generic - allow things other than WC_Connect_Shipping_Method to work here
972991

973992
$method->set_api_client( $this->get_api_client() );
974-
$method->set_logger( $this->get_logger() );
993+
$method->set_logger( $this->get_shipping_logger() );
975994
$method->set_service_settings_store( $this->get_service_settings_store() );
976995

977996
$service_schema = $this->get_service_schemas_store()->get_service_schema_by_id_or_instance_id( $id_or_instance_id );

0 commit comments

Comments
 (0)