Back to Top

How To Use WooCommerce Logger?

Updated 17 April 2024

WooCommerce Logger is a feature in the WooCommerce plugin that allows you to log events that occur within your online store and this can include events such as orders, payments, and customer interactions.

If you require expert assistance or want to develop custom unique functionality, hire WooCommerce Developers for your project.

The WooCommerce Logger records these events and stores them in a log file, which can be useful for troubleshooting and debugging issues.

The WooCommerce Logger can be accessed through the WordPress admin panel by navigating to WooCommerce > Status > Logs. From there, you can view the logs and filter them by date range, log level, and log source.

Moreover, You can also download the logs as a CSV file for further analysis.

Searching for an experienced
WordPress Company ?
Find out More
webkul-woo-logs-1200x488-1

Log Levels Explained

There are different log levels provided in the logger class.

The log levels in WooCommerce Logger are:

  • Emergency: Indicates a system-critical issue that requires immediate attention.
  • Alert: Indicates a critical issue that should be addressed as soon as possible.
  • Critical: Indicates a problem that requires immediate attention.
  • Error: Indicates an error that needs to be fixed.
  • Warning: Indicates a potential issue that should be investigated.
  • Notice: Indicates important information about a process or event.
  • Info: Indicates general information about a process or event.
  • Debug: Indicates detailed information for debugging purposes.

By default, the WooCommerce Logger is set to log events with a log level of Error or higher. However, you can adjust the log level in the WooCommerce settings to include additional events or reduce the level of logging.

        $logger  = wc_get_logger();
	$context = array( 'source Log' => 'Webkul' );
	$logger->debug( 'Webkul Detailed information Log', $context );
	$logger->info( 'Webkul Information Log', $context );
	$logger->notice( 'Webkul Notice Log', $context );
	$logger->warning( 'Webkul Warning Log', $context );
	$logger->error( 'Webkul Errors', $context );
	$logger->critical( 'Webkul Critical conditions', $context );
	$logger->alert( 'Webkul Alert Log', $context );
	$logger->emergency( 'Webkul Emergency Log', $context );

	$logger->log( 'debug', 'Webkul Log Function', $context );

Log handlers

Log Handlers were introduced in WooCommerce 2.7 to alter the behaviour of the core logger.

WC_Logger delegates all the work to the log handlers by passing the log messages to each of them. The log handlers are as follows –

File

To write the logs to a file WC_Log_Handler_File can be used. The logs can be viewed on the WooCommerce admin status page or on the server directory at wp-content/uploads/wc-logs.

WooCo
mmerce File Log

Database

Use the WC_Log_Handler_DB to write the logs to the database. It writes the logs in a new aesthetic interface that is prettier to see.

Database L
og

Email

The Email Handler is also available. But we do not recommend it as Email is not a go-to option for debugging and logging purposes.

To use the Email Handler define the constant as WC_Log_Handler_Email in the wp-config.php file

How to Configure the Log Handler?

The Log handler is registered by default by WooCommerce, So you can define the constant WC_LOG_HANDLER in your wp-config.php to load the desired handler.

define( 'WC_LOG_HANDLER', 'WC_Log_Handler_DB' );

How To Register Custom WooCommerce Log Handler?

By default, WooCommerce provides only three handlers that were described earlier in this post.

But we can register custom handlers by using the filter woocommerce_register_log_handlers, which returns the array of instantiated handlers.

function add_webkul_handler( $handlers ) {
    array_push( $handlers, new Webkul_Log_Handler() );
    return $handlers;
}
add_filter( 'woocommerce_register_log_handlers', 'add_webkul_handler' );

Additionally, Your custom handler must implement the WC_Log_Handler_Interface or otherwise must extend the WC_Log_Handler class.

class Webkul_Log_Handler extends WC_Log_Handler {
    public function handle( $timestamp, $level, $message, $context ) {
        // Implement your handler here
    }
}

Support

For any technical assistance kindly raise a ticket or reach us by email at [email protected]. Thanks for Your Time! Have a Good Day!

Also, discover various solutions to add more features and enhance your online store by visiting the WooCommerce plugins.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home