Skip to content

Commit

Permalink
Introduce JSON logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlloyd committed May 6, 2019
1 parent c987209 commit ab2e8c5
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -50,11 +52,20 @@ public void initializeLogging(LogConfig config) {
}
ArrayList<Handler> 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);
Expand Down
24 changes: 22 additions & 2 deletions docs/src/main/asciidoc/logging-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<format_string>>.
|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 <<format_string>>.
|quarkus.log.console.format-type|text|The type of formatter to use for the console; valid values are `text` or `json` (see <<json_config>>).
|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
Expand All @@ -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="<m,<m,<2",options="header"]
|===
|Property Name|Default|Description
|quarkus.log.console.json.call-details|false|Determine whether extra detail (class name, method name, source file, etc.) should be included in the log output.
|===

=== Logging categories

Logging is done on a per-category basis. Each category can be independently configured.
Expand Down Expand Up @@ -130,6 +142,14 @@ quarkus.log.category."io.quarkus.smallrye.jwt".level=TRACE
quarkus.log.category."io.undertow.request.security".level=TRACE
----

.JSON Console Logging With Extended Call Details
[source, properties]
----
quarkus.log.console.enable=true
quarkus.log.console.format-type=json
quarkus.log.console.json.call-details=true
----

== Supported Logging APIs

Applications and components may use any of the following APIs for logging, and the logs will be merged:
Expand Down

0 comments on commit ab2e8c5

Please sign in to comment.