Is it possible to change the log file location for WP_DEBUG_LOG?

The question:

I use WP_DEBUG_LOG in my development environment and have no issues with debug.log being in the wp-content directory.

Sometimes I turn on WP_DEBUG in production when I need to debug something, and I still want to use the log but would like to redirect it to something outside my web root. Is this possible using WP_DEBUG_LOG?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

It turns out that all WP_DEBUG_LOG does is:

ini_set( 'log_errors', 1 );
ini_set( 'error_log', WP_CONTENT_DIR . '/debug.log' );

So, if you want to change the log location for WP_DEBUG_LOG in a plugin or theme, webaware’s answer is best. If you just want to have it changed within wp-config.php you can replace define( 'WP_DEBUG_LOG', true ); with the above 2 lines and change the log file to wherever you want.

Method 2

It seems most of the answers here are not true anymore for WP version 5.1 and above since this change: https://make.wordpress.org/core/2019/01/23/miscellaneous-developer-focused-changes-in-5-1/

You can now define WP_DEBUG_LOG as a path in wp-config.php if you want to override the default wp-content/debug.log for example:

define( 'WP_DEBUG_LOG', 'wp-content/uploads/debug.log' );

Method 3

Yes, if you add some code to a plugin or a theme’s functions.php like so:

if (defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) {
    ini_set( 'error_log', WP_CONTENT_DIR . '/debug.txt' );
}

Edit: someone else just presented me with the need to do this, so I have dropped some code into a simple plugin they can edit; it’s available as a gist if anyone wants it.

Method 4

Times change, and so do correct answers to technical questions.

The current answer as of late 2019 is simple. When defining the WP_DEBUG_LOG “constant” in wp-config.php, you can now provide a path where you want the file to be written.

define( 'WP_DEBUG_LOG', '/tmp/wp-errors.log' );

See WordPress Support Documentation

Method 5

Looks like the WordPress code has changed since the last answer to this question was posted. The current wp_debug_mode() function related to those constants includes a test for whether WP_DEBUG_LOG equates to true or 1 — in which case it behaves as others have described it.

However, you can also set that constant to a string — your preferred file path — and the log will be output there. For example, you can set it to a path outside of your directories for publicly accessible web content. You may have to play with file permissions to get that to work.

I came looking for this answer because the WordFence security plugin is complaining about my debug log being potentially accessible in /wp-content/

if ( in_array( strtolower( (string) WP_DEBUG_LOG ), array( 'true', '1' ), true ) ) {
$log_path = WP_CONTENT_DIR . '/debug.log';
} elseif ( is_string( WP_DEBUG_LOG ) ) {
$log_path = WP_DEBUG_LOG;
} else {
$log_path = false;
}

Method 6

Afaik, you can’t change the location for the default debug file. What you can change is the location for the MU error log as well as the location for the PHP error log file.

$ds = DIRECTORY_SEPARATOR;
# DEBUG
define( 'WP_DEBUG',               true );
// file: ~/WP_CONTENT_DIR/debug.log
define( 'WP_DEBUG_LOG',           true );
define( 'WP_DEBUG_DISPLAY',       true );
define( 'SAVEQUERIES',            true );
# DEBUG: MU
define( 'DIEONDBERROR',           true );
define( 'ERRORLOGFILE',           WP_CONTENT_DIR.$ds.'logs'.$ds.'mu_error.log' );

@ini_set( 'log_errors',           'On' );
# PHP Error log location
@ini_set( 'error_log',            WP_CONTENT_DIR.$ds.'logs'.$ds.'php_error.log' );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment