How can I use error_log() with WordPress?
Asked Answered
A

4

33

I'm trying to use error_log() in a custom WordPress plugin I am building but for some reason, I can't.

When I use error_log() my site just breaks, but I'm not seeing any errors in debug.log.

I have setup debugging in my wp_config.php file like this:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);

Stranegly, if I use error_log() in my theme, the site doesn't break, but nothing is output to debug.log either.

What do I need to do to be able to use error_log() in my WordPress plugin and theme?

I'm using WordPress 3.9.1.

Argyres answered 20/6, 2014 at 14:49 Comment(0)
A
19

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.

Argyres answered 20/6, 2014 at 14:53 Comment(0)
M
26

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

Medico answered 4/4, 2019 at 12:2 Comment(0)
A
19

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.

Argyres answered 20/6, 2014 at 14:53 Comment(0)
H
2

I have the same problem. For me commenting out this line helped.

define( 'WP_DEBUG_LOG', true );
Hecate answered 18/11, 2017 at 20:37 Comment(0)
K
2

You can enable WordPress logging adding this to wp-config.php:

 // Enable WP_DEBUG mode
define( 'WP_DEBUG', true );

define( 'WP_DEBUG_LOG', true );

you can write to the log file using the error_log() function provided by PHP.

Also, you can add the following code snippet in your wp-config.php file. It's very useful for debugging your WordPress site.

function write_log($log) {
    if (true === WP_DEBUG) {
        if (is_array($log) || is_object($log)) {
            error_log(print_r($log, true));
        } else {
            error_log($log);
        }
    }
}

write_log('THIS IS THE START OF MY CUSTOM DEBUG');
//i can log data like objects
write_log($whatever_you_want_to_log);
Kurd answered 17/11, 2023 at 5:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.