Skip to content

Commit

Permalink
feat : add custom highlight to add color to logs (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
IronMine authored Oct 25, 2024
1 parent 365ee5d commit 3d49835
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ dependencies {
implementation 'ch.qos.logback:logback-core'
implementation 'ch.qos.logback:logback-classic'
implementation "info.picocli:picocli:${picocliVersion}"
implementation 'org.fusesource.jansi:jansi:2.4.0'
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml'
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/me/itzg/helpers/logger/CustomHighlight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.itzg.helpers.logger;

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 CustomHighlight extends ForegroundCompositeConverterBase<ILoggingEvent> {

@Override
protected String getForegroundColorCode(ILoggingEvent event) {
Level level = event.getLevel();
switch (level.toInt()) {
case Level.ERROR_INT:
return ANSIConstants.BOLD + ANSIConstants.RED_FG; // same as default color scheme
case Level.WARN_INT:
return ANSIConstants.YELLOW_FG;// same as default color scheme
case Level.INFO_INT:
default:
return ANSIConstants.DEFAULT_FG;
}
}

}
7 changes: 5 additions & 2 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<configuration>
<conversionRule conversionWord="customHighlight" converterClass="me.itzg.helpers.logger.CustomHighlight" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
Expand All @@ -8,10 +10,11 @@
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>[mc-image-helper] %d{HH:mm:ss.SSS} %-5level : %msg%n</pattern>
<pattern>%customHighlight([mc-image-helper] %d{HH:mm:ss.SSS} %-5level : %msg%n)</pattern>
</encoder>
</appender>
<appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>DENY</onMatch>
Expand All @@ -21,7 +24,7 @@
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>[mc-image-helper] %d{HH:mm:ss.SSS} %-5level : %msg%n</pattern>
<pattern>%customHighlight([mc-image-helper] %d{HH:mm:ss.SSS} %-5level : %msg%n)</pattern>
</encoder>
</appender>

Expand Down

0 comments on commit 3d49835

Please sign in to comment.