From ab2e8c5335a4d6791136104c2aab34fbbeb6d6fc Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Mon, 6 May 2019 08:19:20 -0500 Subject: [PATCH 1/2] Introduce JSON logging Fixes #1847 --- .../runtime/logging/ConsoleConfig.java | 11 ++++++++ .../quarkus/runtime/logging/FormatType.java | 26 +++++++++++++++++++ .../quarkus/runtime/logging/JsonConfig.java | 16 ++++++++++++ .../runtime/logging/LoggingSetupTemplate.java | 19 +++++++++++--- docs/src/main/asciidoc/logging-guide.adoc | 24 +++++++++++++++-- 5 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 core/runtime/src/main/java/io/quarkus/runtime/logging/FormatType.java create mode 100644 core/runtime/src/main/java/io/quarkus/runtime/logging/JsonConfig.java diff --git a/core/runtime/src/main/java/io/quarkus/runtime/logging/ConsoleConfig.java b/core/runtime/src/main/java/io/quarkus/runtime/logging/ConsoleConfig.java index 553259b70d96e..9e666ca741271 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/logging/ConsoleConfig.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/logging/ConsoleConfig.java @@ -36,6 +36,17 @@ public class ConsoleConfig { @ConfigItem(defaultValue = "%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n") String format; + /** + * The type of output format to use. + */ + @ConfigItem(defaultValue = "text") + FormatType formatType; + + /** + * JSON format configuration. + */ + JsonConfig json; + /** * The console log level */ diff --git a/core/runtime/src/main/java/io/quarkus/runtime/logging/FormatType.java b/core/runtime/src/main/java/io/quarkus/runtime/logging/FormatType.java new file mode 100644 index 0000000000000..4db8088833aa8 --- /dev/null +++ b/core/runtime/src/main/java/io/quarkus/runtime/logging/FormatType.java @@ -0,0 +1,26 @@ +package io.quarkus.runtime.logging; + +/** + * The type of formatter to use. + */ +public enum FormatType { + /** + * Format in text for person-readability. + */ + TEXT, + /** + * Format in JSON for machine-readability. + */ + JSON, + ; + + public static FormatType of(String str) { + if (str.equalsIgnoreCase("text")) { + return TEXT; + } else if (str.equalsIgnoreCase("json")) { + return JSON; + } else { + throw new IllegalArgumentException("Unrecognized value: " + str); + } + } +} diff --git a/core/runtime/src/main/java/io/quarkus/runtime/logging/JsonConfig.java b/core/runtime/src/main/java/io/quarkus/runtime/logging/JsonConfig.java new file mode 100644 index 0000000000000..afc8eff18bafb --- /dev/null +++ b/core/runtime/src/main/java/io/quarkus/runtime/logging/JsonConfig.java @@ -0,0 +1,16 @@ +package io.quarkus.runtime.logging; + +import io.quarkus.runtime.annotations.ConfigGroup; +import io.quarkus.runtime.annotations.ConfigItem; + +/** + * JSON-specific logging configuration. + */ +@ConfigGroup +public class JsonConfig { + /** + * Enable logging of call details (method name, class name, etc.) into the JSON output. + */ + @ConfigItem + public boolean callDetails; +} diff --git a/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupTemplate.java b/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupTemplate.java index 5b73e09a91d01..8c895616ba81a 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupTemplate.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupTemplate.java @@ -6,6 +6,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.logging.ErrorManager; +import java.util.logging.Formatter; import java.util.logging.Handler; import java.util.logging.Level; @@ -15,6 +16,7 @@ import org.jboss.logmanager.Logger; import org.jboss.logmanager.errormanager.OnlyOnceErrorManager; import org.jboss.logmanager.formatters.ColorPatternFormatter; +import org.jboss.logmanager.formatters.JsonFormatter; import org.jboss.logmanager.formatters.PatternFormatter; import org.jboss.logmanager.handlers.ConsoleHandler; import org.jboss.logmanager.handlers.FileHandler; @@ -50,11 +52,20 @@ public void initializeLogging(LogConfig config) { } ArrayList handlers = new ArrayList<>(2); if (config.console.enable) { - final PatternFormatter formatter; - if (config.console.color && System.console() != null) { - formatter = new ColorPatternFormatter(config.console.darken, config.console.format); + final Formatter formatter; + if (config.console.formatType == FormatType.JSON) { + JsonFormatter jsonFormatter = new JsonFormatter(); + if (config.console.json.callDetails) { + jsonFormatter.setPrintDetails(true); + } + formatter = jsonFormatter; } else { - formatter = new PatternFormatter(config.console.format); + assert config.console.formatType == FormatType.TEXT; + if (config.console.color && System.console() != null) { + formatter = new ColorPatternFormatter(config.console.darken, config.console.format); + } else { + formatter = new PatternFormatter(config.console.format); + } } final ConsoleHandler handler = new ConsoleHandler(formatter); handler.setLevel(config.console.level); diff --git a/docs/src/main/asciidoc/logging-guide.adoc b/docs/src/main/asciidoc/logging-guide.adoc index a258d5f3df52a..73fd7308c2a5c 100644 --- a/docs/src/main/asciidoc/logging-guide.adoc +++ b/docs/src/main/asciidoc/logging-guide.adoc @@ -21,9 +21,10 @@ Console logging is enabled by default. To configure or disable it, the followin |=== |Property Name|Default|Description |quarkus.log.console.enable|true|Determine whether console logging is enabled. -|quarkus.log.console.format|%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n|The format pattern to use for logging to the console; see <>. +|quarkus.log.console.format|%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n|The format pattern to use for `text` logging to the console; see <>. +|quarkus.log.console.format-type|text|The type of formatter to use for the console; valid values are `text` or `json` (see <>). |quarkus.log.console.level|INFO|The minimum log level to display to the console. -|quarkus.log.console.color|true|Allow color rendering to be used on the console, if it is supported by the terminal. +|quarkus.log.console.color|true|Allow color rendering to be used on the console in `text` mode, if it is supported by the terminal. |=== === File configuration @@ -39,6 +40,17 @@ Logging to a file is also supported but not enabled by default. To configure or |quarkus.log.file.path|quarkus.log|The path of the log file. |=== +[id="json_config"] +=== JSON configuration + +When outputting in JSON format, it is possible to configure additional properties. + +[cols=" Date: Mon, 6 May 2019 09:20:03 -0500 Subject: [PATCH 2/2] Always add JSON as a dependency --- core/runtime/pom.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/runtime/pom.xml b/core/runtime/pom.xml index 52401e7cc11bb..8600c36436586 100644 --- a/core/runtime/pom.xml +++ b/core/runtime/pom.xml @@ -43,6 +43,10 @@ io.smallrye smallrye-config + + org.glassfish + javax.json + org.jboss.logging jboss-logging