Skip to content

Commit fbdb9cd

Browse files
authored
Add Configuration to PatternLayout backport(97679) (#97971)
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
1 parent f9b064e commit fbdb9cd

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

server/src/main/java/org/elasticsearch/common/logging/ESJsonLayout.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010

1111
import org.apache.logging.log4j.core.Layout;
1212
import org.apache.logging.log4j.core.LogEvent;
13+
import org.apache.logging.log4j.core.config.Configuration;
14+
import org.apache.logging.log4j.core.config.DefaultConfiguration;
1315
import org.apache.logging.log4j.core.config.Node;
1416
import org.apache.logging.log4j.core.config.plugins.Plugin;
1517
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
1618
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
19+
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
1720
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
1821
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
1922
import org.apache.logging.log4j.core.layout.ByteBufferDestination;
@@ -63,11 +66,12 @@ public class ESJsonLayout extends AbstractStringLayout {
6366

6467
private final PatternLayout patternLayout;
6568

66-
protected ESJsonLayout(String typeName, Charset charset, String[] esmessagefields) {
69+
protected ESJsonLayout(String typeName, Charset charset, String[] esmessagefields, final Configuration config) {
6770
super(charset);
6871
this.patternLayout = PatternLayout.newBuilder()
6972
.withPattern(pattern(typeName, esmessagefields))
7073
.withAlwaysWriteExceptions(false)
74+
.withConfiguration(config)
7175
.build();
7276
}
7377

@@ -135,8 +139,8 @@ private String inQuotes(String s) {
135139
}
136140

137141
@PluginFactory
138-
public static ESJsonLayout createLayout(String type, Charset charset, String[] esmessagefields) {
139-
return new ESJsonLayout(type, charset, esmessagefields);
142+
public static ESJsonLayout createLayout(String type, Charset charset, String[] esmessagefields, Configuration configuration) {
143+
return new ESJsonLayout(type, charset, esmessagefields, configuration);
140144
}
141145

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

163+
@PluginConfiguration
164+
private Configuration config;
165+
159166
public Builder() {
160167
setCharset(StandardCharsets.UTF_8);
161168
}
162169

163170
@Override
164171
public ESJsonLayout build() {
165172
String[] split = Strings.isNullOrEmpty(esMessageFields) ? new String[] {} : esMessageFields.split(",");
166-
return ESJsonLayout.createLayout(type, charset, split);
173+
return ESJsonLayout.createLayout(type, charset, split, new DefaultConfiguration());
167174
}
168175

169176
public Charset getCharset() {

0 commit comments

Comments
 (0)