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