“Logback 的 access 模块”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1iJ411H74S?p=31”的新页面) |
Jihongchang(讨论 | 贡献) |
||
(未显示同一用户的9个中间版本) | |||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1iJ411H74S?p=31 | https://www.bilibili.com/video/BV1iJ411H74S?p=31 | ||
+ | |||
+ | === 配置 Tomcat 使用 logback-access 记录日志 === | ||
+ | 本例使用: | ||
+ | |||
+ | * tomcat-8.5.31 | ||
+ | |||
+ | * logback-access-1.1.7.jar | ||
+ | * logback-core-1.1.7.jar | ||
+ | |||
+ | logback-access 模块与 Servlet 容器(如 Tomcat 和 Jetty)集成,以提供 HTTP 访问日志功能。 | ||
+ | |||
+ | 可以使用 logback-access 模块来替换 tomcat 的访问日志。 | ||
+ | |||
+ | # 将 logback-access.jar 与 logback-core.jar 复制到 $TOMCAT_HOME/lib/ 目录下 | ||
+ | # 先注释掉原有关于 access log 的配置:<syntaxhighlight lang="xml"> | ||
+ | <Host name="localhost" appBase="webapps" | ||
+ | unpackWARs="true" autoDeploy="true"> | ||
+ | |||
+ | <!-- Access log processes all example. | ||
+ | Documentation at: /docs/config/valve.html | ||
+ | Note: The pattern used is equivalent to using pattern="common" --> | ||
+ | <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" | ||
+ | prefix="localhost_access_log" suffix=".txt" | ||
+ | pattern="%h %l %u %t "%r" %s %b" /> --> | ||
+ | |||
+ | </Host> | ||
+ | </syntaxhighlight> | ||
+ | # 修改 $TOMCAT_HOME/conf/server.xml 中的 Host 元素中添加:<syntaxhighlight lang="xml"> | ||
+ | <Value className="ch.qos.logback.access.tomcat.LogbackValue"/> | ||
+ | </syntaxhighlight>变成:<syntaxhighlight lang="xml"> | ||
+ | <Host name="localhost" appBase="webapps" | ||
+ | unpackWARs="true" autoDeploy="true"> | ||
+ | |||
+ | <!-- SingleSignOn valve, share authentication between web applications | ||
+ | Documentation at: /docs/config/valve.html --> | ||
+ | <!-- | ||
+ | <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> | ||
+ | --> | ||
+ | |||
+ | <!-- Access log processes all example. | ||
+ | Documentation at: /docs/config/valve.html | ||
+ | Note: The pattern used is equivalent to using pattern="common" --> | ||
+ | <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" | ||
+ | prefix="localhost_access_log" suffix=".txt" | ||
+ | pattern="%h %l %u %t "%r" %s %b" /> --> | ||
+ | |||
+ | <Valve className="ch.qos.logback.access.tomcat.LogbackValve"/> | ||
+ | |||
+ | </Host> | ||
+ | </syntaxhighlight> | ||
+ | # Logback 默认会在 $TOMCAT_HOME/conf 下查找文件 logback-access.xml,所以要在 $TOMCAT_HOME/conf/ 下新建 logback-access.xml 文件并复制粘贴以下内容:<syntaxhighlight lang="xml"> | ||
+ | <?xml version="1.0" encoding="UTF-8" ?> | ||
+ | <configuration> | ||
+ | <!-- always a good activate OnConsoleStatusListener --> | ||
+ | <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/> | ||
+ | |||
+ | <property name="LOG_DIR" value="${catalina.base}/logs"/> | ||
+ | |||
+ | <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
+ | <file>${LOG_DIR}/access.log</file> | ||
+ | <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | ||
+ | <fileNamePattern>access.%d{yyyy-MM-dd}.log.zip</fileNamePattern> | ||
+ | </rollingPolicy> | ||
+ | |||
+ | <encoder> | ||
+ | <!-- 日志消息格式表达式 --> | ||
+ | <pattern>%h %l %u [%t] "%r" %s %b "%i{Referer}</pattern> | ||
+ | <!-- 访问日志的格式 --> | ||
+ | <!-- <pattern>combined</pattern> --> | ||
+ | </encoder> | ||
+ | </appender> | ||
+ | |||
+ | <appender-ref ref="FILE"/> | ||
+ | </configuration> | ||
+ | </syntaxhighlight>The so called "combined" format is also widely recognized. It is defined as the '%h %l %u [%t] "%r" %s %b "%i{Referer}" "%i{User-Agent}"' pattern. As a facilitator, you can use the "combined" as a shorthand. Thus, the following directive<syntaxhighlight lang="xml"> | ||
+ | <encoder> | ||
+ | <pattern>%h %l %u [%t] "%r" %s %b "%i{Referer}" "%i{User-Agent}"</pattern> | ||
+ | </encoder> | ||
+ | </syntaxhighlight>is equivalent to:<syntaxhighlight lang="xml"> | ||
+ | <encoder> | ||
+ | <pattern>combined</pattern> | ||
+ | </encoder> | ||
+ | </syntaxhighlight> | ||
+ | # 官方配置说明:https://logback.qos.ch/access.html#configuration |
2023年2月27日 (一) 17:27的最新版本
https://www.bilibili.com/video/BV1iJ411H74S?p=31
配置 Tomcat 使用 logback-access 记录日志
本例使用:
- tomcat-8.5.31
- logback-access-1.1.7.jar
- logback-core-1.1.7.jar
logback-access 模块与 Servlet 容器(如 Tomcat 和 Jetty)集成,以提供 HTTP 访问日志功能。
可以使用 logback-access 模块来替换 tomcat 的访问日志。
- 将 logback-access.jar 与 logback-core.jar 复制到 $TOMCAT_HOME/lib/ 目录下
- 先注释掉原有关于 access log 的配置:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> --> </Host>
- 修改 $TOMCAT_HOME/conf/server.xml 中的 Host 元素中添加:变成:
<Value className="ch.qos.logback.access.tomcat.LogbackValue"/>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> --> <Valve className="ch.qos.logback.access.tomcat.LogbackValve"/> </Host>
- Logback 默认会在 $TOMCAT_HOME/conf 下查找文件 logback-access.xml,所以要在 $TOMCAT_HOME/conf/ 下新建 logback-access.xml 文件并复制粘贴以下内容:The so called "combined" format is also widely recognized. It is defined as the '%h %l %u [%t] "%r" %s %b "%i{Referer}" "%i{User-Agent}"' pattern. As a facilitator, you can use the "combined" as a shorthand. Thus, the following directive
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <!-- always a good activate OnConsoleStatusListener --> <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/> <property name="LOG_DIR" value="${catalina.base}/logs"/> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_DIR}/access.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>access.%d{yyyy-MM-dd}.log.zip</fileNamePattern> </rollingPolicy> <encoder> <!-- 日志消息格式表达式 --> <pattern>%h %l %u [%t] "%r" %s %b "%i{Referer}</pattern> <!-- 访问日志的格式 --> <!-- <pattern>combined</pattern> --> </encoder> </appender> <appender-ref ref="FILE"/> </configuration>
is equivalent to:<encoder> <pattern>%h %l %u [%t] "%r" %s %b "%i{Referer}" "%i{User-Agent}"</pattern> </encoder>
<encoder> <pattern>combined</pattern> </encoder>
- 官方配置说明:https://logback.qos.ch/access.html#configuration