-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Capture stdout and stderr to log4j log #50259
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
37c2672
Capture stdout and stderr to log4j log
rjernst b461467
fix forbidden
rjernst 7091ba0
add buffer size limit
rjernst 8184834
Merge branch 'master' into logging1
rjernst c6ddcb3
Merge branch 'master' into logging1
rjernst 31c754f
Merge branch 'master' into logging1
rjernst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
server/src/main/java/org/elasticsearch/common/logging/LoggingOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.common.logging; | ||
|
||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* A stream whose output is sent to the configured logger, line by line. | ||
*/ | ||
class LoggingOutputStream extends OutputStream { | ||
/** The starting length of the buffer */ | ||
static final int DEFAULT_BUFFER_LENGTH = 1024; | ||
|
||
// limit a single log message to 64k | ||
static final int MAX_BUFFER_LENGTH = DEFAULT_BUFFER_LENGTH * 64; | ||
|
||
class Buffer { | ||
|
||
/** The buffer of bytes sent to the stream */ | ||
byte[] bytes = new byte[DEFAULT_BUFFER_LENGTH]; | ||
|
||
/** Number of used bytes in the buffer */ | ||
int used = 0; | ||
} | ||
|
||
// each thread gets its own buffer so messages don't get garbled | ||
ThreadLocal<Buffer> threadLocal = ThreadLocal.withInitial(Buffer::new); | ||
|
||
private final Logger logger; | ||
|
||
private final Level level; | ||
|
||
LoggingOutputStream(Logger logger, Level level) { | ||
this.logger = logger; | ||
this.level = level; | ||
} | ||
|
||
@Override | ||
public void write(int b) throws IOException { | ||
if (threadLocal == null) { | ||
throw new IOException("buffer closed"); | ||
} | ||
if (b == 0) return; | ||
if (b == '\n') { | ||
// always flush with newlines instead of adding to the buffer | ||
flush(); | ||
return; | ||
} | ||
|
||
Buffer buffer = threadLocal.get(); | ||
|
||
if (buffer.used == buffer.bytes.length) { | ||
if (buffer.bytes.length >= MAX_BUFFER_LENGTH) { | ||
// don't let the buffer get infinitely big | ||
flush(); | ||
// we reset the buffer in flush so get the new instance | ||
buffer = threadLocal.get(); | ||
} else { | ||
// extend the buffer | ||
buffer.bytes = Arrays.copyOf(buffer.bytes, 2 * buffer.bytes.length); | ||
} | ||
} | ||
|
||
buffer.bytes[buffer.used++] = (byte) b; | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
Buffer buffer = threadLocal.get(); | ||
if (buffer.used == 0) return; | ||
log(new String(buffer.bytes, 0, buffer.used, StandardCharsets.UTF_8)); | ||
if (buffer.bytes.length != DEFAULT_BUFFER_LENGTH) { | ||
threadLocal.set(new Buffer()); // reset size | ||
} else { | ||
buffer.used = 0; | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
threadLocal = null; | ||
} | ||
|
||
// pkg private for testing | ||
void log(String msg) { | ||
logger.log(level, msg); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
server/src/test/java/org/elasticsearch/common/logging/LoggingOutputStreamTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.common.logging; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
import org.junit.Before; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.elasticsearch.common.logging.LoggingOutputStream.DEFAULT_BUFFER_LENGTH; | ||
import static org.elasticsearch.common.logging.LoggingOutputStream.MAX_BUFFER_LENGTH; | ||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class LoggingOutputStreamTests extends ESTestCase { | ||
|
||
class TestLoggingOutputStream extends LoggingOutputStream { | ||
List<String> lines = new ArrayList<>(); | ||
|
||
TestLoggingOutputStream() { | ||
super(null, null); | ||
} | ||
|
||
@Override | ||
void log(String msg) { | ||
lines.add(msg); | ||
} | ||
} | ||
|
||
TestLoggingOutputStream loggingStream; | ||
PrintStream printStream; | ||
|
||
@Before | ||
public void createStream() { | ||
loggingStream = new TestLoggingOutputStream(); | ||
printStream = new PrintStream(loggingStream, false, StandardCharsets.UTF_8); | ||
} | ||
|
||
public void testEmptyLine() { | ||
printStream.println(""); | ||
assertTrue(loggingStream.lines.isEmpty()); | ||
printStream.flush(); | ||
assertTrue(loggingStream.lines.isEmpty()); | ||
} | ||
|
||
public void testNull() { | ||
printStream.write(0); | ||
printStream.flush(); | ||
assertTrue(loggingStream.lines.isEmpty()); | ||
} | ||
|
||
public void testFlushOnNewline() { | ||
printStream.println("hello"); | ||
printStream.println("world"); | ||
assertThat(loggingStream.lines, contains("hello", "world")); | ||
} | ||
|
||
public void testBufferExtension() { | ||
String longStr = randomAlphaOfLength(DEFAULT_BUFFER_LENGTH); | ||
String extraLongStr = randomAlphaOfLength(DEFAULT_BUFFER_LENGTH + 1); | ||
printStream.println(longStr); | ||
assertThat(loggingStream.threadLocal.get().bytes.length, equalTo(DEFAULT_BUFFER_LENGTH)); | ||
printStream.println(extraLongStr); | ||
assertThat(loggingStream.lines, contains(longStr, extraLongStr)); | ||
assertThat(loggingStream.threadLocal.get().bytes.length, equalTo(DEFAULT_BUFFER_LENGTH)); | ||
} | ||
|
||
public void testMaxBuffer() { | ||
String longStr = randomAlphaOfLength(MAX_BUFFER_LENGTH); | ||
String extraLongStr = longStr + "OVERFLOW"; | ||
printStream.println(longStr); | ||
printStream.println(extraLongStr); | ||
assertThat(loggingStream.lines, contains(longStr, longStr, "OVERFLOW")); | ||
} | ||
|
||
public void testClosed() { | ||
loggingStream.close(); | ||
IOException e = expectThrows(IOException.class, () -> loggingStream.write('a')); | ||
assertThat(e.getMessage(), containsString("buffer closed")); | ||
} | ||
|
||
public void testThreadIsolation() throws Exception { | ||
printStream.print("from thread 1"); | ||
Thread thread2 = new Thread(() -> { | ||
printStream.println("from thread 2"); | ||
}); | ||
thread2.start(); | ||
thread2.join(); | ||
printStream.flush(); | ||
assertThat(loggingStream.lines, contains("from thread 2", "from thread 1")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this looks fine in plaintext logs, but might end up harder to analyze with JSON logs. Why not adding
\n
to the buffer too?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would cause double the lines in the log. Think of it as making this equivalent to most of our logging calls do not contain a newline in the message. Log4j inserts newlines per message emitted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense, thank you