简体   繁体   中英

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

xxxx - - [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...

Actually code is changed, so define('WP_DEBUG', true); define('WP_DEBUG_LOG', '/dev/stdout'); define('WP_DEBUG', true); define('WP_DEBUG_LOG', '/dev/stdout'); into wp-config.php should be enough:

if ( WP_DEBUG ) {
    error_reporting( E_ALL );

    if ( WP_DEBUG_DISPLAY ) {
        ini_set( 'display_errors', 1 );
    } elseif ( null !== WP_DEBUG_DISPLAY ) {
        ini_set( 'display_errors', 0 );
    }

    if ( in_array( strtolower( (string) WP_DEBUG_LOG ), array( 'true', '1' ), true ) ) {
        $log_path = WP_CONTENT_DIR . '/debug.log';
    } elseif ( is_string( WP_DEBUG_LOG ) ) {
        $log_path = WP_DEBUG_LOG;
    } else {
        $log_path = false;
    }

    if ( $log_path ) {
        ini_set( 'log_errors', 1 );
        ini_set( 'error_log', $log_path );
    }
} else {
    error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR );
}

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM