如何禁止WordPress debug.log中的PHP Deprecated日志信息?
禁止WordPress记录PHP Deprecated日志的解决方案
一、通过WordPress钩子强制覆盖错误报告设置
WordPress加载流程中可能会重置error_reporting配置,在wp-config.php中添加钩子可强制生效:
define('WP_DEBUG', true); define('WP_DEBUG_LOG', true); define('WP_DEBUG_DISPLAY', false); // 添加钩子覆盖错误报告级别,排除Deprecated和Strict类型 add_filter('wp_error_reporting', function() { return E_ALL ^ E_DEPRECATED ^ E_STRICT; });
二、自定义日志过滤函数拦截Deprecated信息
若钩子仍不生效,可替换默认错误日志处理逻辑,直接过滤Deprecated内容:
if (WP_DEBUG && WP_DEBUG_LOG) { // 定义错误类型映射 $wp_error_types = [ E_ERROR => 'Error', E_WARNING => 'Warning', E_PARSE => 'Parse error', E_NOTICE => 'Notice', E_CORE_ERROR => 'Core Error', E_CORE_WARNING => 'Core Warning', E_COMPILE_ERROR => 'Compile Error', E_COMPILE_WARNING => 'Compile Warning', E_USER_ERROR => 'User Error', E_USER_WARNING => 'User Warning', E_USER_NOTICE => 'User Notice', E_RECOVERABLE_ERROR => 'Recoverable Error', E_DEPRECATED => 'Deprecated', E_USER_DEPRECATED => 'Deprecated' ]; // 自定义日志写入函数 function wp_custom_debug_log($message) { error_log($message, 3, WP_DEBUG_LOG); } // 替换默认错误处理函数 set_error_handler(function($errno, $errstr, $errfile, $errline) use ($wp_error_types) { // 跳过已被error_reporting排除的错误 if (!(error_reporting() & $errno)) { return true; } // 过滤掉Deprecated类型日志 if (isset($wp_error_types[$errno]) && $wp_error_types[$errno] === 'Deprecated') { return true; } // 格式化并写入非Deprecated日志 $error_type = $wp_error_types[$errno] ?? 'Unknown error'; $log_message = sprintf('PHP %s: %s in %s on line %d', $error_type, $errstr, $errfile, $errline); wp_custom_debug_log($log_message); return true; }); }
将上述代码添加到wp-config.php中WP_DEBUG相关定义之后。
三、Bitnami环境的额外配置检查
Bitnami部署的WordPress有独立PHP配置路径,需确保修改正确的配置文件:
- 编辑主PHP配置文件:
sudo nano /opt/bitnami/php/etc/php.ini - 确认配置:
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT log_errors = On error_log = /opt/bitnami/php/logs/php_errors.log - 重启PHP-FPM服务生效:
sudo /opt/bitnami/ctlscript.sh restart php-fpm
同时检查WordPress目录下的wp-config-local.php,避免其中存在覆盖error_reporting的配置。
四、应急:用logrotate限制日志文件大小
若日志增长过快导致磁盘压力,可通过logrotate自动轮转日志:
- 创建logrotate配置文件:
sudo nano /etc/logrotate.d/wordpress-debug - 添加以下内容(路径根据Bitnami实际部署调整):
/opt/bitnami/apps/wordpress/htdocs/wp-content/debug.log { daily rotate 7 size 100M compress missingok notifempty create 640 daemon daemon postrotate sudo /opt/bitnami/ctlscript.sh restart php-fpm > /dev/null 2>&1 endscript } - 手动测试轮转:
sudo logrotate -f /etc/logrotate.d/wordpress-debug
内容的提问来源于stack exchange,提问作者Martin
相关产品推荐
相关产品推荐

