How can I use error_log() with WordPress?
Update 2019
Enable Debugging
You can simply enable debugging by adding the following code to your wp-config.php
:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
There will be a debug.log
file generated in your wp-content
folder.
Usage in production environment
If you want to log the errors without printing them in the frontend, add the following line:
define( 'WP_DEBUG_DISPLAY', false );
This is really useful in production environments, since the page visitor will not be able to see your logs.
Printing errors
Now you can just write to your log by using the error_log
function:
error_log( 'Hello World!' );
Or pretty print your output by making use of the print_r
method:
error_log( print_r( 'Hello World!', true ) );
Pro Tip: If you're using the bash you can observe the log with
tail -f wp-content/debug.log
According to the Codex, WP_DEBUG_DISPLAY
should be set to true by default, but it would seem that was not the case.
Adding define('WP_DEBUG_DISPLAY', true);
to wp_config.php
fixed the error logging.
Setting WP_DEBUG_DISPLAY
to false
removed the errors from the browser but allowed them to be output in the log.
It would seem that Wordpress requires define('WP_DEBUG_DISPLAY');
to output errors to the log whether you set it to be true
or false
.
I have the same problem. For me commenting out this line helped.
define( 'WP_DEBUG_LOG', true );