Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce JSON logging #2348

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<groupId>io.smallrye</groupId>
<artifactId>smallrye-config</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't say I'm excited to add a dependency to javax JSON in the core.I have another question: does it work for a native executable? I would have expected the quarkus-jsonp extension to be necessary.

If so, it makes the case of a specific extension IMHO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seemed to work. But yeah maybe a specific extension is warranted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record - quarkus-jsonp only registers org.glassfish.json.JsonProviderImpl for reflection so that javax.json.spi.JsonProvider could instantiate the class.

</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
Expand Down
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