Apache 2:仅记录 400 个错误,而不记录其他 4XX

问题描述 投票:0回答:1

在 Apache2.4 中,我需要尽可能多地记录导致 400 错误的请求。
是否可以在不将日志记录级别设置为

debug
的情况下实现此目的?是否也可以只记录 400 个错误而不记录任何其他 4XX?

谢谢!

将日志记录级别设置为

debug
可能会起作用,但它会记录太多错误(404、403 ...),而我不需要它

apache logging http-headers http-status-code-400 http-error
1个回答
0
投票

/tmp/400errors.log
更改为您想要记录输出的位置

方法1 - SetEnvIf

在apache中使用

SetEnvIf

将此添加到

httpd.conf

# Set an environment variable for 400 series errors
SetEnvIfNoCase Response_Status "^400" LOG_400_ERRORS

# Define a custom log format that includes the error status
LogFormat "%h %l %u %t \"%r\" %>s \"%{Referer}i\" \"%{User-Agent}i\"" combined_with_status

# Use conditional logging based on the environment variable
CustomLog /tmp/400errors.log combined_with_status env=LOG_400_ERRORS

方法2 - Mod重写

在 apache 中使用

mod_rewrite
,无论是在站点配置中,还是在 htaccess 文件中

RewriteEngine On

# Redirect 400 series errors to a custom error log
RewriteCond %{REQUEST_STATUS} ^400
RewriteRule ^ - [E=LOG_400_ERRORS]

# Define a custom log format that includes the error status
LogFormat "%h %l %u %t \"%r\" %>s \"%{Referer}i\" \"%{User-Agent}i\"" combined_with_status

# Use conditional logging based on the environment variable
CustomLog /tmp/400errors.log combined_with_status env=LOG_400_ERRORS
© www.soinside.com 2019 - 2024. All rights reserved.