I have two appenders. One of them is DB appender and the other one is SMTPAppender. When I log a string with a special marker I want to use both my custom db appender and SMTPAppender. However I don't want to define two appenders everythime as like that:
<logger name="com.MyClass" level="DEBUG" additivity="false">
<appender-ref ref="CUSTOM_DB"/>
<appender-ref ref="CUSTOM_EMAIL"/>
</logger>
I want a special reference that as like that:
<appender-group ref="CUSTOM_COMBO">
<appender-ref ref="CUSTOM_DB"/>
<appender-ref ref="CUSTOM_EMAIL"/>
</appender-group>
<logger name="com.MyClass" level="DEBUG" additivity="false">
<appender-ref ref="CUSTOM_COMBO"/>
</logger>
It should both call CUSTOM_DB and CUSTOM_EMAIL. How can I do that at Logback with slf4j?
So from https://tersesystems.com/blog/2019/05/27/application-logging-in-java-part-5/
public class CompositeAppender<E> extends UnsynchronizedAppenderBase<E> implements AppenderAttachable<E> {
protected AppenderAttachableImpl<E> aai = new AppenderAttachableImpl<E>();
@Override
protected void append(E eventObject) {
aai.appendLoopOnAppenders(eventObject);
}
// AppenderAttachable API delegates to aai in all cases
}
and
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%-5level] %logger{15} - message%n%xException{10}</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>file.log</file>
<encoder>
<pattern>%date - %message</pattern>
</encoder>
</appender>
<appender name="CONSOLE_AND_FILE" class="com.tersesystems.logback.CompositeAppender">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</appender>
<root level="TRACE">
<appender-ref ref="CONSOLE_AND_FILE"/>
</root>
</configuration>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With