How to Rename the WordPress Debug Log

Renaming the WordPress debug log file is super easy. You can modify it in the configuration file or your theme functions file. Here’s how.

When you turn on WordPress debugging to a log file, the default file it creates is /wp-content/debug.log .

This is probably fine for a single local development machine.

Sometimes it is necessary to turn on WordPress debugging on production servers to investigate issues.

You don’t really want that log file being exposed publicly on dev, staging and certainly not production servers.

There are a couple of ways to change the debug log file name and path.

Modify wp-config.php

When you turn WordPress log debugging using define( ‘WP_DEBUG_LOG’, true); the WordPress core runs code in the file wp-includes/load.php . Here’s the relevant code:

[pastacode lang=”php” manual=”function%20wp_debug_mode()%20%7B%0A%20%20%20%20if%20(%20WP_DEBUG%20)%20%7B%0A%20%20%20%20%20%20%20%20error_reporting(%20E_ALL%20)%3B%0A%0A%20%20%20%20%20%20%20%20if%20(%20WP_DEBUG_DISPLAY%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20ini_set(%20’display_errors’%2C%201%20)%3B%0A%20%20%20%20%20%20%20%20elseif%20(%20null%20!%3D%3D%20WP_DEBUG_DISPLAY%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20ini_set(%20’display_errors’%2C%200%20)%3B%0A%0A%20%20%20%20%20%20%20%20if%20(%20WP_DEBUG_LOG%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20ini_set(%20’log_errors’%2C%201%20)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20ini_set(%20’error_log’%2C%20WP_CONTENT_DIR%20.%20’%2Fdebug.log’%20)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20error_reporting(%20E_CORE_ERROR%20%7C%20E_CORE_WARNING%20%7C%20E_COMPILE_ERROR%20%7C%20E_ERROR%20%7C%20E_WARNING%20%7C%20E_PARSE%20%7C%20E_USER_ERROR%20%7C%20E_USER_WARNING%20%7C%20E_RECOVERABLE_ERROR%20)%3B%0A%20%20%20%20%7D%0A%20%20%20%20if%20(%20defined(%20’XMLRPC_REQUEST’%20)%20)%0A%20%20%20%20%20%20%20%20ini_set(%20’display_errors’%2C%200%20)%3B%0A%7D” message=”” highlight=”10,11,12,13″ provider=”manual”/]

You can see the debug logging code in lines 10-13.

It sets two PHP ini_set declarations.

Line 11 tells PHP to start logging to a file.

Line 12 tells PHP which file name and path to write the log to.

You can change the file name and path using the following code in your wp-config.php file:

[pastacode lang=”php” manual=”define(%20’WP_DEBUG_LOG’%2C%20false%20)%3B%0Aini_set(%20’log_errors’%2C%201%20)%3B%0Aini_set(%20’error_log’%2C%20dirname(__FILE__)%20%20.%20’%2F~%24debug.log’%20)%3B” message=”” highlight=”” provider=”manual”/]

Line 1 sets WP_DEBUB_LOG to false so that WordPress doesn’t run the core code we look at previously (setting the default log file name and path).

Line 2 tells PHP to start logging to a file (of course we need this to happen).

Line 3 allows us to specify our own file name and path.

In this example I am creating a file called ~$debug.log in the same folder as the config file, usually the WordPress root folder.

Syncing Using Dropbox

You may be asking why use ~$ at the beginning of the file?

Using multiple computers during the week, desktop and laptops, I need a way of synchronising my project files and Dropbox provides that mechanism for me.

However, I don’t want huge debug log files being synced especially when issues are likely to be machine software specific (think of different PHP versions or loaded modules).

Using the ~$ symbols at the beginning of a file tells Dropbox that it is a temporary file and not to sync it. Perfect!

Modify functions.php

Sometimes you don’t want to, or simply can’t edit the wp-config.php file.

So the only other alternative you have is to modify your theme’s (or child theme’s) functions.php[/codelt] file.

This is a neater way of changing the debug filename and file path and it means that if you move your site to another server, you don’t have to duplicate all the stuff in the configuration file. It will just work.

Add the following lines to the bottom of your functions.php file:

[pastacode lang=”php” manual=”function%20zpd_change_error_log()%20%7B%0A%20%20ini_set(%20’error_log’%2C%20ABSPATH%20.%20’%2F~%24debug.log’%20)%3B%0A%7D%0Aadd_action(%20’init’%2C%20’zpd_change_error_log’%2C%2010%20)%3B” message=”” highlight=”” provider=”manual”/]

You can go ahead and change the function name in lines 1 and 4 to something other than [codelet]zpd_change_error_log.

The constant ABSPATH is a WordPress defined constant specifying the root path of your installation.

Conclusion

And there we have it. Two ways to change the file name and path of your WordPress debug log.

Was this article helpful?
YesNo