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

yzckvree  于 12个月前  发布在  Apache
关注(0)|答案(1)|浏览(101)

在Apache2.4中,我需要尽可能多地记录导致400错误的请求。
是否可以在不将日志记录级别设置为debug的情况下实现这一点?是否也可以只记录400个错误,而不记录任何其他4XX?
谢谢你,谢谢
将日志记录级别设置为debug可能会起作用,但它会记录太多的错误(404,403 ...),我不需要这样做

fruv7luv

fruv7luv1#

/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 Rewrite

在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

相关问题