Reputation: 1280
I'm using a logging client/server architecture with SocketAppender in the client, and SimpleSocketServer in the server.
I want to store all the logs in a Java List for later processing. I think I can use the ListAppender, which has a backing List collection.
I got the Console and Rolling appenders working, but I have no idea how to get the ListAppender instance so I can access the logs.
<?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>
Upvotes: 2
Views: 3614
Reputation: 1280
In order to use ListAppender, I was missing to attach it to the root logger in the server xml file. Check the root tag in the server config file below.
<?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>
After that, it's trivial to get the list appender in 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");
Upvotes: 3