Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use error_log() with WordPress?

Tags:

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.

like image 239
Michael Lynch Avatar asked Jun 20 '14 14:06

Michael Lynch


People also ask

How do I print a WordPress error log?

Printing errors 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 . Show activity on this post.

Does WordPress have an error log?

WordPress comes with a debugging system that can log any error messages displayed on your site. This can help you discover and fix problems on your website.


1 Answers

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

like image 200
Orlandster Avatar answered Oct 21 '22 23:10

Orlandster