Created
April 11, 2020 04:13
-
-
Save mike-neck/72470ae7fed5b18e7bcd2b1487e55d54 to your computer and use it in GitHub Desktop.
LogstashEncoder x MDC x Marker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"@timestamp": "2020-04-11T11:07:02.194+09:00", | |
"@version": "1", | |
"message": "example ERROR", | |
"logger_name": "com.example.demo.MdcLogstash", | |
"thread_name": "main", | |
"level": "ERROR", | |
"level_value": 40000, | |
"stack_trace": "java.lang.RuntimeException: test exception\n\tat com.example.demo.MdcLogstash.run(MdcLogstash.java:36)\n\tat com.example.demo.MdcLogstash.lambda$commandLineRunner$0(MdcLogstash.java:27)\n\tat org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795)\n\tat org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779)\n\tat org.springframework.boot.SpringApplication.run(SpringApplication.java:322)\n\tat org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140)\n\tat com.example.demo.MdcLogstash.main(MdcLogstash.java:55)\n", | |
"applicationType": "command-line", | |
"applicationId": "LOCAL_EXEC", | |
"env": "snd", | |
"tags": [ | |
"ERROR" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<included> | |
<appender name="JSON" class="ch.qos.logback.core.ConsoleAppender"> | |
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/> | |
</appender> | |
</included> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"@timestamp":"2020-04-11T11:07:01.814+09:00","@version":"1","message":"Starting MdcLogstash on mike-neck.local with PID 97450 (/Users/mike/tmp/spring-examples/mdc-logstash/build/classes/java/main started by mike in /Users/mike/tmp/spring-examples/mdc-logstash)","logger_name":"com.example.demo.MdcLogstash","thread_name":"main","level":"INFO","level_value":20000,"applicationType":"command-line","applicationId":"LOCAL_EXEC","env":"snd"} | |
{"@timestamp":"2020-04-11T11:07:01.821+09:00","@version":"1","message":"No active profile set, falling back to default profiles: default","logger_name":"com.example.demo.MdcLogstash","thread_name":"main","level":"INFO","level_value":20000,"applicationType":"command-line","applicationId":"LOCAL_EXEC","env":"snd"} | |
{"@timestamp":"2020-04-11T11:07:02.189+09:00","@version":"1","message":"Started MdcLogstash in 0.709 seconds (JVM running for 1.404)","logger_name":"com.example.demo.MdcLogstash","thread_name":"main","level":"INFO","level_value":20000,"applicationType":"command-line","applicationId":"LOCAL_EXEC","env":"snd"} | |
{"@timestamp":"2020-04-11T11:07:02.191+09:00","@version":"1","message":"test log message","logger_name":"com.example.demo.MdcLogstash","thread_name":"main","level":"INFO","level_value":20000,"applicationType":"command-line","applicationId":"LOCAL_EXEC","env":"snd"} | |
{"@timestamp":"2020-04-11T11:07:02.191+09:00","@version":"1","message":"1 + 2 = 3","logger_name":"com.example.demo.MdcLogstash","thread_name":"main","level":"INFO","level_value":20000,"applicationType":"command-line","applicationId":"LOCAL_EXEC","env":"snd","tags":["selenium"]} | |
{"@timestamp":"2020-04-11T11:07:02.194+09:00","@version":"1","message":"example failure","logger_name":"com.example.demo.MdcLogstash","thread_name":"main","level":"ERROR","level_value":40000,"stack_trace":"java.lang.RuntimeException: test exception\n\tat com.example.demo.MdcLogstash.run(MdcLogstash.java:36)\n\tat com.example.demo.MdcLogstash.lambda$commandLineRunner$0(MdcLogstash.java:27)\n\tat org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795)\n\tat org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779)\n\tat org.springframework.boot.SpringApplication.run(SpringApplication.java:322)\n\tat org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140)\n\tat com.example.demo.MdcLogstash.main(MdcLogstash.java:55)\n","applicationType":"command-line","applicationId":"LOCAL_EXEC","env":"snd"} | |
{"@timestamp":"2020-04-11T11:07:02.194+09:00","@version":"1","message":"example ERROR","logger_name":"com.example.demo.MdcLogstash","thread_name":"main","level":"ERROR","level_value":40000,"stack_trace":"java.lang.RuntimeException: test exception\n\tat com.example.demo.MdcLogstash.run(MdcLogstash.java:36)\n\tat com.example.demo.MdcLogstash.lambda$commandLineRunner$0(MdcLogstash.java:27)\n\tat org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795)\n\tat org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779)\n\tat org.springframework.boot.SpringApplication.run(SpringApplication.java:322)\n\tat org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140)\n\tat com.example.demo.MdcLogstash.main(MdcLogstash.java:55)\n","applicationType":"command-line","applicationId":"LOCAL_EXEC","env":"snd","tags":["ERROR"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" ?> | |
<configuration> | |
<include resource="org/springframework/boot/logging/logback/defaults.xml"/> | |
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/> | |
<include resource="json-logback.xml"/> | |
<springProfile name="default"> | |
<root level="INFO"> | |
<appender-ref ref="JSON"/> | |
</root> | |
</springProfile> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.slf4j.MDC; | |
import org.slf4j.helpers.BasicMarkerFactory; | |
import org.springframework.beans.factory.DisposableBean; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.boot.builder.SpringApplicationBuilder; | |
import org.springframework.context.ApplicationContextInitializer; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.support.GenericApplicationContext; | |
@SpringBootApplication | |
public class MdcLogstash { | |
private static final Logger logger = LoggerFactory.getLogger(MdcLogstash.class); | |
@Bean | |
CommandLineRunner commandLineRunner() { | |
return args -> { | |
BasicMarkerFactory factory = new BasicMarkerFactory(); | |
logger.info("test log message"); | |
logger.info(factory.getMarker("selenium"), "{} + {} = {}", 1, 2, 1 + 2); | |
try { | |
run(); | |
} catch (Exception e) { | |
logger.error("example failure", e); | |
logger.error(factory.getMarker("ERROR"), "example ERROR", e); | |
} | |
}; | |
} | |
private static void run() { | |
throw new RuntimeException("test exception"); | |
} | |
private static ApplicationContextInitializer<GenericApplicationContext> applicationContextInitializer() { | |
return applicationContext -> { | |
MDC.put("applicationType", "command-line"); | |
MDC.put("applicationId", "LOCAL_EXEC"); | |
MDC.put("env", "snd"); | |
}; | |
} | |
@Bean | |
DisposableBean disposableBean() { | |
return MDC::clear; | |
} | |
public static void main(String[] args) { | |
new SpringApplicationBuilder(MdcLogstash.class) | |
.initializers(applicationContextInitializer()) | |
.run(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment