Skip to content

Commit e8d5ffc

Browse files
ppkarwaszvy
andauthored
Create DefaultLayout independent of PatternLayout (#3118)
Currently, `PatternLayout` and all its patterns are a required element of Log4j Core, since `DefaultConfiguration` uses it. The default configuration is used in all Log4j Core installation as failsafe to handle logging between the time a logger context is created and the real configuration starts (a couple of ms). This PR creates a simple hardcoded layout to use with `DefaultConfiguration`, based on the `StatusData` formatter. Co-authored-by: Volkan Yazıcı <volkan@yazi.ci>
1 parent 89f80f1 commit e8d5ffc

File tree

3 files changed

+100
-8
lines changed

3 files changed

+100
-8
lines changed

log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.File;
2121
import java.io.IOException;
2222
import java.io.InputStream;
23-
import java.io.Serializable;
2423
import java.lang.ref.WeakReference;
2524
import java.net.URI;
2625
import java.util.ArrayList;
@@ -40,7 +39,6 @@
4039
import org.apache.logging.log4j.Level;
4140
import org.apache.logging.log4j.core.Appender;
4241
import org.apache.logging.log4j.core.Filter;
43-
import org.apache.logging.log4j.core.Layout;
4442
import org.apache.logging.log4j.core.LifeCycle2;
4543
import org.apache.logging.log4j.core.LogEvent;
4644
import org.apache.logging.log4j.core.LoggerContext;
@@ -58,7 +56,6 @@
5856
import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
5957
import org.apache.logging.log4j.core.config.plugins.util.PluginType;
6058
import org.apache.logging.log4j.core.filter.AbstractFilterable;
61-
import org.apache.logging.log4j.core.layout.PatternLayout;
6259
import org.apache.logging.log4j.core.lookup.ConfigurationStrSubstitutor;
6360
import org.apache.logging.log4j.core.lookup.Interpolator;
6461
import org.apache.logging.log4j.core.lookup.PropertiesLookup;
@@ -779,11 +776,7 @@ public static Level getDefaultLevel() {
779776
protected void setToDefault() {
780777
// LOG4J2-1176 facilitate memory leak investigation
781778
setName(DefaultConfiguration.DEFAULT_NAME + "@" + Integer.toHexString(hashCode()));
782-
final Layout<? extends Serializable> layout = PatternLayout.newBuilder()
783-
.withPattern(DefaultConfiguration.DEFAULT_PATTERN)
784-
.withConfiguration(this)
785-
.build();
786-
final Appender appender = ConsoleAppender.createDefaultAppenderForLayout(layout);
779+
final Appender appender = ConsoleAppender.createDefaultAppenderForLayout(DefaultLayout.INSTANCE);
787780
appender.start();
788781
addAppender(appender);
789782
final LoggerConfig rootLoggerConfig = getRootLogger();
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.core.config;
18+
19+
import java.nio.charset.Charset;
20+
import java.util.Collections;
21+
import java.util.Map;
22+
import org.apache.logging.log4j.core.LogEvent;
23+
import org.apache.logging.log4j.core.StringLayout;
24+
import org.apache.logging.log4j.core.layout.ByteBufferDestination;
25+
import org.apache.logging.log4j.status.StatusData;
26+
27+
/**
28+
* A simple layout used only by {@link DefaultConfiguration}
29+
* <p>
30+
* This layout allows to create applications that don't contain {@link org.apache.logging.log4j.core.layout.PatternLayout}
31+
* and all its patterns, e.g. GraalVM applications.
32+
* </p>
33+
*
34+
* @since 2.25.0
35+
*/
36+
final class DefaultLayout implements StringLayout {
37+
38+
static final StringLayout INSTANCE = new DefaultLayout();
39+
40+
private DefaultLayout() {}
41+
42+
@Override
43+
public String toSerializable(LogEvent event) {
44+
return new StatusData(
45+
event.getSource(),
46+
event.getLevel(),
47+
event.getMessage(),
48+
event.getThrown(),
49+
event.getThreadName())
50+
.getFormattedStatus();
51+
}
52+
53+
@Override
54+
public byte[] toByteArray(LogEvent event) {
55+
return toSerializable(event).getBytes(Charset.defaultCharset());
56+
}
57+
58+
@Override
59+
public void encode(LogEvent event, ByteBufferDestination destination) {
60+
final byte[] data = toByteArray(event);
61+
destination.writeBytes(data, 0, data.length);
62+
}
63+
64+
@Override
65+
public String getContentType() {
66+
return "text/plain";
67+
}
68+
69+
@Override
70+
public Charset getCharset() {
71+
return Charset.defaultCharset();
72+
}
73+
74+
@Override
75+
public byte[] getFooter() {
76+
return null;
77+
}
78+
79+
@Override
80+
public byte[] getHeader() {
81+
return null;
82+
}
83+
84+
@Override
85+
public Map<String, String> getContentFormat() {
86+
return Collections.emptyMap();
87+
}
88+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="changed">
6+
<issue id="3118" link="https://github.com/apache/logging-log4j2/pull/3118"/>
7+
<description format="asciidoc">
8+
Changes the layout used by the
9+
https://logging.apache.org/log4j/2.x/manual/configuration.html#automatic-configuration[default configuration].
10+
</description>
11+
</entry>

0 commit comments

Comments
 (0)