Skip to content

Commit dc0af84

Browse files
authored
Fix line separators in JSON logging tests backport#38771 (#38833)
The hardcoded '\n' in string will not work in Windows where there is a different line separator. A System.lineSeparator should be used to make it work on all platforms closes #38705 backport #38771
1 parent 15bb494 commit dc0af84

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

qa/logging-config/src/test/java/org/elasticsearch/common/logging/JsonLoggerTests.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* It has to be in a <code>org.elasticsearch.common.logging</code> package to use <code>PrefixLogger</code>
4747
*/
4848
public class JsonLoggerTests extends ESTestCase {
49+
private static final String LINE_SEPARATOR = System.lineSeparator();
4950

5051
@BeforeClass
5152
public static void initNodeName() {
@@ -109,15 +110,15 @@ public void testPrefixLoggerInJson() throws IOException {
109110

110111
public void testJsonInMessage() throws IOException {
111112
final Logger testLogger = LogManager.getLogger("test");
112-
String json = "{\n" +
113-
" \"terms\" : {\n" +
114-
" \"user\" : [\n" +
115-
" \"u1\",\n" +
116-
" \"u2\",\n" +
117-
" \"u3\"\n" +
118-
" ],\n" +
119-
" \"boost\" : 1.0\n" +
120-
" }\n" +
113+
String json = "{" + LINE_SEPARATOR +
114+
" \"terms\" : {" + LINE_SEPARATOR +
115+
" \"user\" : [" + LINE_SEPARATOR +
116+
" \"u1\"," + LINE_SEPARATOR +
117+
" \"u2\"," + LINE_SEPARATOR +
118+
" \"u3\"" + LINE_SEPARATOR +
119+
" ]," + LINE_SEPARATOR +
120+
" \"boost\" : 1.0" + LINE_SEPARATOR +
121+
" }" + LINE_SEPARATOR +
121122
"}";
122123

123124
testLogger.info(json);
@@ -151,15 +152,15 @@ public void testStacktrace() throws IOException {
151152
public void testJsonInStacktraceMessageIsSplitted() throws IOException {
152153
final Logger testLogger = LogManager.getLogger("test");
153154

154-
String json = "{\n" +
155-
" \"terms\" : {\n" +
156-
" \"user\" : [\n" +
157-
" \"u1\",\n" +
158-
" \"u2\",\n" +
159-
" \"u3\"\n" +
160-
" ],\n" +
161-
" \"boost\" : 1.0\n" +
162-
" }\n" +
155+
String json = "{" + LINE_SEPARATOR +
156+
" \"terms\" : {" + LINE_SEPARATOR +
157+
" \"user\" : [" + LINE_SEPARATOR +
158+
" \"u1\"," + LINE_SEPARATOR +
159+
" \"u2\"," + LINE_SEPARATOR +
160+
" \"u3\"" + LINE_SEPARATOR +
161+
" ]," + LINE_SEPARATOR +
162+
" \"boost\" : 1.0" + LINE_SEPARATOR +
163+
" }" + LINE_SEPARATOR +
163164
"}";
164165
testLogger.error("error message " + json, new Exception(json));
165166

server/src/test/java/org/elasticsearch/common/logging/JsonThrowablePatternConverterTests.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
import static org.hamcrest.Matchers.equalTo;
3434

3535
public class JsonThrowablePatternConverterTests extends ESTestCase {
36-
JsonThrowablePatternConverter converter = JsonThrowablePatternConverter.newInstance(null, null);
36+
private static final String LINE_SEPARATOR = System.lineSeparator();
37+
private JsonThrowablePatternConverter converter = JsonThrowablePatternConverter.newInstance(null, null);
3738

3839
public void testNoStacktrace() throws IOException {
3940
LogEvent event = Log4jLogEvent.newBuilder()
@@ -47,19 +48,18 @@ public void testNoStacktrace() throws IOException {
4748
assertThat(jsonLogLine.stacktrace(), Matchers.nullValue());
4849
}
4950

50-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/38705")
5151
public void testStacktraceWithJson() throws IOException {
5252
LogManager.getLogger().info("asdf");
5353

54-
String json = "{\n" +
55-
" \"terms\" : {\n" +
56-
" \"user\" : [\n" +
57-
" \"u1\",\n" +
58-
" \"u2\",\n" +
59-
" \"u3\"\n" +
60-
" ],\n" +
61-
" \"boost\" : 1.0\n" +
62-
" }\n" +
54+
String json = "{" + LINE_SEPARATOR +
55+
" \"terms\" : {" + LINE_SEPARATOR +
56+
" \"user\" : [" + LINE_SEPARATOR +
57+
" \"u1\"," + LINE_SEPARATOR +
58+
" \"u2\"," + LINE_SEPARATOR +
59+
" \"u3\"" + LINE_SEPARATOR +
60+
" ]," + LINE_SEPARATOR +
61+
" \"boost\" : 1.0" + LINE_SEPARATOR +
62+
" }" + LINE_SEPARATOR +
6363
"}";
6464
Exception thrown = new Exception(json);
6565
LogEvent event = Log4jLogEvent.newBuilder()
@@ -75,7 +75,7 @@ public void testStacktraceWithJson() throws IOException {
7575
.findFirst()
7676
.orElseThrow(() -> new AssertionError("no logs parsed"));
7777

78-
int jsonLength = json.split("\n").length;
78+
int jsonLength = json.split(LINE_SEPARATOR).length;
7979
int stacktraceLength = thrown.getStackTrace().length;
8080
assertThat("stacktrace should formatted in multiple lines",
8181
jsonLogLine.stacktrace().size(), equalTo(jsonLength + stacktraceLength));

0 commit comments

Comments
 (0)