Skip to content

Instantly share code, notes, and snippets.

@farmdawgnation
Created April 9, 2018 17:13
Show Gist options
  • Save farmdawgnation/fa4e45ec5f308995ed517846e14e5653 to your computer and use it in GitHub Desktop.
Save farmdawgnation/fa4e45ec5f308995ed517846e14e5653 to your computer and use it in GitHub Desktop.
Example using Logback in Java
/// Add this to your build.gradle file under dependencies:
// Logging dependencies
compile 'ch.qos.logback:logback-classic:1.2.3'
compile 'ch.qos.logback.contrib:logback-json-classic:0.1.5'
compile 'ch.qos.logback.contrib:logback-jackson:0.1.5'
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.5'
compile 'org.codehaus.janino:janino:3.0.8'
testCompile 'ch.qos.logback:logback-classic:1.2.3'
testCompile 'ch.qos.logback.contrib:logback-json-classic:0.1.5'
testCompile 'ch.qos.logback.contrib:logback-jackson:0.1.5'
testCompile 'com.fasterxml.jackson.core:jackson-databind:2.9.5'
testCompile 'org.codehaus.janino:janino:3.0.8'
// end logging dependencies
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Example {
public static Logger logger = LoggerFactory.getLogger(BabysitterMain.class);
public static void main(String[] args) {
logger.info("Hello, world");
}
}
<!-- this file goes in src/main/resources/logback.xml -->
<configuration>
<!-- if the system property LOCAL is defined, we'll use a human-friendly log format -->
<if condition='isDefined("LOCAL")'>
<then>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
</then>
<else>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<appendLineSeparator>true</appendLineSeparator>
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter" />
</layout>
</encoder>
</appender>
</else>
</if>
<!-- overriding the default log level per class / package -->
<!-- <logger name="com.example" level="INFO" /> -->
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment