How to setup a PHP Error log for WordPress – Revisited
There are a few hosts that do not give you an easy ability to see php errors that might happen. The first step is to edit your wp-config.php in your installation root directory.
Add these lines:
ini_set( 'display_errors', 0 ); ini_set( 'log_errors', 1 ); ini_set( 'error_log’, WP_CONTENT_DIR . '/debug.log' );
Although the use of WP_CONTENT_DIR doesn’t work for me, I did this instead.
ini_set( 'error_log', dirname(__FILE__) . '/wp-content/debug.log' );
Also, since I don’t want all those silly notices and warning clogging up my debug.log file, I just want errors only, I do this:
ini_set( 'error_reporting', E_ALL ^ E_NOTICE );
This line basically tells the php parser to report all errors, except notices. If more plugin programmers would test their plugins with the notices on then I wouldnt have 3 pages of notices come up inside the debug.log file.
So in summary:
ini_set( 'display_errors', 0 ); ini_set( 'log_errors', 1 ); ini_set( 'error_log', dirname(__FILE__) . '/wp-content/debug.log' ); ini_set( 'error_reporting', E_ALL ^ E_NOTICE );
Based on the comments section, this is a more proper route to go then actually using the defines that were written into the code, dunno why but I take people who know the wordpress core code better then me at their word. Thanks guys for great comments.
——–
Then if you navigate to your server’s wp-content directory there will be a debug.log waiting for you with information in it about what is wrong with your wordpress if an error ever happens.
/wp-content/debug.log
PHP.Net Manual on ini_set: http://www.php.net/ini_set
– Phil (Frumph)
Discussion (13) ¬
Thanks for the ideas. I’m not a WordPress expert by any means, but I wonder if this is the best way of doing it. Any changes to wp-settings.php will get lost during an upgrade, and also defining E_DEPRECATED (or redefining in the case of PHP 5.3 or newer) to have the same value as E_ERROR (1) is asking for trouble.
the ~ before the E_DEPRECATED means to ‘not include’ in the error reporting
I know that, but it only works if it’s correctly defined as the value 8192:
http://www.php.net/manual/en/errorfunc.constants.php
It doesn’t work in your case because you redefined E_DEPRECATED to be 1, which is the same value as E_ERROR. So you are doing a bitwise AND (&) with ~1, which results in hiding all fatal runtime errors, rather than hiding deprecation warnings. That’s definitely a bad idea!
Well thats the thing, it does work on my machine, it’s currently creating a debug.log with *only* php errors inside of it.
Also, WP doesn’t currently let you hide E_DEPRECATED warnings in a clean manner, so I reopened this ticket with a new proposal to solve it:
http://core.trac.wordpress.org/ticket/11987
E_DEPRECATED is a PHP constant, new in PHP 5.3, when PHP introduced deprecated warnings. If it exists, we exclude E_DEPRECATED warnings, as WordPress does a few things that is deprecated in 5.3 to ensure PHP 4 compatibility.
(E_STRICT is excluded from E_ALL, but in PHP 6, it is included. Hence why we also include that if PHP 5.3 or greater.)
WP_DEBUG_DISPLAY == false does not hide any errors, it simply doesn’t force them to be displayed (and thus it falls back to the server setting). They are forced on when WP_DEBUG is true.
The inline documentation for this was confusing and in some cases incorrect, so I wrote up new documentation a few days ago.
If you *don’t* want errors displayed, as the example shows, then do this:
define(‘WP_DEBUG’, true); // Turn on WP debug mode
define(‘WP_DEBUG_DISPLAY’, false); // WP won’t force-display them
define(‘WP_DEBUG_LOG’, true); // Log them to wp-content/debug.log
ini_set(‘display_errors’, 0 ); // Set the global config display_errors to off.
Hope that all makes sense.
For those interested in Windows Servers, this code (Nacin’s suggestion) doesn’t work for me on a self-hosted Windows 2008 Server, WordPress 3.5.1, PHP5.3.13 but Phil’s suggested code does work. I also cannot get the PHP directive to log errors to Windows Event Viewer to work. Will be interested to see what works on a later version of PHP 5.4.x or 5.5.x..if I ever get one of those version working.
I also noticed you suggested a core hack. I’d suggest instead forgoing WP_DEBUG all together — the whole point there is to get E_NOTICEs, which includes WordPress-specific deprecated warnings and debug material.
Instead, just let WordPress set its standard non-debug error_reporting level and do exactly what WordPress would do to turn on a log:
ini_set( ‘display_errors’, 0 );
ini_set( ‘log_errors’, 1 );
ini_set( ‘error_log’, WP_CONTENT_DIR . ‘/debug.log’ );
Hope that helps!
In my shared host, all the WP errors go to file errors.log without any hack to config.php file. But I think this post is nice for some situation, thank you.
None of these suggestions work for me. I tried different permutations of this in my wp-config.php file, and I’m still not getting an error log. Any suggestions?
Your server’s hosting might have things disabled so tight that you can’t do it with those settings.