27

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?

7 Answers 7

23

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' );
21

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.

3
  • Couple years later, and I can't seem to get the file to populate with errors unless it's in the wordpress content folder. Jan 29, 2017 at 5:39
  • @MikeKormendy - it could be a directory permissions problem. The location where you wish to write the file must be writable by the user who is the process owner for the php runner. In simple terms, make sure the new file destination has the same permissions as the wp-content folder currently has. Dec 23, 2019 at 16:54
  • Do this returns error 500 for me(
    – skywind
    Oct 4, 2021 at 10:13
5

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

4

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.

4
  • I tried adding this to my wp-config.php file and it doesn't work. Is that because wp-config.php is maybe loaded earlier than the actual place in the code where WP_DEBUG_LOG is used to define the log file?
    – jjeaton
    Feb 2, 2013 at 22:25
  • 1
    I was able to get this to work by adding it into a mu-plugin. Is there anyway for me to just make this change within wp-config? I'm assuming I'd just have to set WP_DEBUG_LOG to false and replace what it does myself?
    – jjeaton
    Feb 2, 2013 at 22:27
  • 1
    This works well if you can put the code in a plugin or theme, thanks!
    – jjeaton
    Feb 2, 2013 at 23:02
  • Yes, just dump it into a simple plugin. See Writing a plugin on codex.
    – webaware
    Feb 2, 2013 at 23:04
1

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; }

0

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' );
2
  • So I would need to turn off WP_DEBUG_LOG in order to make sure that the debug.log file is never created in my wp-content directory?
    – jjeaton
    Feb 1, 2013 at 19:59
  • 1
    You're asking quite some questions for this day and time :) I'm not in the brain condition to answer those now. Come back tomorrow and jump in chat.
    – kaiser
    Feb 1, 2013 at 20:47
0

I had this problem. What i did was add the below to the wp-config:

define('WP_DEBUG', true);
define( 'WP_DEBUG_DISPLAY', false );
define( 'WP_DEBUG_LOG', '/var/log/wp-debug.log' );

Then chown wordpress-debug.log:

chown -R www-data:www-data /var/log/wp-debug.log

So for me it was a permissions problem. Using chown to set the permissions of the file to the wordpress user/group solved it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.