In My code:
System.out.println("hello println");
mylogger.info("hello info");
mylogger.debug("hello debug");
I want only log from logger in my console (no print "hello println" via System.out.println):
hello info
hello debug
My log4j file is:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="consoleInfo" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss}]: %m%n" />
</layout>
</appender>
<category name="org.openqa.selenium">
<priority value="off" />
</category>
<category name="org.apache.log4j">
<priority value="off" />
</category>
<root>
<level value="INFO" />
<appender-ref ref="consoleInfo" />
</root>
</log4j:configuration>
You can "turn off" System.out
by replacing it with a writer that just ignores all input:
System.out.println("This is written.");
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {}
}));
System.out.println("Not written!");
If you happen to have Guava on the classpath, it can be shortened to
System.setOut(new PrintStream(ByteStreams.nullOutputStream()));
You can redirect your system outs to an appender by pointing the out parameter of System to an appender as below:
public abstract class TraceStream extends PrintStream
{
private static final Logger trace = LogManager.getLogger(TraceStream.class);
public TraceStream(OutputStream stream) {
super(stream);
}
protected abstract void trace(String line);
public void print(boolean b) {
TRACE.info(String.valueOf(b));
}
public void print(char c) {
trace(String.valueOf(c));
}
public void print(char[] s) {
trace(String.valueOf(s));
}
public void print(double d) {
trace(String.valueOf(d));
}
public void print(float f) {
trace(String.valueOf(f));
}
public void print(int i) {
trace(String.valueOf(i));
}
public void print(long l) {
trace(String.valueOf(l));
}
public void print(Object obj) {
trace(obj.toString());
}
public void print(String s) {
trace(s);
}
public void println() {
trace("");
}
public void println(boolean x) {
trace(String.valueOf(x));
}
public void println(char x) {
trace(String.valueOf(x));
}
public void println(char[] x) {
trace(String.valueOf(x));
}
public void println(double x) {
trace(String.valueOf(x));
}
public void println(float x) {
trace(String.valueOf(x));
}
public void println(int x) {
trace(String.valueOf(x));
}
public void println(long x) {
trace(String.valueOf(x));
}
public void println(Object x) {
trace(x.toString());
}
public void println(String x) {
trace(x);
}
public void write(int c) {
trace(String.valueOf(c));
}
public static void traceStdErr() {
System.setErr(new TraceStream(System.err) {
protected void trace(String line) {
TRACE.error(ThreeLetterCode.CODE_EXC, line);
}
});
}
public static void traceStdOut() {
System.setOut(new TraceStream(System.out) {
protected void trace(String line) {
TRACE.info(ThreeLetterCode.CODE_INF, line);
}
});
}
}
This way , all your System outs and errs will reach the log file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.