Using WC_Logger Class for Better Debugging in WooCommerce

In this tutorial I am going to dive deep into how you can use WooCommerce logs when developing your own extension for it for example.

Of course you can always add define( 'WP_DEBUG_LOG', true ) into your wp-config.php file, then use error_log() function to add any debug information and then check the log file in wp-content/debug.log. But if there is a better way, why not use it?

Activate WooCommerce Logs

Usually WooCommerce logs are enabled by default, but just to double check you can go to WooCommerce > Debug > Logs and if you see a message like this, you need to enable logs:

how to enable logging in WooCommerce

When you activate this checkbox and hit “Save changes”, more settings will appear on this page, everything is pretty straightforward, for example you can decide where to store logs – in /uploads/wc-logs/ or in the database, how many days to keep them and also to change the minimum severinity levels (we’re going to talk about log levels below).

WooCommerce logs settings

Create Debug Messages Using WC_Logger

Let’s begin with something really simple. Just try to run the code below:

$wc_logger = wc_get_logger();
$wc_logger->debug( 'Custom log message' );
// or you can do it like wc_get_logger()->debug( 'Custom log message' );

Then you will notice that a new log appeared on the Status > Logs page:

a debug log file in WooCommerce logs

And if you open it:

Create a debug log message in WooCommerce

Change a debug message source

Now let’s talk about what “Source” is.

On the screenshots above you can see clearly, that our debug log source is plugin-rudr-simple-inventory-sync-for-woocommerce.

First of all this source was generated automatically because I didn’t provide one. And because I used debug() method inside my Simple Inventory Sync plugin, and specifically in its rudr-simple-inventory-sync-for-woocommerce.php file, the the log source was generated following this pattern plugin-{PLUGIN SLUG}. As simple as that.

The good news is that we can either provide our custom source or use one of the default WooCommerce sources, that’s how you can do it:

$wc_logger = wc_get_logger();
$wc_logger->debug( 'Custom log message', array( 'source' => 'inventory-sync' ) );

There we go, now our custom debug message appears under a new source:

How to create a debug message with a custom source in WooCommerce

Log levels

There are different log levels available for us in the WC_Logger class.

SeverinityWC_Logger method
0. Emergency$wc_logger->emergency()
1. Alert$wc_logger->alert()
2. Critical$wc_logger->critical()
3. Error$wc_logger->error()
4. Warning$wc_logger->warning()
5. Notice$wc_logger->notice()
6. Info$wc_logger->info()
7. Debug$wc_logger->debug()

And now let’s try to print every possible log message type!

$wc_logger = wc_get_logger();
$wc_logger->emergency( 'Custom emergency message' );
$wc_logger->alert( 'Custom alert message' );
$wc_logger->critical( 'Custom critical message' );
$wc_logger->error( 'Custom error message' );
$wc_logger->warning( 'Custom warning message' );
$wc_logger->notice( 'Custom notice' );
$wc_logger->info( 'Custom info message' );
$wc_logger->debug( 'Custom debug message' );

And this is what we got as a result:

Log levels in WC_Logger class

Get Log Files

The last but not the least – you can easily get all the available log filenames as an array using WC_Log_Handler_File::get_log_files().

Example:

$log_files = WC_Log_Handler_File::get_log_files();

print_r( $log_files );
/*
Array
(
    [inventory-sync-2024-05-11-e0930f2abe2090418ae706d63df70418-log] => inventory-sync-2024-05-11-e0930f2abe2090418ae706d63df70418.log
    [plugin-rudr-simple-inventory-sync-for-woocommerce-2024-05-11-962ebe40bad4004ef06cd4171353b636-log] => plugin-rudr-simple-inventory-sync-for-woocommerce-2024-05-11-962ebe40bad4004ef06cd4171353b636.log
)
*/
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X