The WooCommerce Logger functionality is a very useful tool for developers, website builders and code dabblers. In my opinion it is not the best to replace an actual error log such as the PHP error log, but it can be very useful for logging debug data for a theme or plugin. Another use case I come across every so often is when I have no access to the PHP error log on a ‘live’ site and want to check the value of a function of variable.

Accessing WooCommerce Logs

The WooCommerce Logs are accessible under the WooCommerce > Tools > Logs page. From there on you can access all the logs that are available. From the dropdown you can select which log to view. Logs are created for each day that there’s a message logged, this to keep it organised and relatively small files, instead of an ever growing one enormous file.

The logs are stored as files (by default). They are accessible in the content/uploads/wc-logs/ directory.

Adding New Log Messages

Using the WooCommerce Logger is pretty simple since the by now ancient old version of WooCommerce 2.7. Since then the function of wc_get_logger() function is available to use and log messages. Below is the most simple way of logging a message;

wc_get_logger()->debug( 'Your log message' );

This adds a new log to the default log file – named ‘log-{YEAR-MONTH-DAY}-{HASH}’.

Log Levels Explained

The logger has different ‘levels’ build in that are used to identify the level of severity of a log. In the line of code above I used the debug() method to indicate the ‘debug’ level (least severity). Below are all the levels and methods that are available in WooCommerce.

0 Emergency: system is unusable
1 Alert: action must be taken immediately
2 Critical: critical conditions
3 Error: error conditions
4 Warning: warning conditions
5 Notice: normal but significant condition
6 Info: informational messages 
7 Debug: debug-level messages

In order of severity (highest to lowest)

wc_get_logger()->emergency( '' );
wc_get_logger()->alert( '' );
wc_get_logger()->critical( '' );
wc_get_logger()->error( '' );
wc_get_logger()->warning( '' );
wc_get_logger()->notice( '' );
wc_get_logger()->info( '' ); 
wc_get_logger()->debug( '' );

Each level has its own corresponding method to use to log message appropriately as seen in the right column.

Creating a Separate Log File

Adding messages with the functions above it will all be added to the default log file. This works for a lot of cases, but there are plenty of reasons when it makes sense to have a separate log file. For example, if something else is causing warnings/notices and logging to the file it can make it a lot more difficult to find your own messages. Or when you have a debug function for your plugin and want to specifically group together the logged information.

The following line creates a separately named log (or adds to it if it already exists) named ‘my-logs’.

wc_get_logger()->debug( 'YOUR LOG MESSAGE
', array( 'source' => 'CUSTOM LOG NAME' ) );

Custom log files will also use daily logs, so be sure to keep that in mind with your decisions.

Viewing Logs in WooCommerce

As previously already mentioned the logs are available under the WooCommerce > Tools > Logs page. There’s one big tip I wanted to give you to here as its a big limitation with the viewer. Every time you refresh the page it loads the first file in the dropdown. When this isn’t the file you need and reload the file multiple times its a big hassle and personal annoyance. Luckily there’s a quick workaround; you can add a parameter to the URL to load a file directly.

For example, my log file is called CUSTOM-LOG-NAME-2022-05-31-1eadb0817ab2069f19e6ef8b843a6926.log (seen at the top of the log file). I can add this to my URL like this: /wp-admin/admin.php?page=wc-status&tab=logs&log_file=CUSTOM-LOG-NAME-2022-05-31-1eadb0817ab2069f19e6ef8b843a6926.log to make sure it loads the same file when I refresh the page.

Jeroen Sormani

I'm a professional WordPress plugin developer on a mission to create the best plugins for my clients. I'm specialised in developing general WordPress, WooCommerce and Easy Digital Downloads plugins.

Interested in talking about a WordPress project? Get in touch!

Follow me on Twitter

One thought on “How to Use the WooCommerce Logger

Aleksandar November 25, 2022 at 7:06 pm

Thanks Jeroen!

I didn’t know about the log_file query parameter.

Using WC_Log_Handler_File::get_log_files() I now can link directly to the log files on my plugins’ support page. 🙂

Leave a Reply