Skip to content

Commit 4abaf42

Browse files
authored
Implemented prettyPrint (#388)
* Implemented prettyPrint
1 parent b334a03 commit 4abaf42

File tree

7 files changed

+124
-29
lines changed

7 files changed

+124
-29
lines changed

runtime/src/main/java/io/quarkiverse/loggingjson/JsonFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import java.io.IOException;
44

5+
import io.quarkiverse.loggingjson.config.Config;
6+
57
public interface JsonFactory {
8+
void setConfig(Config config);
69

710
JsonGenerator createGenerator(StringBuilderWriter writer) throws IOException;
811
}

runtime/src/main/java/io/quarkiverse/loggingjson/LoggingJsonRecorder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public RuntimeValue<Optional<Formatter>> initializeJsonLogging(ConfigFormatter f
4444
if (formatter == null || !formatter.isEnabled()) {
4545
return new RuntimeValue<>(Optional.empty());
4646
}
47+
jsonFactory.setConfig(config);
4748

4849
List<JsonProvider> providers;
4950

@@ -71,7 +72,6 @@ public RuntimeValue<Optional<Formatter>> initializeJsonLogging(ConfigFormatter f
7172
}
7273

7374
return new RuntimeValue<>(Optional.of(new JsonFormatter(providers, jsonFactory, config)));
74-
7575
}
7676

7777
private List<JsonProvider> defaultFormat(Config config) {

runtime/src/main/java/io/quarkiverse/loggingjson/jackson/JacksonJsonFactory.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@
33
import java.io.IOException;
44
import java.util.ServiceConfigurationError;
55

6+
import com.fasterxml.jackson.core.JsonFactoryBuilder;
67
import com.fasterxml.jackson.databind.ObjectMapper;
78
import com.fasterxml.jackson.databind.SerializationFeature;
89

910
import io.quarkiverse.loggingjson.JsonFactory;
1011
import io.quarkiverse.loggingjson.JsonGenerator;
1112
import io.quarkiverse.loggingjson.StringBuilderWriter;
13+
import io.quarkiverse.loggingjson.config.Config;
1214

1315
public class JacksonJsonFactory implements JsonFactory {
1416

15-
private final com.fasterxml.jackson.core.JsonFactory jsonFactory;
17+
private com.fasterxml.jackson.core.JsonFactory jsonFactory;
1618

1719
public JacksonJsonFactory() {
18-
jsonFactory = createJsonFactory();
20+
jsonFactory = createJsonFactory(false);
1921
}
2022

21-
private com.fasterxml.jackson.core.JsonFactory createJsonFactory() {
23+
private com.fasterxml.jackson.core.JsonFactory createJsonFactory(boolean prettyPrint) {
2224
ObjectMapper objectMapper = new ObjectMapper()
2325
/*
2426
* Assume empty beans are ok.
@@ -30,28 +32,25 @@ private com.fasterxml.jackson.core.JsonFactory createJsonFactory() {
3032
} catch (ServiceConfigurationError serviceConfigurationError) {
3133
// TODO Fix output of error message
3234

33-
// addError("Error occurred while dynamically loading jackson modules", serviceConfigurationError);
3435
System.err.println("Error occurred while dynamically loading jackson modules");
3536
serviceConfigurationError.printStackTrace();
3637
}
3738

38-
return objectMapper
39-
.getFactory()
40-
/*
41-
* When generators are flushed, don't flush the underlying outputStream.
42-
*
43-
* This allows some streaming optimizations when using an encoder.
44-
*
45-
* The encoder generally determines when the stream should be flushed
46-
* by an 'immediateFlush' property.
47-
*
48-
* The 'immediateFlush' property of the encoder can be set to false
49-
* when the appender performs the flushes at appropriate times
50-
* (such as the end of a batch in the AbstractLogstashTcpSocketAppender).
51-
*/
39+
final JsonFactoryBuilder builder = new JsonFactoryBuilder(objectMapper.getFactory());
40+
if (prettyPrint) {
41+
builder.addDecorator((factory, generator) -> generator.useDefaultPrettyPrinter());
42+
}
43+
return builder
44+
.build()
45+
.setCodec(objectMapper)
5246
.disable(com.fasterxml.jackson.core.JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM);
5347
}
5448

49+
@Override
50+
public void setConfig(Config config) {
51+
jsonFactory = createJsonFactory(config.prettyPrint());
52+
}
53+
5554
@Override
5655
public JsonGenerator createGenerator(StringBuilderWriter writer) throws IOException {
5756
return new JacksonJsonGenerator(jsonFactory.createGenerator(writer));

runtime/src/main/java/io/quarkiverse/loggingjson/jsonb/JsonbJsonFactory.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.quarkiverse.loggingjson.jsonb;
22

33
import java.util.HashMap;
4+
import java.util.Map;
45

56
import jakarta.json.Json;
67
import jakarta.json.stream.JsonGeneratorFactory;
@@ -11,14 +12,24 @@
1112
import io.quarkiverse.loggingjson.JsonFactory;
1213
import io.quarkiverse.loggingjson.JsonGenerator;
1314
import io.quarkiverse.loggingjson.StringBuilderWriter;
15+
import io.quarkiverse.loggingjson.config.Config;
1416

1517
public class JsonbJsonFactory implements JsonFactory {
1618

17-
private final JsonGeneratorFactory factory;
18-
private final YassonJsonb jsonb;
19+
private JsonGeneratorFactory factory;
20+
private YassonJsonb jsonb;
1921

2022
public JsonbJsonFactory() {
21-
factory = Json.createGeneratorFactory(new HashMap<>());
23+
setConfig(null);
24+
}
25+
26+
@Override
27+
public void setConfig(Config config) {
28+
Map<String, Object> jsonConfig = new HashMap<>();
29+
if (config != null && config.prettyPrint()) {
30+
jsonConfig.put(jakarta.json.stream.JsonGenerator.PRETTY_PRINTING, Boolean.TRUE);
31+
}
32+
factory = Json.createGeneratorFactory(jsonConfig);
2233
jsonb = (YassonJsonb) new JsonBindingBuilder().build();
2334
}
2435

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.quarkiverse.loggingjson;
2+
3+
class JsonFormatterJacksonTest extends JsonFormatterJsonbTest {
4+
5+
public JsonFormatterJacksonTest() {
6+
expectedPrettyPrintMessage = "{\n \"message\" : \"TestMessage\"\n}\n";
7+
}
8+
9+
@Override
10+
protected Type type() {
11+
return Type.JACKSON;
12+
}
13+
14+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.quarkiverse.loggingjson;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.logging.Level;
6+
7+
import org.jboss.logmanager.ExtLogRecord;
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.Test;
10+
11+
import io.quarkiverse.loggingjson.config.Config;
12+
import io.quarkiverse.loggingjson.providers.JsonProviderBaseTest;
13+
import io.quarkiverse.loggingjson.providers.MessageJsonProvider;
14+
15+
class JsonFormatterJsonbTest extends JsonProviderBaseTest {
16+
17+
protected String expectedPrettyPrintMessage = "{\n \"message\": \"TestMessage\"\n}\n";
18+
19+
@Override
20+
protected Type type() {
21+
return Type.JSONB;
22+
}
23+
24+
@Test
25+
void format() {
26+
final Config config = GetConfig(Map.of());
27+
28+
final JsonFormatter formatter = new JsonFormatter(List.of(new MessageJsonProvider(config.fields().message())),
29+
getJsonFactory(config), config);
30+
31+
final ExtLogRecord event = new ExtLogRecord(Level.ALL, "TestMessage", "");
32+
final String formatted = formatter.format(event);
33+
34+
Assertions.assertNotNull(formatted);
35+
Assertions.assertFalse(formatted.isEmpty());
36+
Assertions.assertEquals("{\"message\":\"TestMessage\"}\n", formatted);
37+
}
38+
39+
@Test
40+
void formatPrettyPrint() {
41+
final Config config = GetConfig(Map.of("quarkus.log.json.pretty-print", "true"));
42+
43+
final JsonFormatter formatter = new JsonFormatter(List.of(new MessageJsonProvider(config.fields().message())),
44+
getJsonFactory(config), config);
45+
46+
final ExtLogRecord event = new ExtLogRecord(Level.ALL, "TestMessage", "");
47+
final String formatted = formatter.format(event);
48+
49+
Assertions.assertNotNull(formatted);
50+
Assertions.assertFalse(formatted.isEmpty());
51+
Assertions.assertEquals(expectedPrettyPrintMessage, formatted);
52+
}
53+
}

runtime/src/test/java/io/quarkiverse/loggingjson/providers/JsonProviderBaseTest.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.quarkiverse.loggingjson.providers;
22

33
import java.io.IOException;
4+
import java.util.Map;
45
import java.util.Optional;
56

67
import org.jboss.logmanager.ExtLogRecord;
@@ -15,28 +16,42 @@
1516
import io.quarkiverse.loggingjson.config.Config;
1617
import io.quarkiverse.loggingjson.jackson.JacksonJsonFactory;
1718
import io.quarkiverse.loggingjson.jsonb.JsonbJsonFactory;
19+
import io.smallrye.config.SmallRyeConfigBuilder;
20+
import io.smallrye.config.common.MapBackedConfigSource;
1821

19-
abstract class JsonProviderBaseTest {
22+
public abstract class JsonProviderBaseTest {
2023

21-
private static final JsonFactory jsonb = new JsonbJsonFactory();
22-
private static final JsonFactory jackson = new JacksonJsonFactory();
2324
private static final ObjectMapper mapper = new ObjectMapper();
2425

2526
protected abstract Type type();
2627

27-
private JsonFactory getJsonFactory() {
28+
protected static Config GetConfig(Map<String, String> values) {
29+
return new SmallRyeConfigBuilder()
30+
.withMapping(Config.class)
31+
.withSources(new MapBackedConfigSource("test", values) {
32+
})
33+
.build()
34+
.getConfigMapping(Config.class);
35+
}
36+
37+
protected JsonFactory getJsonFactory(Config config) {
38+
JsonFactory jsonFactory;
2839
switch (type()) {
2940
case JSONB:
30-
return jsonb;
41+
jsonFactory = new JsonbJsonFactory();
42+
break;
3143
case JACKSON:
32-
return jackson;
44+
jsonFactory = new JacksonJsonFactory();
45+
break;
3346
default:
3447
throw new RuntimeException("Unsupported type");
3548
}
49+
jsonFactory.setConfig(config);
50+
return jsonFactory;
3651
}
3752

3853
protected JsonGenerator getGenerator(StringBuilderWriter writer) throws IOException {
39-
return getJsonFactory().createGenerator(writer);
54+
return getJsonFactory(GetConfig(Map.of())).createGenerator(writer);
4055
}
4156

4257
protected String getResult(JsonProvider jsonProvider, ExtLogRecord event) throws IOException {

0 commit comments

Comments
 (0)