In Logback, %highlight() is used to color the output. Is there any way to change the color schema for highlight()? I prefer the INFO level logs stay white instead of blue....
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>
%d{HH:mm:ss.SSS} [%t] %highlight(%-5level %logger{36} - %msg%n)
</Pattern>
</encoder>
</appender>
I just came across an example of how to do just that
As per their documentation you need to:
package code.nighma.logging.utils;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.pattern.color.ANSIConstants;
import ch.qos.logback.core.pattern.color.ForegroundCompositeConverterBase;
public class LogbackHighlightCompositeConverterEx extends ForegroundCompositeConverterBase<ILoggingEvent> {
@Override
protected String getForegroundColorCode(ILoggingEvent event) {
Level level = event.getLevel();
return switch (level.toInt()) {
case Level.ERROR_INT -> ANSIConstants.BOLD + ANSIConstants.RED_FG;
case Level.WARN_INT -> ANSIConstants.BOLD + ANSIConstants.MAGENTA_FG;
case Level.INFO_INT -> ANSIConstants.BOLD + ANSIConstants.CYAN_FG;
case Level.DEBUG_INT -> ANSIConstants.BOLD + ANSIConstants.YELLOW_FG;
case Level.TRACE_INT -> ANSIConstants.BOLD + ANSIConstants.WHITE_FG;
default -> ANSIConstants.BOLD + ANSIConstants.DEFAULT_FG;
};
}
}
and then your configuration xml will look something like this
<configuration>
<conversionRule conversionWord="highlightex" converterClass="code.nighma.logging.utils.HighlightingCompositeConverterEx"/>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%green(%d{ISO8601}) [%blue(%t)] %highlightex(%-5level) %yellow(%C{5}): %msg%n%throwable</Pattern>
</layout>
</appender>
<root level="info">
<appender-ref ref="Console"/>
</root>
<logger name="code.nighma" level="trace" additivity="false">
<appender-ref ref="Console"/>
</logger>
</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