15

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?

17

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.

  • Couple years later, and I can't seem to get the file to populate with errors unless it's in the wordpress content folder. – Mike Kormendy Jan 29 '17 at 5:39
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.

  • 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 '13 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 '13 at 22:27
  • 1
    This works well if you can put the code in a plugin or theme, thanks! – jjeaton Feb 2 '13 at 23:02
  • Yes, just dump it into a simple plugin. See Writing a plugin on codex. – webaware Feb 2 '13 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; }

1

It seems the answers in here is 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 if you want to override the default wp-content/debug.log for example:

define( 'WP_DEBUG_LOG', 'wp-content/uploads/debug.log' );
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' );
  • 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 '13 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 '13 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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