[英]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 错误。
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.