1

我正在使用客户端中的 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>
4

1 回答 1

0

为了使用 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 中获取列表附加程序是微不足道的:

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");
于 2020-03-12T14:07:39.617 回答