Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.quarkiverse.loggingjson.LoggingJsonRecorder;
import io.quarkiverse.loggingjson.jackson.JacksonJsonFactory;
import io.quarkiverse.loggingjson.jsonb.JsonbJsonFactory;
import io.quarkiverse.loggingjson.providers.ObjectMapperProvider;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
Expand All @@ -33,26 +34,31 @@ FeatureBuildItem feature() {
@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
LogConsoleFormatBuildItem setUpConsoleFormatter(Capabilities capabilities, LoggingJsonRecorder recorder) {
return new LogConsoleFormatBuildItem(recorder.initializeConsoleJsonLogging(jsonFactory(capabilities)));
return new LogConsoleFormatBuildItem(recorder.initializeConsoleJsonLogging(resolveJsonFactoryType(capabilities)));
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
LogFileFormatBuildItem setUpFileFormatter(Capabilities capabilities, LoggingJsonRecorder recorder) {
return new LogFileFormatBuildItem(recorder.initializeFileJsonLogging(jsonFactory(capabilities)));
return new LogFileFormatBuildItem(recorder.initializeFileJsonLogging(resolveJsonFactoryType(capabilities)));
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
LogSocketFormatBuildItem setUpSocketFormatter(Capabilities capabilities, LoggingJsonRecorder recorder) {
return new LogSocketFormatBuildItem(recorder.initializeSocketJsonLogging(jsonFactory(capabilities)));
return new LogSocketFormatBuildItem(recorder.initializeSocketJsonLogging(resolveJsonFactoryType(capabilities)));
}

private JsonFactory jsonFactory(Capabilities capabilities) {
@BuildStep
AdditionalBeanBuildItem registerObjectMapperProvider() {
return AdditionalBeanBuildItem.unremovableOf(ObjectMapperProvider.class);
}

private Class<? extends JsonFactory> resolveJsonFactoryType(Capabilities capabilities) {
if (capabilities.isPresent(Capability.JACKSON)) {
return new JacksonJsonFactory();
return JacksonJsonFactory.class;
} else if (capabilities.isPresent(Capability.JSONB)) {
return new JsonbJsonFactory();
return JsonbJsonFactory.class;
} else {
throw new RuntimeException(
"Missing json implementation to use for logging-json. Supported: [quarkus-jackson, quarkus-jsonb]");
Expand Down
4 changes: 4 additions & 0 deletions runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<artifactId>quarkus-jsonb</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
</dependency>
Comment on lines +33 to +36
Copy link
Member

Choose a reason for hiding this comment

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

Why are you enforcing Yasson now? It doesn't look like an improvement?

Copy link
Contributor Author

@IvanPuntev IvanPuntev Nov 21, 2025

Choose a reason for hiding this comment

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

The jsonb factory initialization is now moved from the processor to the recorder and it is instantiated only on demand. The dependency is added because the native build starts to break without it.

Error: Discovered unresolved type during parsing: org.eclipse.yasson.internal.JsonBindingBuilder. This error is reported at image build time because class io.quarkiverse.loggingjson.jsonb.JsonbJsonFactory is registered for linking at image build time by command line and command line.

This solution appears to be working but if there is a better / correct way to solve this I will change it.


<dependency>
<groupId>io.quarkus</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.quarkiverse.loggingjson;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.function.Supplier;
import java.util.logging.Formatter;
import java.util.stream.Collectors;

Expand All @@ -11,6 +10,8 @@

import io.quarkiverse.loggingjson.config.Config;
import io.quarkiverse.loggingjson.config.ConfigFormatter;
import io.quarkiverse.loggingjson.jackson.JacksonJsonFactory;
import io.quarkiverse.loggingjson.jsonb.JsonbJsonFactory;
import io.quarkiverse.loggingjson.providers.*;
import io.quarkus.arc.Arc;
import io.quarkus.arc.InjectableInstance;
Expand All @@ -20,30 +21,33 @@
@Recorder
public class LoggingJsonRecorder {
private static final Logger log = LoggerFactory.getLogger(LoggingJsonRecorder.class);

private static final Map<Class<? extends JsonFactory>, Supplier<JsonFactory>> jsonFactories = Map.of(
JacksonJsonFactory.class, LoggingJsonRecorder::initializeJacksonJsonFactory,
JsonbJsonFactory.class, JsonbJsonFactory::new);
private final RuntimeValue<Config> config;

public LoggingJsonRecorder(RuntimeValue<Config> config) {
this.config = config;
}

public RuntimeValue<Optional<Formatter>> initializeConsoleJsonLogging(JsonFactory jsonFactory) {
return initializeJsonLogging(config.getValue().console(), config.getValue(), jsonFactory);
public RuntimeValue<Optional<Formatter>> initializeConsoleJsonLogging(Class<? extends JsonFactory> jsonFactoryClass) {
return initializeJsonLogging(config.getValue().console(), config.getValue(), jsonFactoryClass);
}

public RuntimeValue<Optional<Formatter>> initializeFileJsonLogging(JsonFactory jsonFactory) {
return initializeJsonLogging(config.getValue().file(), config.getValue(), jsonFactory);
public RuntimeValue<Optional<Formatter>> initializeFileJsonLogging(Class<? extends JsonFactory> jsonFactoryClass) {
return initializeJsonLogging(config.getValue().file(), config.getValue(), jsonFactoryClass);
}

public RuntimeValue<Optional<Formatter>> initializeSocketJsonLogging(JsonFactory jsonFactory) {
return initializeJsonLogging(config.getValue().socket(), config.getValue(), jsonFactory);
public RuntimeValue<Optional<Formatter>> initializeSocketJsonLogging(Class<? extends JsonFactory> jsonFactoryClass) {
return initializeJsonLogging(config.getValue().socket(), config.getValue(), jsonFactoryClass);
}

public RuntimeValue<Optional<Formatter>> initializeJsonLogging(ConfigFormatter formatter, Config config,
JsonFactory jsonFactory) {
Class<? extends JsonFactory> jsonFactoryClass) {
if (formatter == null || !formatter.isEnabled()) {
return new RuntimeValue<>(Optional.empty());
}
JsonFactory jsonFactory = getJsonFactory(jsonFactoryClass);
jsonFactory.setConfig(config);

List<JsonProvider> providers;
Expand Down Expand Up @@ -74,6 +78,23 @@ public RuntimeValue<Optional<Formatter>> initializeJsonLogging(ConfigFormatter f
return new RuntimeValue<>(Optional.of(new JsonFormatter(providers, jsonFactory, config)));
}

private JsonFactory getJsonFactory(Class<? extends JsonFactory> jsonFactoryType) {
Supplier<JsonFactory> jsonFactorySupplier = jsonFactories.get(jsonFactoryType);
if (jsonFactorySupplier == null) {
throw new IllegalArgumentException("Unsupported json factory type: " + jsonFactoryType);
}
return jsonFactorySupplier.get();
}

private static JacksonJsonFactory initializeJacksonJsonFactory() {
ObjectMapperProvider objectMapperProvider = Arc.container().instance(ObjectMapperProvider.class).orElse(null);
if (objectMapperProvider != null) {
return new JacksonJsonFactory(objectMapperProvider.get());
}
log.warn("ObjectMapperProvider not found, falling back to default ObjectMapper.");
return new JacksonJsonFactory();
}

private List<JsonProvider> defaultFormat(Config config) {
List<JsonProvider> providers = new ArrayList<>();
providers.add(new TimestampJsonProvider(config.fields().timestamp()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.io.IOException;
import java.util.ServiceConfigurationError;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonFactoryBuilder;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
Expand All @@ -13,29 +16,32 @@
import io.quarkiverse.loggingjson.config.Config;

public class JacksonJsonFactory implements JsonFactory {

private static final Logger log = LoggerFactory.getLogger(JacksonJsonFactory.class);
private final ObjectMapper objectMapper;
private com.fasterxml.jackson.core.JsonFactory jsonFactory;

public JacksonJsonFactory() {
jsonFactory = createJsonFactory(false);
public JacksonJsonFactory(ObjectMapper objectMapper) {
this.objectMapper = objectMapper == null ? initializeObjectMapper() : objectMapper; //just in case
this.jsonFactory = createJsonFactory(false);
}

private com.fasterxml.jackson.core.JsonFactory createJsonFactory(boolean prettyPrint) {
ObjectMapper objectMapper = new ObjectMapper()
/*
* Assume empty beans are ok.
*/
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
public JacksonJsonFactory() {
this.objectMapper = initializeObjectMapper();
this.jsonFactory = createJsonFactory(false);
}

private ObjectMapper initializeObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
try {
objectMapper.findAndRegisterModules();
} catch (ServiceConfigurationError serviceConfigurationError) {
// TODO Fix output of error message

System.err.println("Error occurred while dynamically loading jackson modules");
serviceConfigurationError.printStackTrace();
log.error("Error occurred while dynamically loading jackson modules.", serviceConfigurationError);
}
return objectMapper;
}

private com.fasterxml.jackson.core.JsonFactory createJsonFactory(boolean prettyPrint) {
final JsonFactoryBuilder builder = new JsonFactoryBuilder(objectMapper.getFactory());
if (prettyPrint) {
builder.addDecorator((factory, generator) -> generator.useDefaultPrettyPrinter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkiverse.loggingjson.providers;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import com.fasterxml.jackson.databind.ObjectMapper;

@Singleton
public class ObjectMapperProvider {

@Inject
ObjectMapper mapper;

public ObjectMapper get() {
return this.mapper;
}

}
Comment on lines +8 to +18
Copy link
Member

Choose a reason for hiding this comment

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

I doubt this will work well in practice as you want logging very early, even before ArC is set up.

If you require ArC to be set up to set up logging, it might work but you will collect lots of messages and buffer them in memory before the setup is fully done.

I think you probably need to use the ServiceLoader for this and my guess is that you won't be able to reuse the common ObjectMapper.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea is to be able to get the object mapper provided from the project including the extension. Don't think the ServiceLoader will do...

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.quarkiverse.loggingjson.providers;

import java.util.Optional;
import java.util.logging.Level;

import org.jboss.logmanager.ExtLogRecord;
Expand All @@ -19,7 +18,7 @@ protected Type type() {

@Test
void testDefaultConfig() throws Exception {
final Config.FieldConfig config = fieldConfig(Optional.empty(), Optional.empty());
final Config.FieldConfig config = fieldConfig(null, null);
Comment on lines -22 to +21
Copy link
Member

Choose a reason for hiding this comment

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

I think it's not a very good idea to mix changes that are not really related to the issue at hand. It makes the PR very verbose.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm aware of that but I considered these changes harmless and since there is no frequent activity in this repo decided to include them.

final ErrorMessageJsonProvider provider = new ErrorMessageJsonProvider(config);

final RuntimeException t = new RuntimeException("Testing errorMessage");
Expand All @@ -37,7 +36,7 @@ void testDefaultConfig() throws Exception {

@Test
void testCustomConfig() throws Exception {
Config.FieldConfig config = fieldConfig(Optional.of("em"), Optional.of(false));
Config.FieldConfig config = fieldConfig("em", false);
final ErrorMessageJsonProvider provider = new ErrorMessageJsonProvider(config);

final RuntimeException t = new RuntimeException("Testing errorMessage");
Expand All @@ -52,7 +51,7 @@ void testCustomConfig() throws Exception {
Assertions.assertEquals("Testing errorMessage", em);
Assertions.assertFalse(provider.isEnabled());

config = fieldConfig(Optional.of("em"), Optional.of(true));
config = fieldConfig("em", true);
Assertions.assertTrue(new ErrorMessageJsonProvider(config).isEnabled());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.quarkiverse.loggingjson.providers;

import java.util.Optional;
import java.util.logging.Level;

import org.jboss.logmanager.ExtLogRecord;
Expand All @@ -19,7 +18,7 @@ protected Type type() {

@Test
void testDefaultConfig() throws Exception {
final Config.FieldConfig config = fieldConfig(Optional.empty(), Optional.empty());
final Config.FieldConfig config = fieldConfig(null, null);
final ErrorTypeJsonProvider provider = new ErrorTypeJsonProvider(config);

final RuntimeException t = new RuntimeException("Testing errorType");
Expand All @@ -37,7 +36,7 @@ void testDefaultConfig() throws Exception {

@Test
void testCustomConfig() throws Exception {
Config.FieldConfig config = fieldConfig(Optional.of("et"), Optional.of(false));
Config.FieldConfig config = fieldConfig("et", false);
final ErrorTypeJsonProvider provider = new ErrorTypeJsonProvider(config);

final RuntimeException t = new RuntimeException("Testing errorType");
Expand All @@ -52,7 +51,7 @@ void testCustomConfig() throws Exception {
Assertions.assertEquals("java.lang.RuntimeException", et);
Assertions.assertFalse(provider.isEnabled());

config = fieldConfig(Optional.of("et"), Optional.of(true));
config = fieldConfig("et", true);
Assertions.assertTrue(new ErrorTypeJsonProvider(config).isEnabled());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.quarkiverse.loggingjson.providers;

import java.util.Optional;
import java.util.logging.Level;

import org.jboss.logmanager.ExtLogRecord;
Expand All @@ -19,7 +18,7 @@ protected Type type() {

@Test
void testDefaultConfig() throws Exception {
final Config.FieldConfig config = fieldConfig(Optional.empty(), Optional.empty());
final Config.FieldConfig config = fieldConfig(null, null);
final HostNameJsonProvider provider = new HostNameJsonProvider(config);

final JsonNode result = getResultAsJsonNode(provider, new ExtLogRecord(Level.ALL, "", ""));
Expand All @@ -32,7 +31,7 @@ void testDefaultConfig() throws Exception {

@Test
void testCustomConfig() throws Exception {
Config.FieldConfig config = fieldConfig(Optional.of("host"), Optional.of(false));
Config.FieldConfig config = fieldConfig("host", false);
final HostNameJsonProvider provider = new HostNameJsonProvider(config);

final JsonNode result = getResultAsJsonNode(provider, new ExtLogRecord(Level.ALL, "", ""));
Expand All @@ -42,7 +41,7 @@ void testCustomConfig() throws Exception {
Assertions.assertFalse(hostName.isEmpty());
Assertions.assertFalse(provider.isEnabled());

config = fieldConfig(Optional.of("host"), Optional.of(true));
config = fieldConfig("host", true);
Assertions.assertTrue(new HostNameJsonProvider(config).isEnabled());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ protected JsonNode getResultAsJsonNode(JsonProvider jsonProvider, ExtLogRecord e
return mapper.readValue(getResult(jsonProvider, event), JsonNode.class);
}

protected Config.FieldConfig fieldConfig(Optional<String> fieldName, Optional<Boolean> enabled) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Optionals should only be used as a return type.

protected Config.FieldConfig fieldConfig(String fieldName, Boolean enabled) {
Copy link
Contributor Author

@IvanPuntev IvanPuntev Nov 21, 2025

Choose a reason for hiding this comment

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

Using proper types instead of Optional.

return new Config.FieldConfig() {

@Override
public Optional<String> fieldName() {
return fieldName;
return Optional.ofNullable(fieldName);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will return empty optional if input is null.

}

@Override
public Optional<Boolean> enabled() {
return enabled;
return Optional.ofNullable(enabled);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will return empty optional if input is null.

}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.quarkiverse.loggingjson.providers;

import java.util.Optional;
import java.util.logging.Level;

import org.jboss.logmanager.ExtLogRecord;
Expand All @@ -19,7 +18,7 @@ protected Type type() {

@Test
void testDefaultConfig() throws Exception {
final Config.FieldConfig config = fieldConfig(Optional.empty(), Optional.empty());
final Config.FieldConfig config = fieldConfig(null, null);
final LogLevelJsonProvider provider = new LogLevelJsonProvider(config);

final ExtLogRecord event = new ExtLogRecord(Level.ALL, "", "");
Expand All @@ -34,7 +33,7 @@ void testDefaultConfig() throws Exception {

@Test
void testCustomConfig() throws Exception {
Config.FieldConfig config = fieldConfig(Optional.of("logLevel"), Optional.of(false));
Config.FieldConfig config = fieldConfig("logLevel", false);
final LogLevelJsonProvider provider = new LogLevelJsonProvider(config);

final ExtLogRecord event = new ExtLogRecord(Level.ALL, "", "");
Expand All @@ -46,7 +45,7 @@ void testCustomConfig() throws Exception {
Assertions.assertEquals("ALL", logger);
Assertions.assertFalse(provider.isEnabled());

config = fieldConfig(Optional.of("logLevel"), Optional.of(true));
config = fieldConfig("logLevel", true);
Assertions.assertTrue(new LogLevelJsonProvider(config).isEnabled());
}
}
Loading