Log4j将System.out搞到log4j中输出四

System.out输出的信息,难于控制,让log4j接管System.out。

第一步,自定义输出流,翻译成java语言就是Log4jPrintStream extends PrintStream,在该流中重写print方法,println方法,用log.debug输出

第二部,给jvm分配新的standard输出流,翻译成java语言就是 System.setOut(new Log4jPrintStream())

 

上干货,fuck goods

测试主类,SystemOut2Log.java 

package redirect;

public class SystemOut2Log {
	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((int)(Math.random()*100));
		}
	}
}

 

System.out的装饰类

package redirect;

import java.io.OutputStream;
import java.io.PrintStream;

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 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 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);
	}

}
/*
 *
 * public void println(boolean x) {
 * if(log.isDebugEnabled()) {
 * log.debug(Boolean.valueOf(x));
 * }
 * }
 */

再配合log4j.properties

log4j.rootCategory=DEBUG, stdout 

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p\: %d{HH\:mm\:ss,SSS} %C{1}[ln\:%L]\: %m%n

 

看输出

DEBUG: 16:20:05,083 Log4jPrintStream[ln:77]: 4
DEBUG: 16:20:05,083 Log4jPrintStream[ln:89]: abc
DEBUG: 16:20:05,083 Log4jPrintStream[ln:77]: 7
DEBUG: 16:20:05,084 Log4jPrintStream[ln:77]: 33
DEBUG: 16:20:05,084 Log4jPrintStream[ln:89]: abc
DEBUG: 16:20:05,084 Log4jPrintStream[ln:77]: 8
DEBUG: 16:20:05,084 Log4jPrintStream[ln:77]: 89
DEBUG: 16:20:05,085 Log4jPrintStream[ln:89]: abc
DEBUG: 16:20:05,085 Log4jPrintStream[ln:77]: 9
DEBUG: 16:20:05,085 Log4jPrintStream[ln:77]: 65

 

 

 Log4j的HelloWorld 在javaSE中入门使用一

 Log4j使用的效率二

 Log4j的输出格式三

 Log4j将System.out搞到log4j中输出四

 

你可能感兴趣的:(log4j,重定向,屏蔽,System.out)