Skip to content

Commit

Permalink
Add Configuration to PatternLayout backport(97679) (#97971)
Browse files Browse the repository at this point in the history
in 2.17.2 (patch release) log4j has made a refactoring that requires a Configuration to be manually passed into the created PatternLayout
If the Configuration is not passed, the System Variable lookup will not work This results in cluster.name field not being populated in logs

This commit creates a PatternLayout with a DefaultConfiguration (the same was used previous to the refactoring)

backports #97679
  • Loading branch information
pgomulka committed Jul 26, 2023
1 parent f9b064e commit fbdb9cd
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@

import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.apache.logging.log4j.core.config.Node;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
import org.apache.logging.log4j.core.layout.ByteBufferDestination;
Expand Down Expand Up @@ -63,11 +66,12 @@ public class ESJsonLayout extends AbstractStringLayout {

private final PatternLayout patternLayout;

protected ESJsonLayout(String typeName, Charset charset, String[] esmessagefields) {
protected ESJsonLayout(String typeName, Charset charset, String[] esmessagefields, final Configuration config) {
super(charset);
this.patternLayout = PatternLayout.newBuilder()
.withPattern(pattern(typeName, esmessagefields))
.withAlwaysWriteExceptions(false)
.withConfiguration(config)
.build();
}

Expand Down Expand Up @@ -135,8 +139,8 @@ private String inQuotes(String s) {
}

@PluginFactory
public static ESJsonLayout createLayout(String type, Charset charset, String[] esmessagefields) {
return new ESJsonLayout(type, charset, esmessagefields);
public static ESJsonLayout createLayout(String type, Charset charset, String[] esmessagefields, Configuration configuration) {
return new ESJsonLayout(type, charset, esmessagefields, configuration);
}

PatternLayout getPatternLayout() {
Expand All @@ -156,14 +160,17 @@ public static class Builder<B extends ESJsonLayout.Builder<B>> extends AbstractS
@PluginAttribute("esmessagefields")
private String esMessageFields;

@PluginConfiguration
private Configuration config;

public Builder() {
setCharset(StandardCharsets.UTF_8);
}

@Override
public ESJsonLayout build() {
String[] split = Strings.isNullOrEmpty(esMessageFields) ? new String[] {} : esMessageFields.split(",");
return ESJsonLayout.createLayout(type, charset, split);
return ESJsonLayout.createLayout(type, charset, split, new DefaultConfiguration());
}

public Charset getCharset() {
Expand Down

0 comments on commit fbdb9cd

Please sign in to comment.