Do not replace System.err during Slf4JLoggerFactory construction
Motivation: Replacing System.err during Slf4JLoggerFactory construction is problematic as another class may optain the System.err reference before we set it back to the original value. Modifications: Remove code that temporary replaced System.err. Result: Fixes [#6212].
This commit is contained in:
parent
22b255ec9f
commit
d3e99cd75e
@ -19,16 +19,13 @@ package io.netty.util.internal.logging;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.slf4j.helpers.NOPLoggerFactory;
|
import org.slf4j.helpers.NOPLoggerFactory;
|
||||||
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logger factory which creates a <a href="http://www.slf4j.org/">SLF4J</a>
|
* Logger factory which creates a <a href="http://www.slf4j.org/">SLF4J</a>
|
||||||
* logger.
|
* logger.
|
||||||
*/
|
*/
|
||||||
public class Slf4JLoggerFactory extends InternalLoggerFactory {
|
public class Slf4JLoggerFactory extends InternalLoggerFactory {
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
public static final InternalLoggerFactory INSTANCE = new Slf4JLoggerFactory();
|
public static final InternalLoggerFactory INSTANCE = new Slf4JLoggerFactory();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,31 +37,8 @@ public class Slf4JLoggerFactory extends InternalLoggerFactory {
|
|||||||
|
|
||||||
Slf4JLoggerFactory(boolean failIfNOP) {
|
Slf4JLoggerFactory(boolean failIfNOP) {
|
||||||
assert failIfNOP; // Should be always called with true.
|
assert failIfNOP; // Should be always called with true.
|
||||||
|
if (LoggerFactory.getILoggerFactory() instanceof NOPLoggerFactory) {
|
||||||
// SFL4J writes it error messages to System.err. Capture them so that the user does not see such a message on
|
throw new NoClassDefFoundError("NOPLoggerFactory not supported");
|
||||||
// the console during automatic detection.
|
|
||||||
final StringBuffer buf = new StringBuffer();
|
|
||||||
final PrintStream err = System.err;
|
|
||||||
try {
|
|
||||||
System.setErr(new PrintStream(new OutputStream() {
|
|
||||||
@Override
|
|
||||||
public void write(int b) {
|
|
||||||
buf.append((char) b);
|
|
||||||
}
|
|
||||||
}, true, "US-ASCII"));
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new Error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (LoggerFactory.getILoggerFactory() instanceof NOPLoggerFactory) {
|
|
||||||
throw new NoClassDefFoundError(buf.toString());
|
|
||||||
} else {
|
|
||||||
err.print(buf);
|
|
||||||
err.flush();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
System.setErr(err);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user