• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
SnippetPress

SnippetPress

The Home of WordPress Snippets

Vultr Web Hosting
  • Cheat Sheets
  • Reviews
  • Snippets
  • Tutorials

Add an Entry to the WooCommerce Log

May 14, 2020 by SnippetPress Leave a Comment

If you’ve ever had to do any debugging on a WooCommerce site, you’ve probably come across the WooCommerce logs. These are where WooCommerce plugins log any important messages to, such as debug information.

The logs can be accessed in the WordPress admin area by going to WooCommerce->Status and clicking on Logs. Here you will be presented with a drop-down list of all the logs that are stored on your site.

Recently whilst I’ve been working on extending WooCommerce, I’ve had the need to log my own information, so that I can check what’s going on in the background. For instance, I recently needed to hook into the WooCommerce Google Analytics Pro plugin to add the product brand to the data that it sends to Google Analytics. Whilst doing this, I wanted to log the array of order data that was being sent. So in my callback function, after modifying the data array, I logged it to the WooCommerce logs, so that I could check that the modifications I made were correct.

So, how do we add our own entry to the WooCommerce logs? The good news is, WooCommerce has made it really simple!

First, we need to retrieve the logger using the function wc_get_logger(). Then we build our log entry, and finally add it. A simple example goes like this:

$logger = wc_get_logger();
	
$context = array( 'source' => 'snippetpress' );

$log = "Our log entry goes here!";
	
$logger->info( $log, $context );

The example above will generate a log entry like this:

You’ll notice that the title of our log is “snippetpress” followed by the date and a long string of letters and numbers. We can modify what the log is called by changing our $context variable $context = array( 'source' => 'snippetpress' );. So if we wanted to call the log “my-test-woocommerce-log” we’d put $context = array( 'source' => 'my-test-woocommerce-log' );. The title of the log will always be followed by the date and a random string. This is so that you know what date the log is from, and also to stop people from being able to guess your log name and access it (WooCommerce stores the logs as .txt files in /wp-content/uploads/wc-logs).

You’ll also notice that the log entry above is an INFO entry. There are 8 entry types we can use to help us organize our logs. These are:

$logger->debug( 'Detailed debug information', $context );
$logger->info( 'Interesting events', $context );
$logger->notice( 'Normal but significant events', $context );
$logger->warning( 'Exceptional occurrences that are not errors', $context );
$logger->error( 'Runtime errors that do not require immediate', $context );
$logger->critical( 'Critical conditions', $context );
$logger->alert( 'Action must be taken immediately', $context );
$logger->emergency( 'System is unusable', $context );

So to create a debug log entry, we’d use something like this:

$logger = wc_get_logger();
	
$context = array( 'source' => 'snippetpress' );

$log = "Our log entry goes here!";
	
$logger->debug( $log, $context );

To summarise, let’s have a look at a real life example. Going back to what I was saying at the start of this post about adding the product brand to the data that is sent to Google Analytics by the WooCommerce Google Analytics Pro plugin. Let’s say that I want to debug my code and see what data is being sent by the plugin. I’ll be hooking into the wc_google_analytics_pro_api_ec_purchase_parameters filter to modify the data, and then print the data to the WooCommerce log, before passing the data back to the Google Analytics plugin:

add_filter( 'wc_google_analytics_pro_api_ec_purchase_parameters', 'env_ga_order_brand', 10, 2 );
function env_ga_order_brand( $data, $order ) {
	
	// The magic to modify the data goes here
	// ...
	
	$logger = wc_get_logger();
	
	$context = array( 'source' => 'enviroli-ga-purchase' );
	
	$logger = wc_get_logger();
	
	$context = array( 'source' => 'enviroli-ga-purchase' );
	
	$log = "\n" . "==== AN ORDER WAS SENT TO GA ====" . "\n" . print_r( $params, true ) . "\n" . "====END LOG====" . "\n\n";
		
	$logger->debug( $log, $context );
	
	return $params;
	
}

Now when somebody makes a purchase and the Google Analytics plugin compiles the data to send to Google, it will also create a log entry like this:

So I can see exactly what data is being sent, and if it’s not right, modify my code accordingly.

A quick note about formatting your log entry. If you want anything on a new line in your log, you’ll need to use the newline character \n. Ensure that your string is contained in double double quotes ( " ), not single quotes ( ' ), otherwise the new line will not be rendered. So to have a log entry like this:

You’ll need to have your log entry like this:

	$logger = wc_get_logger();
		
	$context = array( 'source' => 'snippetpress' );

	$log = "Line 1\nLine 2"; // Note the double quotations ( " )
		
	$logger->info( $log, $context );

Also, if you are needing to log an array using print_r, you will need to ensure that your second (return) parameter is set to true, so that it is returned to the log, and not just printed. So you’ll need to write print_r( $array, true ). You can see this in the above Google Analytics example.

Filed Under: Tutorials, Tutorials, WooCommerce

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

E-mail Newsletter

More to See

WooCommerce – Prevent Product Being Added To Cart If A Specific Product Is Already In Cart

July 15, 2020 By SnippetPress

WooCommerce – Display an “Add $X To Get Free Shipping” Notice

July 13, 2020 By SnippetPress

Footer

Recent

  • Change The WordPress Page Title Separator Without A Plugin
  • WooCommerce – Prevent Product Being Added To Cart If A Specific Product Is Already In Cart
  • WooCommerce – Display an “Add $X To Get Free Shipping” Notice
  • WooCommerce – Integrate Google Merchant Center Customer Reviews
  • WooCommerce – Disable Out of Stock Variations

Search

Copyright © 2023 ·SnippetPress