简体   繁体   English

在 STDOUT 而不是 debug.log 中写入 WordPress 错误?

[英]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.我的 WordPress 应用程序在 ECS 实例上运行,我还为基本日志记录信息设置了 CloudWatch。

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.... 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 ).所以我想 output STDOUT中发生的任何错误,以便我可以在我的常规日志(十二因素应用程序 - 日志记录)中看到它们。

I was also thinking of implementing monolog, but that seems like it would take too much time, and I need something quick.我也在考虑实现 monolog,但这似乎需要太多时间,我需要一些快速的东西。

From digging in the WP core code I see that in the wp-includes/load.php I have通过挖掘 WP 核心代码,我在wp-includes/load.php中看到我有

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.所以error_log默认在debug.log中写入错误和通知。

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 ?在我的插件或我的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 .一般来说,我想避免一起创建debug.log文件,并且只向STDOUT发送 output 错误。

EDIT编辑

Ok, so putting the above code in wp-config.php won't work, as I get好的,所以将上面的代码放在wp-config.php中是行不通的,因为我得到了

Use of undefined constant STDOUT使用未定义常量 STDOUT

Use of undefined constant WP_CONTENT_DIR使用未定义常量 WP_CONTENT_DIR

and

: fwrite() expects parameter 1 to be resource, string given in : fwrite() 期望参数 1 是资源,在中给出的字符串

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'); define('WP_DEBUG', true); define('WP_DEBUG_LOG', '/dev/stdout'); into wp-config.php should be enough:进入 wp-config.php 应该足够了:

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 :) 感谢Sarah Pantry在Facebook上提供摘录:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CakePHP更改error.log或debug.log文件名 - CakePHP change error.log or debug.log file name 将WP_DEBUG_LOG设置为true,debug.log中没有显示调试输出,为什么? - With WP_DEBUG_LOG set to true, no debug output shows in debug.log, why? Magento 2 — debug.log — main.DEBUG:操作“Vendor\Module\Controller\Index\Post\Interceptor”的请求验证失败 - Magento 2 — debug.log — main.DEBUG: Request validation failed for action “Vendor\Module\Controller\Index\Post\Interceptor” zend-log:将错误发送到 stderr,将其他任何内容发送到 stdout - zend-log: Send errors to stderr, anything else to stdout Yii框架:仅在Apache日志中写入错误 - Yii framework: writing errors in apache log only WordPress的WP_DEBUG不显示错误 - Wordpress WP_DEBUG not showing errors PHP 7脚本不显示错误,或将其写入日志 - PHP 7 Script not display errors, or writing them to the log Apache2 PHP docker 容器,如何将错误作为 stderr 和信息作为 stdout 记录到容器日志中? - Apache2 PHP docker container, how to log errors as stderr and info as stdout to container log? 将错误保存在数据库中,而不是cakePHP中的errors.log - Save the Errors in Database instead of errors.log in cakePHP 当debug为0时,如何使用CakePHP记录php错误? - How do you log php errors with CakePHP when debug is 0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM