Jetty print all request log with Logback
一。准备Logback包放入$JETTY_HOME/lib/ext中
-rw-r--r-- 1 conversant conversant 93525 Apr 2 2014 logback-access-1.1.2.jar -rw-r--r-- 1 conversant conversant 270747 Apr 2 2014 logback-classic-1.1.2.jar -rw-r--r-- 1 conversant conversant 427729 Apr 2 2014 logback-core-1.1.2.jar
二。配置logback-access.xml并保存
<?xml version="1.0" encoding="UTF-8"?> <configuration> <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" /> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.home}/requests/%d{yyyy-MM-dd}_request.log</fileNamePattern> </rollingPolicy> <encoder> <pattern>combined</pattern> </encoder> </appender> <appender name="all_requests" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <pattern> [%t] -%fullRequest - %fullResponse</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.home}/requests/%d{yyyy-MM-dd}_all.log </fileNamePattern> </rollingPolicy> </appender> <appender-ref ref="all_requests"/> <appender-ref ref="FILE"/> </configuration>
三。加载使用logback-access.xml
例如:jetty-requestlog-logback.xml,其具体内容如下:
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <!-- =============================================================== --> <!-- Configure the Jetty Request Log --> <!-- =============================================================== --> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <!-- =========================================================== --> <!-- Configure Request Log --> <!-- =========================================================== --> <Ref refid="Handlers"> <Call name="addHandler"> <Arg> <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler"> <Set name="requestLog"> <New id="requestLogImpl" class="ch.qos.logback.access.jetty.RequestLogImpl"> <Set name="fileName">/opt/app/xxxx/server/conf/logback-access.xml</Set> </New> </Set> </New> </Arg> </Call> </Ref> </Configure>
四。配置TeeFilter,打印 all_requests
路径:webapp/web.xml,增加如下代码
<filter> <filter-name>TeeFilter</filter-name> <filter-class>ch.qos.logback.access.servlet.TeeFilter</filter-class> </filter> <filter-mapping> <filter-name>TeeFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
五。设置启动脚本
修改JETTY_ARGS 参数加载jetty-requestlog-logback.xml文件
JETTY_ARGS="lib=$JETTY_HOME/lib $APP_HOME/conf/jetty/jetty.xml $APP_HOME/conf/jetty/jetty-requestlog-logback.xml OPTIONS=jsp"
六。重启并查看全部日志
cd ${log.home}/requests 查看 ${yyyy-MM-dd}_all.log 文件,即可看到输出的全部日志
总结
该配置对于需要回去http所有请求时合适。但是对于线上的生产环境由于效率以及存储空间问题,则需要关闭此配置