Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing WordPress errors in STDOUT instead of debug.log?

My WordPress app is running on an ECS instance, and I also have CloudWatch set up for basic logging info.

This is all ok when everything is running fine, but when I have an error, all I see in my logs is something like

x.x.x.x - - [21/Jan/2019:08:03:00 + 0000] "POST /wp-admin/user-new.php HTTP/1.1" 400 5 ....

Which happened when I created a new user, but there was an error. Which one, I have no idea.

So I'd like to output any errors that happen in STDOUT so that I can see them in my regular logs (the twelve factor app - logging).

I was also thinking of implementing monolog, but that seems like it would take too much time, and I need something quick.

From digging in the WP core code I see that in the wp-includes/load.php I have

if ( WP_DEBUG_LOG ) {
  ini_set( 'log_errors', 1 );
  ini_set( 'error_log', WP_CONTENT_DIR . '/debug.log' );
}

So the error_log writes errors and notices in the debug.log by default.

Can I just override this with something like

ini_set( 'error_log', fwrite( STDOUT, WP_CONTENT_DIR . '/debug.log' ) );

in my plugin or in my wp-config.php? Or is there something else I need to do? In general, I'd like to avoid creating the debug.log file all-together, and just output errors to STDOUT.

EDIT

Ok, so putting the above code in wp-config.php won't work, as I get

Use of undefined constant STDOUT

Use of undefined constant WP_CONTENT_DIR

and

: fwrite() expects parameter 1 to be resource, string given in

warnings...

like image 967
dingo_d Avatar asked Oct 15 '25 18:10

dingo_d


1 Answers

So this apparently did the trick:

function overwrite_error_log() {
  ini_set( 'error_log', '/dev/stdout' ); // phpcs:ignore
}

add_action( 'init', 'overwrite_error_log', 10 );

Thanks to Sarah Pantry for providing the snippet on Facebook :)

like image 200
dingo_d Avatar answered Oct 17 '25 09:10

dingo_d