java 用 log4j 将System.out.printLine("");中的数据也保存在文件中, 怎么配置呢?

2025-02-27 09:09:15
推荐回答(3个)
回答1:

这个只能输出到控制台。如果你真的要这样麻烦的话请参照下面:
log4j.xml 配置(放到 src 下,即 classpath 下):

XML code




















测试类:
public class SystemOutLog {

static {
Log4jPrintStream.redirectSystemOut();
}

public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
System.out.print("abc");
System.out.print(i);
System.out.print((char)(i + 0×21));
}
}
}

Log4jPrintStream 的实现:
import java.io.IOException;
import java.io.OutputStream; import java.io.PrintStream;
import java.util.Locale;

import org.apache.log4j.Logger;

public class Log4jPrintStream extends PrintStream {

private Logger log = Logger.getLogger("SystemOut");

private static PrintStream instance = new Log4jPrintStream(System.out);

private Log4jPrintStream(OutputStream out) {
super(out);
}

public static void redirectSystemOut() {
System.setOut(instance);
}

public PrintStream append(char c) {
// Ignore
return this;
}

public PrintStream append(CharSequence csq, int start, int end) {
// Ignore
return this;
}

public PrintStream append(CharSequence csq) {
// Ignore
return this;
}

public boolean checkError() {
// Ignore
return false;
}

protected void clearError() {
// Ignore
}

public void close() {
// Ignore
}

public void flush() {
// Ignore
}

public PrintStream format(Locale l, String format, Object… args) {
// Ignore
return this;
}

public PrintStream format(String format, Object… args) {
// Ignore
return this;
}

public void print(boolean b) {
println(b);
}

public void print(char c) {
println(c);
}

public void print(char[] s) {
println(s);
}

public void print(double d) {
println(d);
}

public void print(float f) {
println(f);
}

public void print(int i) {
println(i);
}

public void print(long l) {
println(l);
}

public void print(Object obj) {
println(obj);
}

public void print(String s) {
println(s);
}

public PrintStream printf(Locale l, String format, Object… args) {
// Ignore
return this;
}

public PrintStream printf(String format, Object… args) {
// Ignore
return this;
}

public void println() {
// Ignore
}

public void println(boolean x) {
log.debug(Boolean.valueOf(x));
}

public void println(char x) {
log.debug(Character.valueOf(x));
}

public void println(char[] x) {
log.debug(x == null ? null : new String(x));
}

public void println(double x) {
log.debug(Double.valueOf(x));
}

public void println(float x) {
log.debug(Float.valueOf(x));
}

public void println(int x) {
log.debug(Integer.valueOf(x));
}

public void println(long x) {
log.debug(x);
}

public void println(Object x) {
log.debug(x);
}

public void println(String x) {
log.debug(x);
}

protected void setError() {
// Ignore
}

public void write(byte[] buf, int off, int len) {
// Ignore
}

public void write(int b) {
// Ignore
}

public void write(byte[] b) throws IOException {
// Ignore
}
}

把 System.out 的输出流重定向到 log4j 的 DEBUG 级别日志中去
为了提高效率,还能改成这样:
public void println(boolean x) {
if(log.isDebugEnabled()) {
log.debug(Boolean.valueOf(x));
}
}

public void println(char x) {
if(log.isDebugEnabled()) {
log.debug(Character.valueOf(x));
}
}

public void println(char[] x) {
if(log.isDebugEnabled()) {
log.debug(x == null ? null : new String(x));
}
}

public void println(double x) {
if(log.isDebugEnabled()) {
log.debug(Double.valueOf(x));
}
}

public void println(float x) {
if(log.isDebugEnabled()) {
log.debug(Float.valueOf(x));
}
}

public void println(int x) {
if(log.isDebugEnabled()) {
log.debug(Integer.valueOf(x));
}
}

public void println(long x) {
if(log.isDebugEnabled()) {
log.debug(Long.valueOf(x));
}
}

回答2:

我这是jboss中的xml配置,你参考一下:
我这个是可以把system.out.println()的你让存储在日志文件中的。





回答3:

log4j和System.out.print没有关系,所以控制不了。如果需要记录,不要用System.out.print来打印信息,直接用log4j