I have Java desktop client application that utilizes Log4j2 framework. JSON structure is used when logging the exceptions or infos with custom parameters. I am trying to configure the Log4j2 programmatically for this reason. I am aware that the file based configuration is recommended but it seems like it'd be better to programmatically initialize the framework for my case.
This is the structure I am trying to build.
- Custom Json Layout
HttpAppenderthat will send the logs over HTTP- If HttpAppender fails, it will use
JdbcAppenderto write to the database - If JdbcAppender fails, it will use
FileAppenderto write to a log file
I am able to generate JsonLayout but I cannot add custom fields and remove existing ones from the default structure. I've probably checked over 50 articles/questions but none of them worked for me. This is the current code I have.
public static void initializeLogger()
{
ConfigurationBuilder<BuiltConfiguration> builder =
ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setStatusLevel(Level.DEBUG);
builder.setConfigurationName("DefaultLogger");
//Creating console appender just to see logging is there.
AppenderComponentBuilder appenderBuilder = builder.newAppender("Console", "CONSOLE")
.addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
appenderBuilder.add(builder.newLayout("PatternLayout")
.addAttribute("pattern", pattern)
.addAttribute("AdditionalField.key", "asd")
.addAttribute("AdditionalField.value", "qwe"));
RootLoggerComponentBuilder rootLogger = builder.newRootLogger(Level.DEBUG);
rootLogger.add(builder.newAppenderRef("Console"));
builder.add(appenderBuilder);
//Creating the file appender
LayoutComponentBuilder layoutBuilder = builder.newLayout("JsonLayout")
.addAttribute("compact", "false")
.addAttribute("AdditionalField", builder.newKeyValuePair("asd", "qwe"));
ComponentBuilder triggeringPolicy = builder.newComponent("Policies")
.addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", "1KB"));
appenderBuilder = builder.newAppender("LogToRollingFile", "RollingFile")
.addAttribute("fileName", fileName)
.addAttribute("filePattern", "applog-%d{MM-dd-yy-HH-mm-ss}.log.")
.add(layoutBuilder)
.addComponent(triggeringPolicy);
builder.add(appenderBuilder);
rootLogger.add(builder.newAppenderRef("LogToRollingFile"));
builder.add(rootLogger);
Configurator.reconfigure(builder.build());
}
This code prints to the console and writes Json formatted logging to a file. The custom values are not added to the Json. I tried to create JsonLayout via its builder but I cannot add it to the builder itself.
//Probably creating a new layout instance with additional fields
//might be helpful but I cannot use this in the builder above.
JsonLayout layout = JsonLayout.newBuilder()
.setAdditionalFields(new KeyValuePair [] {
new KeyValuePair("asd", "qwe"),
new KeyValuePair("zxc", "rty"),
}).build();
The parameters that I need to add to the Json is dynamic and I do not know the keys and values of the custom nodes. At this stage, I am trying to create a custom Json Layout at write it to the file.
I might be doing this completely wrong so please advice! I'm open to suggestions. Thanks.