Skip to content

Instantly share code, notes, and snippets.

@Shengaero
Last active November 15, 2017 23:52
Show Gist options
  • Save Shengaero/239723436e1158fee434fbc2d513e879 to your computer and use it in GitHub Desktop.
Save Shengaero/239723436e1158fee434fbc2d513e879 to your computer and use it in GitHub Desktop.
Logback configuration similar in style to SimpleLog format
//...
// Add this to your build.gradle
repositories {
// Logging
compileOnly "ch.qos.logback:logback-classic:$logback_version"
}
//...
<!--
~ Place this in the following directory and name it "logback.xml"
~ For testing, you'll want to have a copy of this and name it "logback-test.xml"
~ src
~ - main
~ - resources
~ - logback.xml
~ - test
~ - resources
~ - logback-test.xml
-->
<!-- author: Kaidan Gustave -->
<configuration>
<!-- Conversions for logging. -->
<!-- Make sure to replace the "converterClass" path with the path to the file in your project. -->
<conversionRule conversionWord="simpleHL"
converterClass="path.to.SimpleHL"/>
<appender name="Simple" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!-- Pattern -->
<pattern>
%nopex[%d{HH:mm:ss}] [%simpleHL(%level)] [%logger{0}]: %simpleHL(%msg%n%ex)
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="Simple"/>
</root>
</configuration>
package path.to; // Make sure to replace this with the path to the file in your project.
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.pattern.color.ForegroundCompositeConverterBase;
import static ch.qos.logback.core.pattern.color.ANSIConstants.*;
import static ch.qos.logback.classic.Level.*;
/**
* A simple highlighter for console text using logback classic.
*
* @author Kaidan Gustave
*/
public class SimpleHL extends ForegroundCompositeConverterBase<ILoggingEvent>
{
@Override
protected String getForegroundColorCode(ILoggingEvent event)
{
switch(event.getLevel().levelInt)
{
case ERROR_INT:
return BOLD + RED_FG;
case WARN_INT:
return RED_FG;
case INFO_INT:
return GREEN_FG;
case DEBUG_INT:
return YELLOW_FG;
default:
return DEFAULT_FG;
}
}
}
package path.to // Make sure to replace this with the path to the file in your project.
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.pattern.color.ForegroundCompositeConverterBase
import ch.qos.logback.core.pattern.color.ANSIConstants.*
import ch.qos.logback.classic.Level.*
/**
* A simple highlighter for console text using logback classic.
*
* @author Kaidan Gustave
*/
class SimpleHL : ForegroundCompositeConverterBase<ILoggingEvent>()
{
override fun getForegroundColorCode(event: ILoggingEvent?) = when (event!!.level.levelInt)
{
ERROR_INT -> BOLD + RED_FG
WARN_INT -> RED_FG
INFO_INT -> GREEN_FG
DEBUG_INT -> YELLOW_FG
else -> DEFAULT_FG
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment