Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor debug log structure #22

Open
3 tasks
bilfeldt opened this issue Feb 1, 2022 · 0 comments
Open
3 tasks

Refactor debug log structure #22

bilfeldt opened this issue Feb 1, 2022 · 0 comments
Milestone

Comments

@bilfeldt
Copy link
Member

bilfeldt commented Feb 1, 2022

Take inspiration from the plugin
WooCommerce Stripe Payment Gateway
:

Tasks:

  • Implement a dedicated class for the logger (take almost 100% inspiration from the file includes/class-wc-stripe-logger.php from the
    WooCommerce Stripe Payment Gateway
    plugin
    • Perform enabled/disabled checks inside this class instead of the places performing the logging (already implemented in example)
    • Add a filter (already implemented in example)
  • Include HTTP status codes in logging
  • Reformat the layout of logging messages

Example from plugin

2019-06-20T09:15:02+00:00 DEBUG 
====Stripe Version: 4.1.15====
====Start Log====
customers/cus_EQmIAnTrI2qAPU/sources request: Array
(
    [limit] => 100
)

====End Log====


2019-06-20T09:15:05+00:00 DEBUG 
====Stripe Version: 4.1.15====
====Start Log====
customers/cus_EQmIAnTrI2qAPU/sources request: Array
(
    [limit] => 100
)

====End Log====

includes/class-wc-stripe-logger.php

<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Log all things!
 *
 * @since 4.0.0
 * @version 4.0.0
 */
class WC_Stripe_Logger {

	public static $logger;
	const WC_LOG_FILENAME = 'woocommerce-gateway-stripe';

	/**
	 * Utilize WC logger class
	 *
	 * @since 4.0.0
	 * @version 4.0.0
	 */
	public static function log( $message, $start_time = null, $end_time = null ) {
		if ( ! class_exists( 'WC_Logger' ) ) {
			return;
		}

		if ( apply_filters( 'wc_stripe_logging', true, $message ) ) {
			if ( empty( self::$logger ) ) {
				self::$logger = wc_get_logger();
			}

			$settings = get_option( 'woocommerce_stripe_settings' );

			if ( empty( $settings ) || isset( $settings['logging'] ) && 'yes' !== $settings['logging'] ) {
				return;
			}

			if ( ! is_null( $start_time ) ) {

				$formatted_start_time = date_i18n( get_option( 'date_format' ) . ' g:ia', $start_time );
				$end_time             = is_null( $end_time ) ? current_time( 'timestamp' ) : $end_time;
				$formatted_end_time   = date_i18n( get_option( 'date_format' ) . ' g:ia', $end_time );
				$elapsed_time         = round( abs( $end_time - $start_time ) / 60, 2 );

				$log_entry  = "\n" . '====Stripe Version: ' . WC_STRIPE_VERSION . '====' . "\n";
				$log_entry .= '====Stripe Plugin API Version: ' . WC_Stripe_API::STRIPE_API_VERSION . '====' . "\n";
				$log_entry .= '====Start Log ' . $formatted_start_time . '====' . "\n" . $message . "\n";
				$log_entry .= '====End Log ' . $formatted_end_time . ' (' . $elapsed_time . ')====' . "\n\n";

			} else {
				$log_entry  = "\n" . '====Stripe Version: ' . WC_STRIPE_VERSION . '====' . "\n";
				$log_entry .= '====Stripe Plugin API Version: ' . WC_Stripe_API::STRIPE_API_VERSION . '====' . "\n";
				$log_entry .= '====Start Log====' . "\n" . $message . "\n" . '====End Log====' . "\n\n";

			}

			self::$logger->debug( $log_entry, [ 'source' => self::WC_LOG_FILENAME ] );
		}
	}
}
@bilfeldt bilfeldt added this to the v9.0.0 milestone Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant