diff --git a/server/src/main/java/org/elasticsearch/common/logging/LoggingOutputStream.java b/server/src/main/java/org/elasticsearch/common/logging/LoggingOutputStream.java index 72d4b34da702d..b5679ada5a28f 100644 --- a/server/src/main/java/org/elasticsearch/common/logging/LoggingOutputStream.java +++ b/server/src/main/java/org/elasticsearch/common/logging/LoggingOutputStream.java @@ -91,7 +91,12 @@ public void write(int b) throws IOException { public void flush() { Buffer buffer = threadLocal.get(); if (buffer.used == 0) return; - log(new String(buffer.bytes, 0, buffer.used, StandardCharsets.UTF_8)); + int used = buffer.used; + if (buffer.bytes[used - 1] == '\r') { + // windows case: remove the first part of newlines there too + --used; + } + log(new String(buffer.bytes, 0, used, StandardCharsets.UTF_8)); if (buffer.bytes.length != DEFAULT_BUFFER_LENGTH) { threadLocal.set(new Buffer()); // reset size } else { diff --git a/server/src/test/java/org/elasticsearch/common/logging/LoggingOutputStreamTests.java b/server/src/test/java/org/elasticsearch/common/logging/LoggingOutputStreamTests.java index 0787ff78fafe7..dd2ba2bd3977d 100644 --- a/server/src/test/java/org/elasticsearch/common/logging/LoggingOutputStreamTests.java +++ b/server/src/test/java/org/elasticsearch/common/logging/LoggingOutputStreamTests.java @@ -71,9 +71,17 @@ public void testNull() { assertTrue(loggingStream.lines.isEmpty()); } - public void testFlushOnNewline() { - printStream.println("hello"); - printStream.println("world"); + // this test explicitly outputs the newlines instead of relying on println, to always test the unix behavior + public void testFlushOnUnixNewline() { + printStream.print("hello\n"); + printStream.print("world\n"); + assertThat(loggingStream.lines, contains("hello", "world")); + } + + // this test explicitly outputs the newlines instead of relying on println, to always test the windows behavior + public void testFlushOnWindowsNewline() { + printStream.print("hello\r\n"); + printStream.print("world\r\n"); assertThat(loggingStream.lines, contains("hello", "world")); }