Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix line separators in JSON logging tests #38771

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* It has to be in a <code>org.elasticsearch.common.logging</code> package to use <code>PrefixLogger</code>
*/
public class JsonLoggerTests extends ESTestCase {
private static final String LINE_SEPARATOR = System.lineSeparator();

@BeforeClass
public static void initNodeName() {
Expand Down Expand Up @@ -109,15 +110,15 @@ public void testPrefixLoggerInJson() throws IOException {

public void testJsonInMessage() throws IOException {
final Logger testLogger = LogManager.getLogger("test");
String json = "{\n" +
" \"terms\" : {\n" +
" \"user\" : [\n" +
" \"u1\",\n" +
" \"u2\",\n" +
" \"u3\"\n" +
" ],\n" +
" \"boost\" : 1.0\n" +
" }\n" +
String json = "{" + LINE_SEPARATOR +
" \"terms\" : {" + LINE_SEPARATOR +
" \"user\" : [" + LINE_SEPARATOR +
" \"u1\"," + LINE_SEPARATOR +
" \"u2\"," + LINE_SEPARATOR +
" \"u3\"" + LINE_SEPARATOR +
" ]," + LINE_SEPARATOR +
" \"boost\" : 1.0" + LINE_SEPARATOR +
" }" + LINE_SEPARATOR +
"}";

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

String json = "{\n" +
" \"terms\" : {\n" +
" \"user\" : [\n" +
" \"u1\",\n" +
" \"u2\",\n" +
" \"u3\"\n" +
" ],\n" +
" \"boost\" : 1.0\n" +
" }\n" +
String json = "{" + LINE_SEPARATOR +
" \"terms\" : {" + LINE_SEPARATOR +
" \"user\" : [" + LINE_SEPARATOR +
" \"u1\"," + LINE_SEPARATOR +
" \"u2\"," + LINE_SEPARATOR +
" \"u3\"" + LINE_SEPARATOR +
" ]," + LINE_SEPARATOR +
" \"boost\" : 1.0" + LINE_SEPARATOR +
" }" + LINE_SEPARATOR +
"}";
testLogger.error("error message " + json, new Exception(json));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
import static org.hamcrest.Matchers.equalTo;

public class JsonThrowablePatternConverterTests extends ESTestCase {
JsonThrowablePatternConverter converter = JsonThrowablePatternConverter.newInstance(null, null);
private static final String LINE_SEPARATOR = System.lineSeparator();
private JsonThrowablePatternConverter converter = JsonThrowablePatternConverter.newInstance(null, null);

public void testNoStacktrace() throws IOException {
LogEvent event = Log4jLogEvent.newBuilder()
Expand All @@ -48,15 +49,15 @@ public void testNoStacktrace() throws IOException {

public void testStacktraceWithJson() throws IOException {

String json = "{\n" +
" \"terms\" : {\n" +
" \"user\" : [\n" +
" \"u1\",\n" +
" \"u2\",\n" +
" \"u3\"\n" +
" ],\n" +
" \"boost\" : 1.0\n" +
" }\n" +
String json = "{" + LINE_SEPARATOR +
" \"terms\" : {" + LINE_SEPARATOR +
" \"user\" : [" + LINE_SEPARATOR +
" \"u1\"," + LINE_SEPARATOR +
" \"u2\"," + LINE_SEPARATOR +
" \"u3\"" + LINE_SEPARATOR +
" ]," + LINE_SEPARATOR +
" \"boost\" : 1.0" + LINE_SEPARATOR +
" }" + LINE_SEPARATOR +
"}";
Exception thrown = new Exception(json);
LogEvent event = Log4jLogEvent.newBuilder()
Expand All @@ -73,7 +74,7 @@ public void testStacktraceWithJson() throws IOException {
.findFirst()
.orElseThrow(() -> new AssertionError("no logs parsed"));

int jsonLength = json.split("\n").length;
int jsonLength = json.split(LINE_SEPARATOR).length;
int stacktraceLength = thrown.getStackTrace().length;
assertThat("stacktrace should formatted in multiple lines. JsonLogLine= " + jsonLogLine+" result= "+result,
jsonLogLine.stacktrace().size(), equalTo(jsonLength + stacktraceLength));
Expand Down