Skip to content
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 @@ -27,6 +27,7 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.logging.ErrorManager;
Expand All @@ -49,7 +50,8 @@
* directory and the default file size is 1 GB.
*/
@SuppressWarnings({
"nullness" // TODO(https://issues.apache.org/jira/browse/BEAM-10402)
"nullness", // TODO(https://issues.apache.org/jira/browse/BEAM-10402)
"ForbidDefaultCharset"
})
public class DataflowWorkerLoggingInitializer {
private static final String ROOT_LOGGER_NAME = "";
Expand Down Expand Up @@ -167,10 +169,10 @@ public static synchronized void initialize() {
originalStdErr = System.err;
System.setOut(
JulHandlerPrintStreamAdapterFactory.create(
loggingHandler, SYSTEM_OUT_LOG_NAME, Level.INFO));
loggingHandler, SYSTEM_OUT_LOG_NAME, Level.INFO, Charset.defaultCharset()));
System.setErr(
JulHandlerPrintStreamAdapterFactory.create(
loggingHandler, SYSTEM_ERR_LOG_NAME, Level.SEVERE));
loggingHandler, SYSTEM_ERR_LOG_NAME, Level.SEVERE, Charset.defaultCharset()));

// Initialize the SDK Logging Handler, which will only be used for the LoggingService
sdkLoggingHandler = makeLoggingHandler(SDK_FILEPATH_PROPERTY, DEFAULT_SDK_LOGGING_LOCATION);
Expand Down Expand Up @@ -208,7 +210,8 @@ public static synchronized void configure(DataflowWorkerLoggingOptions options)
JulHandlerPrintStreamAdapterFactory.create(
loggingHandler,
SYSTEM_OUT_LOG_NAME,
getJulLevel(options.getWorkerSystemOutMessageLevel())));
getJulLevel(options.getWorkerSystemOutMessageLevel()),
Charset.defaultCharset()));
}

if (options.getWorkerSystemErrMessageLevel() != null) {
Expand All @@ -217,7 +220,8 @@ public static synchronized void configure(DataflowWorkerLoggingOptions options)
JulHandlerPrintStreamAdapterFactory.create(
loggingHandler,
SYSTEM_ERR_LOG_NAME,
getJulLevel(options.getWorkerSystemErrMessageLevel())));
getJulLevel(options.getWorkerSystemErrMessageLevel()),
Charset.defaultCharset()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Charsets;

/**
* A {@link PrintStream} factory that creates {@link PrintStream}s which output to the specified JUL
Expand Down Expand Up @@ -69,8 +68,8 @@ private static class JulHandlerPrintStream extends PrintStream {
private int carryOverBytes;
private byte[] carryOverByteArray;

@SuppressWarnings("ForbidDefaultCharset")
private JulHandlerPrintStream(Handler handler, String loggerName, Level logLevel)
private JulHandlerPrintStream(
Handler handler, String loggerName, Level logLevel, Charset charset)
throws UnsupportedEncodingException {
super(
new OutputStream() {
Expand All @@ -80,14 +79,14 @@ public void write(int i) throws IOException {
}
},
false,
Charsets.UTF_8.name());
charset.name());
this.handler = handler;
this.loggerName = loggerName;
this.messageLevel = logLevel;
this.logger = Logger.getLogger(loggerName);
this.buffer = new StringBuilder();
this.decoder =
Charset.defaultCharset()
charset
.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
Expand Down Expand Up @@ -403,11 +402,12 @@ private void publishIfNonEmpty(String message) {
* Creates a {@link PrintStream} which redirects all output to the JUL {@link Handler} with the
* specified {@code loggerName} and {@code level}.
*/
static PrintStream create(Handler handler, String loggerName, Level messageLevel) {
static PrintStream create(
Handler handler, String loggerName, Level messageLevel, Charset charset) {
try {
return new JulHandlerPrintStream(handler, loggerName, messageLevel);
return new JulHandlerPrintStream(handler, loggerName, messageLevel, charset);
} catch (UnsupportedEncodingException exc) {
throw new RuntimeException("Encoding not supported: " + Charsets.UTF_8.name(), exc);
throw new RuntimeException("Encoding not supported: " + charset.name(), exc);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public void testLogOnNewLine() {
@Test
public void testLogRecordMetadata() {
PrintStream printStream =
JulHandlerPrintStreamAdapterFactory.create(handler, "fooLogger", Level.WARNING);
JulHandlerPrintStreamAdapterFactory.create(
handler, "fooLogger", Level.WARNING, StandardCharsets.UTF_8);
printStream.println("anyMessage");

assertThat(handler.getLogs(), not(empty()));
Expand Down Expand Up @@ -165,6 +166,7 @@ public void testNoEmptyMessages() {
}

private PrintStream createPrintStreamAdapter() {
return JulHandlerPrintStreamAdapterFactory.create(handler, LOGGER_NAME, Level.INFO);
return JulHandlerPrintStreamAdapterFactory.create(
handler, LOGGER_NAME, Level.INFO, StandardCharsets.UTF_8);
}
}