From 348f2055bfb000633bb80e89bcf62ea037a5af0b Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sat, 15 Jan 2022 16:13:01 +0100 Subject: Add a Spring Boot example and integration code. Change-Id: Ia11dea607c74d9b4cc9a698e9ec92e930bd03f37 --- examples/quarkus/pom.xml | 44 ++++++++++ .../example/RandomNumberParameterProvider.java | 14 ++++ .../jsonlogging/example/RootResource.java | 37 +++++++++ ...lecloud.jsonlogging.StructuredParameterProvider | 1 + .../src/main/resources/application.properties | 0 examples/spring-boot/pom.xml | 93 ++++++++++++++++++++++ .../jsonlogging/example/Application.java | 12 +++ .../example/ApplicationLoggingSystem.java | 22 +++++ .../example/RandomNumberParameterProvider.java | 14 ++++ .../jsonlogging/example/RootResource.java | 33 ++++++++ ...lecloud.jsonlogging.StructuredParameterProvider | 1 + .../org.jboss.logmanager.EmbeddedConfigurator | 1 + .../src/main/resources/application.properties | 1 + .../src/main/resources/logging.properties | 2 + 14 files changed, 275 insertions(+) create mode 100644 examples/quarkus/pom.xml create mode 100644 examples/quarkus/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RandomNumberParameterProvider.java create mode 100644 examples/quarkus/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RootResource.java create mode 100644 examples/quarkus/src/main/resources/META-INF/services/eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameterProvider create mode 100644 examples/quarkus/src/main/resources/application.properties create mode 100644 examples/spring-boot/pom.xml create mode 100644 examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/Application.java create mode 100644 examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/ApplicationLoggingSystem.java create mode 100644 examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RandomNumberParameterProvider.java create mode 100644 examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RootResource.java create mode 100644 examples/spring-boot/src/main/resources/META-INF/services/eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameterProvider create mode 100644 examples/spring-boot/src/main/resources/META-INF/services/org.jboss.logmanager.EmbeddedConfigurator create mode 100644 examples/spring-boot/src/main/resources/application.properties create mode 100644 examples/spring-boot/src/main/resources/logging.properties (limited to 'examples') diff --git a/examples/quarkus/pom.xml b/examples/quarkus/pom.xml new file mode 100644 index 0000000..a26f782 --- /dev/null +++ b/examples/quarkus/pom.xml @@ -0,0 +1,44 @@ + + + + 4.0.0 + + + eu.mulk.quarkus-googlecloud-jsonlogging + quarkus-googlecloud-jsonlogging-parent + 3.1.4-SNAPSHOT + + + quarkus-googlecloud-jsonlogging-quarkus-example + Quarkus Google Cloud JSON Logging Extension - Quarkus Example + + + + io.quarkus + quarkus-resteasy + + + eu.mulk.quarkus-googlecloud-jsonlogging + quarkus-googlecloud-jsonlogging + ${project.version} + + + + + + + io.quarkus + quarkus-maven-plugin + + + + build + + + + + + + + diff --git a/examples/quarkus/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RandomNumberParameterProvider.java b/examples/quarkus/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RandomNumberParameterProvider.java new file mode 100644 index 0000000..7e4158c --- /dev/null +++ b/examples/quarkus/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RandomNumberParameterProvider.java @@ -0,0 +1,14 @@ +package eu.mulk.quarkus.googlecloud.jsonlogging.example; + +import eu.mulk.quarkus.googlecloud.jsonlogging.KeyValueParameter; +import eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameter; +import eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameterProvider; +import java.util.concurrent.ThreadLocalRandom; + +public class RandomNumberParameterProvider implements StructuredParameterProvider { + + @Override + public StructuredParameter getParameter() { + return KeyValueParameter.of("randomNumber", ThreadLocalRandom.current().nextInt()); + } +} diff --git a/examples/quarkus/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RootResource.java b/examples/quarkus/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RootResource.java new file mode 100644 index 0000000..90cd587 --- /dev/null +++ b/examples/quarkus/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RootResource.java @@ -0,0 +1,37 @@ +package eu.mulk.quarkus.googlecloud.jsonlogging.example; + +import eu.mulk.quarkus.googlecloud.jsonlogging.KeyValueParameter; +import eu.mulk.quarkus.googlecloud.jsonlogging.Label; +import javax.annotation.PostConstruct; +import javax.enterprise.context.ApplicationScoped; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import org.jboss.logging.Logger; +import org.jboss.logging.MDC; + +@Produces("text/plain") +@Path("/") +@ApplicationScoped +public class RootResource { + + static final Logger log = Logger.getLogger(RootResource.class); + + @PostConstruct + public void init() { + log.warn("Hey!"); + } + + @GET + public String hello() { + MDC.put("requestMethod", "GET"); + log.infof( + "Hello %s.", + "Mulkiatsch", + KeyValueParameter.of("a", "b"), + Label.of("app", "foo"), + KeyValueParameter.of("version", 10)); + throw new IllegalStateException(); + // return "ok"; + } +} diff --git a/examples/quarkus/src/main/resources/META-INF/services/eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameterProvider b/examples/quarkus/src/main/resources/META-INF/services/eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameterProvider new file mode 100644 index 0000000..c0017e6 --- /dev/null +++ b/examples/quarkus/src/main/resources/META-INF/services/eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameterProvider @@ -0,0 +1 @@ +eu.mulk.quarkus.googlecloud.jsonlogging.example.RandomNumberParameterProvider diff --git a/examples/quarkus/src/main/resources/application.properties b/examples/quarkus/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/examples/spring-boot/pom.xml b/examples/spring-boot/pom.xml new file mode 100644 index 0000000..29e17d3 --- /dev/null +++ b/examples/spring-boot/pom.xml @@ -0,0 +1,93 @@ + + + + 4.0.0 + + + eu.mulk.quarkus-googlecloud-jsonlogging + quarkus-googlecloud-jsonlogging-parent + 3.1.4-SNAPSHOT + + + quarkus-googlecloud-jsonlogging-spring-boot-example + Quarkus Google Cloud JSON Logging Extension - Spring Boot Example + + + + + org.springframework.boot + spring-boot-dependencies + 2.6.2 + pom + import + + + + + + + org.jboss.slf4j + slf4j-jboss-logmanager + 1.1.0.Final + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter + + + ch.qos.logback + logback-classic + + + + + + eu.mulk.quarkus-googlecloud-jsonlogging + quarkus-googlecloud-jsonlogging-core + ${project.version} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 2.6.2 + + + + + diff --git a/examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/Application.java b/examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/Application.java new file mode 100644 index 0000000..d003b3f --- /dev/null +++ b/examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/Application.java @@ -0,0 +1,12 @@ +package eu.mulk.quarkus.googlecloud.jsonlogging.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/ApplicationLoggingSystem.java b/examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/ApplicationLoggingSystem.java new file mode 100644 index 0000000..4050b99 --- /dev/null +++ b/examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/ApplicationLoggingSystem.java @@ -0,0 +1,22 @@ +package eu.mulk.quarkus.googlecloud.jsonlogging.example; + +import org.springframework.boot.logging.LogFile; +import org.springframework.boot.logging.LoggingInitializationContext; +import org.springframework.boot.logging.Slf4JLoggingSystem; + +public class ApplicationLoggingSystem extends Slf4JLoggingSystem { + + public ApplicationLoggingSystem(ClassLoader classLoader) { + super(classLoader); + } + + @Override + protected String[] getStandardConfigLocations() { + return new String[0]; + } + + @Override + protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) { + /* no configuration necessary */ + } +} diff --git a/examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RandomNumberParameterProvider.java b/examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RandomNumberParameterProvider.java new file mode 100644 index 0000000..7e4158c --- /dev/null +++ b/examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RandomNumberParameterProvider.java @@ -0,0 +1,14 @@ +package eu.mulk.quarkus.googlecloud.jsonlogging.example; + +import eu.mulk.quarkus.googlecloud.jsonlogging.KeyValueParameter; +import eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameter; +import eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameterProvider; +import java.util.concurrent.ThreadLocalRandom; + +public class RandomNumberParameterProvider implements StructuredParameterProvider { + + @Override + public StructuredParameter getParameter() { + return KeyValueParameter.of("randomNumber", ThreadLocalRandom.current().nextInt()); + } +} diff --git a/examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RootResource.java b/examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RootResource.java new file mode 100644 index 0000000..b3197fd --- /dev/null +++ b/examples/spring-boot/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/example/RootResource.java @@ -0,0 +1,33 @@ +package eu.mulk.quarkus.googlecloud.jsonlogging.example; + +import eu.mulk.quarkus.googlecloud.jsonlogging.KeyValueParameter; +import eu.mulk.quarkus.googlecloud.jsonlogging.Label; +import javax.annotation.PostConstruct; +import org.jboss.logging.Logger; +import org.jboss.logging.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController("/") +public class RootResource { + + static final Logger log = Logger.getLogger(RootResource.class); + + @PostConstruct + public void init() { + log.warn("Hey!"); + } + + @GetMapping(produces = "text/plain") + public String hello() { + MDC.put("requestMethod", "GET"); + log.infof( + "Hello %s.", + "Mulkiatsch", + KeyValueParameter.of("a", "b"), + Label.of("app", "foo"), + KeyValueParameter.of("version", 10)); + throw new IllegalStateException(); + // return "ok"; + } +} diff --git a/examples/spring-boot/src/main/resources/META-INF/services/eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameterProvider b/examples/spring-boot/src/main/resources/META-INF/services/eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameterProvider new file mode 100644 index 0000000..c0017e6 --- /dev/null +++ b/examples/spring-boot/src/main/resources/META-INF/services/eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameterProvider @@ -0,0 +1 @@ +eu.mulk.quarkus.googlecloud.jsonlogging.example.RandomNumberParameterProvider diff --git a/examples/spring-boot/src/main/resources/META-INF/services/org.jboss.logmanager.EmbeddedConfigurator b/examples/spring-boot/src/main/resources/META-INF/services/org.jboss.logmanager.EmbeddedConfigurator new file mode 100644 index 0000000..6d937e5 --- /dev/null +++ b/examples/spring-boot/src/main/resources/META-INF/services/org.jboss.logmanager.EmbeddedConfigurator @@ -0,0 +1 @@ +eu.mulk.quarkus.googlecloud.jsonlogging.logmanager.DefaultEmbeddedConfigurator diff --git a/examples/spring-boot/src/main/resources/application.properties b/examples/spring-boot/src/main/resources/application.properties new file mode 100644 index 0000000..c87d7c3 --- /dev/null +++ b/examples/spring-boot/src/main/resources/application.properties @@ -0,0 +1 @@ +logging.config = classpath:logging.properties diff --git a/examples/spring-boot/src/main/resources/logging.properties b/examples/spring-boot/src/main/resources/logging.properties new file mode 100644 index 0000000..e80e0c6 --- /dev/null +++ b/examples/spring-boot/src/main/resources/logging.properties @@ -0,0 +1,2 @@ +handlers = eu.mulk.quarkus.googlecloud.jsonlogging.logmanager.DefaultConsoleHandler +.level = INFO -- cgit v1.2.3