【问题标题】:Logback - How to get ListAppender instanceLogback - 如何获取 ListAppender 实例
【发布时间】:2020-06-23 23:09:40
【问题描述】:

我在客户端使用 SocketAppender 和服务器中的 SimpleSocketServer 的日志记录客户端/服务器架构。

我想将所有日志存储在 Java 列表中以供以后处理。我想我可以使用 ListAppender,它有一个支持 List 集合。

我让控制台和滚动附加程序正常工作,但我不知道如何获取 ListAppender 实例以便访问日志。

客户端配置文件

<?xml version="1.0" encoding="UTF-8" ?>

<!-- ==================================================================== -->
<!-- SocketAppender configuration file.                                   -->
<!-- ==================================================================== -->

<configuration>

  <appender name="SOCKET" class="ch.qos.logback.classic.net.SocketAppender">
    <RemoteHost>127.0.0.1</RemoteHost>
    <Port>6000</Port>
    <ReconnectionDelay>5000</ReconnectionDelay>
    <IncludeCallerData>true</IncludeCallerData>
  </appender>

  <root level="debug">
    <appender-ref ref="SOCKET" />
  </root>  

</configuration>

服务器配置文件

<?xml version="1.0" encoding="UTF-8" ?>

<!-- ==================================================================== -->
<!-- This config file is intended to be used by a SocketServer that logs  -->
<!-- events received from various clients on the console and to a file    -->
<!-- that is rolled over when appropriate. The interesting point to note  -->
<!-- is that it is a configuration file like any other.                   -->   
<!-- ==================================================================== -->

<configuration>

  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">

    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%date %-5level [%thread] %logger - %message%n</Pattern>
    </layout>       

  </appender>

  <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>rolling.log</File>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>rolling.%i.log</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>3</MaxIndex>
        </rollingPolicy>

        <triggeringPolicy
            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>8KB</MaxFileSize>
        </triggeringPolicy>

    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%relative %-5level %logger - %message%n</Pattern>
    </layout>       
  </appender>

  <appender name="LIST"  class="ch.qos.logback.core.read.ListAppender"/>

  <root>
    <level value ="debug"/>
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="ROLLING" />
  </root>  
</configuration>

【问题讨论】:

    标签: logback logback-classic


    【解决方案1】:

    为了使用 ListAppender,我没有将它附加到服务器 xml 文件中的根记录器。检查下面服务器配置文件中的根标记。

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!-- ==================================================================== -->
    <!-- This config file is intended to be used by a SocketServer that logs  -->
    <!-- events received from various clients on the console and to a file    -->
    <!-- that is rolled over when appropriate. The interesting point to note  -->
    <!-- is that it is a configuration file like any other.                   -->   
    <!-- ==================================================================== -->
    
    <configuration>
    
      <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    
        <layout class="ch.qos.logback.classic.PatternLayout">
          <Pattern>%date %-5level [%thread] %logger - %message%n</Pattern>
        </layout>       
    
      </appender>
    
      <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>rolling.log</File>
    
            <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <FileNamePattern>rolling.%i.log</FileNamePattern>
                <MinIndex>1</MinIndex>
                <MaxIndex>3</MaxIndex>
            </rollingPolicy>
    
            <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <MaxFileSize>8KB</MaxFileSize>
            </triggeringPolicy>
    
        <layout class="ch.qos.logback.classic.PatternLayout">
          <Pattern>%relative %-5level %logger - %message%n</Pattern>
        </layout>       
      </appender>
    
      <appender name="LIST"  class="ch.qos.logback.core.read.ListAppender"/>
    
      <root>
        <level value ="debug"/>
    <!--    <appender-ref ref="CONSOLE" />-->
    <!--    <appender-ref ref="ROLLING" />-->
        <appender-ref ref="LIST" />
      </root>
    </configuration>
    

    之后,在 Java 中获取列表 appender 就很简单了:

    import ch.qos.logback.classic.Logger;
    import ch.qos.logback.classic.spi.ILoggingEvent;
    import ch.qos.logback.core.read.ListAppender;
    import org.slf4j.LoggerFactory;
    
    Logger rootLogger = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    ListAppender<ILoggingEvent> listAppender = (ListAppender<ILoggingEvent>) rootLogger.getAppender("LIST");
    

    【讨论】:

      猜你喜欢
      • 2018-02-20
      • 2012-11-26
      • 2019-07-08
      • 2010-10-12
      • 2019-07-08
      • 1970-01-01
      • 2023-03-03
      • 2012-10-11
      • 2015-01-19
      相关资源
      最近更新 更多