Skip to content

Commit 286191e

Browse files
authored
DefaultLayout: Append a newline to the serialized LogEvent (#3851)
Fixes #3835.
1 parent 61ec36a commit 286191e

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.appender;
18+
19+
import static org.junit.jupiter.api.Assertions.assertFalse;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import java.nio.charset.Charset;
25+
import org.apache.logging.log4j.LogManager;
26+
import org.apache.logging.log4j.Logger;
27+
import org.junit.jupiter.api.Test;
28+
29+
class DefaultLayoutTest {
30+
@Test
31+
void testDefaultLayout() {
32+
PrintStream standardOutput = System.out;
33+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
34+
35+
System.setOut(new PrintStream(baos));
36+
try {
37+
Logger log = LogManager.getLogger(getClass());
38+
log.fatal("This is a fatal message");
39+
log.error("This is an error message");
40+
log.warn("This is a warning message");
41+
42+
String actualOutput = new String(baos.toByteArray(), Charset.defaultCharset());
43+
assertTrue(actualOutput.contains("FATAL This is a fatal message" + System.lineSeparator()));
44+
assertTrue(actualOutput.contains("ERROR This is an error message" + System.lineSeparator()));
45+
assertFalse(actualOutput.contains("This is a warning message"));
46+
} finally {
47+
System.setOut(standardOutput);
48+
}
49+
}
50+
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ private DefaultLayout() {}
4242
@Override
4343
public String toSerializable(LogEvent event) {
4444
return new StatusData(
45-
event.getSource(),
46-
event.getLevel(),
47-
event.getMessage(),
48-
event.getThrown(),
49-
event.getThreadName())
50-
.getFormattedStatus();
45+
event.getSource(),
46+
event.getLevel(),
47+
event.getMessage(),
48+
event.getThrown(),
49+
event.getThreadName())
50+
.getFormattedStatus()
51+
+ System.lineSeparator();
5152
}
5253

5354
@Override
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns="https://logging.apache.org/xml/ns"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="
5+
https://logging.apache.org/xml/ns
6+
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
7+
type="fixed">
8+
<issue id="3835" link="https://github.com/apache/logging-log4j2/issues/3835"/>
9+
<description format="asciidoc">
10+
Fix missing newlines in default logging configuration for log4j-core.
11+
</description>
12+
</entry>

0 commit comments

Comments
 (0)