“Logback 的 access 模块”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
第15行: 第15行:
 
# 将 logback-access.jar 与 logback-core.jar 复制到 $TOMCAT_HOME/lib/ 目录下
 
# 将 logback-access.jar 与 logback-core.jar 复制到 $TOMCAT_HOME/lib/ 目录下
 
# 先注释掉原有关于 access log 的配置:<syntaxhighlight lang="xml">
 
# 先注释掉原有关于 access log 的配置:<syntaxhighlight lang="xml">
 +
      <Host name="localhost"  appBase="webapps"
 +
            unpackWARs="true" autoDeploy="true">
 +
 
         <!-- Access log processes all example.
 
         <!-- Access log processes all example.
 
             Documentation at: /docs/config/valve.html
 
             Documentation at: /docs/config/valve.html
第21行: 第24行:
 
               prefix="localhost_access_log" suffix=".txt"
 
               prefix="localhost_access_log" suffix=".txt"
 
               pattern="%h %l %u %t &quot;%r&quot; %s %b" /> -->
 
               pattern="%h %l %u %t &quot;%r&quot; %s %b" /> -->
 +
 +
      </Host>
 
</syntaxhighlight>
 
</syntaxhighlight>
 
# 修改 $TOMCAT_HOME/conf/server.xml 中的 Host 元素中添加:<syntaxhighlight lang="xml">
 
# 修改 $TOMCAT_HOME/conf/server.xml 中的 Host 元素中添加:<syntaxhighlight lang="xml">
 
<Value className="ch.qos.logback.access.tomcat.LogbackValue"/>
 
<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 &quot;%r&quot; %s %b" /> -->
 +
          <Value className="ch.qos.logback.access.tomcat.LogbackValue"/>
 +
 +
      </Host>
 
</syntaxhighlight>
 
</syntaxhighlight>
 
# Logback 默认会在 $TOMCAT_HOME/conf 下查找文件 logback-access.xml<syntaxhighlight lang="xml">
 
# Logback 默认会在 $TOMCAT_HOME/conf 下查找文件 logback-access.xml<syntaxhighlight lang="xml">

2023年2月27日 (一) 15:44的版本

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 的访问日志。

  1. 将 logback-access.jar 与 logback-core.jar 复制到 $TOMCAT_HOME/lib/ 目录下
  2. 先注释掉原有关于 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 &quot;%r&quot; %s %b" /> -->
    
          </Host>
    
  3. 修改 $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 &quot;%r&quot; %s %b" /> -->
              <Value className="ch.qos.logback.access.tomcat.LogbackValue"/>
    
          </Host>
    
  4. Logback 默认会在 $TOMCAT_HOME/conf 下查找文件 logback-access.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>combined</pattern>
            </encoder>
        </appender>
    
    
        <appender-ref ref="FILE"/>
    </configuration>