-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32119 from gsmet/syslog-config
Support JSON formatter for syslog logging
- Loading branch information
Showing
10 changed files
with
250 additions
and
9 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
core/deployment/src/main/java/io/quarkus/deployment/builditem/LogSyslogFormatBuildItem.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.quarkus.deployment.builditem; | ||
|
||
import java.util.Optional; | ||
import java.util.logging.Formatter; | ||
|
||
import org.wildfly.common.Assert; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
import io.quarkus.runtime.RuntimeValue; | ||
|
||
/** | ||
* The syslog format build item. Producing this item will cause the logging subsystem to disregard its | ||
* syslog logging formatting configuration and use the formatter provided instead. If multiple formatters | ||
* are enabled at runtime, a warning message is printed and only one is used. | ||
*/ | ||
public final class LogSyslogFormatBuildItem extends MultiBuildItem { | ||
private final RuntimeValue<Optional<Formatter>> formatterValue; | ||
|
||
/** | ||
* Construct a new instance. | ||
* | ||
* @param formatterValue the optional formatter runtime value to use (must not be {@code null}) | ||
*/ | ||
public LogSyslogFormatBuildItem(final RuntimeValue<Optional<Formatter>> formatterValue) { | ||
this.formatterValue = Assert.checkNotNullParam("formatterValue", formatterValue); | ||
} | ||
|
||
/** | ||
* Get the formatter value. | ||
* | ||
* @return the formatter value | ||
*/ | ||
public RuntimeValue<Optional<Formatter>> getFormatterValue() { | ||
return formatterValue; | ||
} | ||
} |
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
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
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
72 changes: 72 additions & 0 deletions
72
...deployment/src/test/java/io/quarkus/logging/json/SyslogJsonFormatterCustomConfigTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.quarkus.logging.json; | ||
|
||
import static io.quarkus.logging.json.SyslogJsonFormatterDefaultConfigTest.getJsonFormatter; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.time.ZoneId; | ||
import java.util.logging.Level; | ||
import java.util.logging.LogRecord; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.jboss.logmanager.formatters.StructuredFormatter; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkus.logging.json.runtime.AdditionalFieldConfig; | ||
import io.quarkus.logging.json.runtime.JsonFormatter; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class SyslogJsonFormatterCustomConfigTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar.addClasses(SyslogJsonFormatterDefaultConfigTest.class)) | ||
.withConfigurationResource("application-syslog-json-formatter-custom.properties"); | ||
|
||
@Test | ||
public void jsonFormatterCustomConfigurationTest() { | ||
JsonFormatter jsonFormatter = getJsonFormatter(); | ||
assertThat(jsonFormatter.isPrettyPrint()).isTrue(); | ||
assertThat(jsonFormatter.getDateTimeFormatter().toString()) | ||
.isEqualTo("Value(DayOfMonth)' 'Text(MonthOfYear,SHORT)' 'Value(Year,4,19,EXCEEDS_PAD)"); | ||
assertThat(jsonFormatter.getDateTimeFormatter().getZone()).isEqualTo(ZoneId.of("UTC+05:00")); | ||
assertThat(jsonFormatter.getExceptionOutputType()) | ||
.isEqualTo(StructuredFormatter.ExceptionOutputType.DETAILED_AND_FORMATTED); | ||
assertThat(jsonFormatter.getRecordDelimiter()).isEqualTo("\n;"); | ||
assertThat(jsonFormatter.isPrintDetails()).isTrue(); | ||
assertThat(jsonFormatter.getExcludedKeys()).containsExactly("timestamp", "sequence"); | ||
assertThat(jsonFormatter.getAdditionalFields().size()).isEqualTo(2); | ||
assertThat(jsonFormatter.getAdditionalFields().containsKey("foo")).isTrue(); | ||
assertThat(jsonFormatter.getAdditionalFields().get("foo").type).isEqualTo(AdditionalFieldConfig.Type.INT); | ||
assertThat(jsonFormatter.getAdditionalFields().get("foo").value).isEqualTo("42"); | ||
assertThat(jsonFormatter.getAdditionalFields().containsKey("bar")).isTrue(); | ||
assertThat(jsonFormatter.getAdditionalFields().get("bar").type).isEqualTo(AdditionalFieldConfig.Type.STRING); | ||
assertThat(jsonFormatter.getAdditionalFields().get("bar").value).isEqualTo("baz"); | ||
} | ||
|
||
@Test | ||
public void jsonFormatterOutputTest() throws Exception { | ||
JsonFormatter jsonFormatter = getJsonFormatter(); | ||
String line = jsonFormatter.format(new LogRecord(Level.INFO, "Hello, World!")); | ||
|
||
JsonNode node = new ObjectMapper().readTree(line); | ||
// "level" has been renamed to HEY | ||
Assertions.assertThat(node.has("level")).isFalse(); | ||
Assertions.assertThat(node.has("HEY")).isTrue(); | ||
Assertions.assertThat(node.get("HEY").asText()).isEqualTo("INFO"); | ||
|
||
// excluded fields | ||
Assertions.assertThat(node.has("timestamp")).isFalse(); | ||
Assertions.assertThat(node.has("sequence")).isFalse(); | ||
|
||
// additional fields | ||
Assertions.assertThat(node.has("foo")).isTrue(); | ||
Assertions.assertThat(node.get("foo").asInt()).isEqualTo(42); | ||
Assertions.assertThat(node.has("bar")).isTrue(); | ||
Assertions.assertThat(node.get("bar").asText()).isEqualTo("baz"); | ||
Assertions.assertThat(node.get("message").asText()).isEqualTo("Hello, World!"); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...eployment/src/test/java/io/quarkus/logging/json/SyslogJsonFormatterDefaultConfigTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.quarkus.logging.json; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Arrays; | ||
import java.util.logging.Formatter; | ||
import java.util.logging.Handler; | ||
import java.util.logging.Level; | ||
import java.util.logging.LogManager; | ||
import java.util.logging.Logger; | ||
|
||
import org.jboss.logmanager.formatters.StructuredFormatter; | ||
import org.jboss.logmanager.handlers.SyslogHandler; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.bootstrap.logging.InitialConfigurator; | ||
import io.quarkus.bootstrap.logging.QuarkusDelayedHandler; | ||
import io.quarkus.logging.json.runtime.JsonFormatter; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class SyslogJsonFormatterDefaultConfigTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withConfigurationResource("application-syslog-json-formatter-default.properties"); | ||
|
||
@Test | ||
public void jsonFormatterDefaultConfigurationTest() { | ||
JsonFormatter jsonFormatter = getJsonFormatter(); | ||
assertThat(jsonFormatter.isPrettyPrint()).isFalse(); | ||
assertThat(jsonFormatter.getDateTimeFormatter().toString()) | ||
.isEqualTo(DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()).toString()); | ||
assertThat(jsonFormatter.getDateTimeFormatter().getZone()).isEqualTo(ZoneId.systemDefault()); | ||
assertThat(jsonFormatter.getExceptionOutputType()).isEqualTo(StructuredFormatter.ExceptionOutputType.DETAILED); | ||
assertThat(jsonFormatter.getRecordDelimiter()).isEqualTo("\n"); | ||
assertThat(jsonFormatter.isPrintDetails()).isFalse(); | ||
assertThat(jsonFormatter.getExcludedKeys()).isEmpty(); | ||
assertThat(jsonFormatter.getAdditionalFields().entrySet()).isEmpty(); | ||
} | ||
|
||
public static JsonFormatter getJsonFormatter() { | ||
LogManager logManager = LogManager.getLogManager(); | ||
assertThat(logManager).isInstanceOf(org.jboss.logmanager.LogManager.class); | ||
|
||
QuarkusDelayedHandler delayedHandler = InitialConfigurator.DELAYED_HANDLER; | ||
assertThat(Logger.getLogger("").getHandlers()).contains(delayedHandler); | ||
assertThat(delayedHandler.getLevel()).isEqualTo(Level.ALL); | ||
|
||
Handler handler = Arrays.stream(delayedHandler.getHandlers()) | ||
.filter(h -> (h instanceof SyslogHandler)) | ||
.findFirst().orElse(null); | ||
assertThat(handler).isNotNull(); | ||
assertThat(handler.getLevel()).isEqualTo(Level.WARNING); | ||
|
||
Formatter formatter = handler.getFormatter(); | ||
assertThat(formatter).isInstanceOf(JsonFormatter.class); | ||
return (JsonFormatter) formatter; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ng-json/deployment/src/test/resources/application-syslog-json-formatter-custom.properties
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
quarkus.log.level=INFO | ||
quarkus.log.syslog.enable=true | ||
quarkus.log.syslog.level=WARNING | ||
quarkus.log.syslog.json=true | ||
quarkus.log.syslog.json.pretty-print=true | ||
quarkus.log.syslog.json.date-format=d MMM uuuu | ||
quarkus.log.syslog.json.record-delimiter=\n; | ||
quarkus.log.syslog.json.zone-id=UTC+05:00 | ||
quarkus.log.syslog.json.exception-output-type=DETAILED_AND_FORMATTED | ||
quarkus.log.syslog.json.print-details=true | ||
quarkus.log.syslog.json.key-overrides=level=HEY | ||
quarkus.log.syslog.json.excluded-keys=timestamp,sequence | ||
quarkus.log.syslog.json.additional-field.foo.value=42 | ||
quarkus.log.syslog.json.additional-field.foo.type=int | ||
quarkus.log.syslog.json.additional-field.bar.value=baz | ||
quarkus.log.syslog.json.additional-field.bar.type=string |
5 changes: 5 additions & 0 deletions
5
...g-json/deployment/src/test/resources/application-syslog-json-formatter-default.properties
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
quarkus.log.level=INFO | ||
quarkus.log.syslog.enable=true | ||
quarkus.log.syslog.level=WARNING | ||
quarkus.log.syslog.format=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n | ||
quarkus.log.syslog.json=true |
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
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