Skip to content

Commit bf892e1

Browse files
committed
Address review suggestions
1 parent 72c7f01 commit bf892e1

File tree

3 files changed

+87
-72
lines changed

3 files changed

+87
-72
lines changed

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

Lines changed: 1 addition & 4 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;
@@ -778,8 +776,7 @@ public static Level getDefaultLevel() {
778776
protected void setToDefault() {
779777
// LOG4J2-1176 facilitate memory leak investigation
780778
setName(DefaultConfiguration.DEFAULT_NAME + "@" + Integer.toHexString(hashCode()));
781-
final Layout<? extends Serializable> layout = DefaultConfiguration.createDefaultLayout();
782-
final Appender appender = ConsoleAppender.createDefaultAppenderForLayout(layout);
779+
final Appender appender = ConsoleAppender.createDefaultAppenderForLayout(DefaultLayout.INSTANCE);
783780
appender.start();
784781
addAppender(appender);
785782
final LoggerConfig rootLoggerConfig = getRootLogger();

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

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,6 @@
1616
*/
1717
package org.apache.logging.log4j.core.config;
1818

19-
import java.io.IOException;
20-
import java.io.PrintWriter;
21-
import java.io.Writer;
22-
import java.nio.charset.Charset;
23-
import org.apache.logging.log4j.LoggingException;
24-
import org.apache.logging.log4j.core.Layout;
25-
import org.apache.logging.log4j.core.LogEvent;
26-
import org.apache.logging.log4j.core.layout.AbstractLayout;
27-
import org.apache.logging.log4j.core.util.StringBuilderWriter;
28-
import org.apache.logging.log4j.core.util.datetime.FixedDateFormat;
29-
import org.apache.logging.log4j.core.util.datetime.FixedDateFormat.FixedFormat;
30-
3119
/**
3220
* The default configuration writes all output to the Console using the default logging level. You configure default
3321
* logging level by setting the system property "org.apache.logging.log4j.level" to a level name. If you do not
@@ -61,60 +49,4 @@ public DefaultConfiguration() {
6149

6250
@Override
6351
protected void doConfigure() {}
64-
65-
static Layout<? extends String> createDefaultLayout() {
66-
return new DefaultLayout();
67-
}
68-
69-
/**
70-
* A simple layout used only by {@link DefaultConfiguration}
71-
* <p>
72-
* This layout allows to create applications that don't contain {@link org.apache.logging.log4j.core.layout.PatternLayout}
73-
* and all its patterns, e.g. GraalVM applications.
74-
* </p>
75-
*/
76-
private static final class DefaultLayout extends AbstractLayout<String> {
77-
78-
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
79-
80-
private final FixedDateFormat dateFormat = FixedDateFormat.create(FixedFormat.ABSOLUTE);
81-
82-
private DefaultLayout() {
83-
super(null, EMPTY_BYTE_ARRAY, EMPTY_BYTE_ARRAY);
84-
}
85-
86-
@Override
87-
public String toSerializable(LogEvent event) {
88-
try (Writer sw = new StringBuilderWriter();
89-
PrintWriter pw = new PrintWriter(sw)) {
90-
pw.append(dateFormat.format(event.getTimeMillis()))
91-
.append(" [")
92-
.append(event.getThreadName())
93-
.append("] ")
94-
.append(event.getLevel().toString())
95-
.append(" ")
96-
.append(event.getLoggerName())
97-
.append(" - ")
98-
.append(event.getMessage().getFormattedMessage())
99-
.append("\n");
100-
Throwable throwable = event.getThrown();
101-
if (throwable != null) {
102-
throwable.printStackTrace(pw);
103-
}
104-
return sw.toString();
105-
} catch (IOException e) {
106-
throw new LoggingException(e);
107-
}
108-
}
109-
110-
@Override
111-
public byte[] toByteArray(LogEvent event) {
112-
return toSerializable(event).getBytes(Charset.defaultCharset());
113-
}
114-
115-
@Override
116-
public String getContentType() {
117-
return "text/plain";
118-
}
119-
}
12052
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
final class DefaultLayout implements StringLayout {
35+
36+
static final StringLayout INSTANCE = new DefaultLayout();
37+
38+
private DefaultLayout() {}
39+
40+
@Override
41+
public String toSerializable(LogEvent event) {
42+
return new StatusData(
43+
event.getSource(),
44+
event.getLevel(),
45+
event.getMessage(),
46+
event.getThrown(),
47+
event.getThreadName())
48+
.getFormattedStatus();
49+
}
50+
51+
@Override
52+
public byte[] toByteArray(LogEvent event) {
53+
return toSerializable(event).getBytes(Charset.defaultCharset());
54+
}
55+
56+
@Override
57+
public void encode(LogEvent event, ByteBufferDestination destination) {
58+
final byte[] data = toByteArray(event);
59+
destination.writeBytes(data, 0, data.length);
60+
}
61+
62+
@Override
63+
public String getContentType() {
64+
return "text/plain";
65+
}
66+
67+
@Override
68+
public Charset getCharset() {
69+
return Charset.defaultCharset();
70+
}
71+
72+
@Override
73+
public byte[] getFooter() {
74+
return null;
75+
}
76+
77+
@Override
78+
public byte[] getHeader() {
79+
return null;
80+
}
81+
82+
@Override
83+
public Map<String, String> getContentFormat() {
84+
return Collections.emptyMap();
85+
}
86+
}

0 commit comments

Comments
 (0)