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中入门使用一
相关推荐
通过以上步骤,Tomcat服务器将使用Log4j来管理catalina.out日志,这不但解决了日志文件过大和格式不统一的问题,而且提供了更丰富的日志管理功能,例如,可以利用Log4j强大的过滤、路由和格式化等功能,将日志管理得...
log4j.appender.Console.Target=System.out log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.appender....
log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n # 定义输出到日志...
log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ``` 2. 日志级别...
- **配置Appender属性**:如`log4j.appender.stdout.Target=System.out`,表示将日志输出到控制台。 - **设置Layout**:如`log4j.appender.stdout.layout=org.apache.log4j.PatternLayout`,并定义其模式`log4j....
log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n ...
log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n #...
log4j.appender.Console.Target=System.out log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.appender....
本教程将详细解释如何配置Log4j,使其在不同操作系统环境下(Windows和Linux)都能统一将日志打印到`user.dir`目录下,避免因路径分隔符差异带来的问题。 首先,了解`user.dir`系统属性。在Java中,`user.dir`代表...
log4j.appender.Console.Target=System.out log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j....
log4j.appender.console.Target = System.out log4j.appender.console.Threshold=DEBUG log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=[%c]-%m...
log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n # 配置文件...
在`log4j.properties`中,可能会定义多个Appender,比如将日志输出到控制台(ConsoleAppender)和文件(FileAppender)。 3. **Layouts**: Layout定义了日志信息的格式。常见的Layout有PatternLayout,可以自定义...
在Log4j中,控制日志级别主要通过配置文件`log4j.properties`或`log4j.xml`来实现。在提供的压缩包`log4j-demo`中,我们可以找到这样的配置文件。例如,如果我们想要控制特定类`com.example.MyClass`的日志级别为...
- `log4j.appender.CONSOLE.Target=System.out`:日志输出到标准输出流(System.out)。 - `log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout`:设置了布局为模式化布局。 - `log4j.appender.CONSOLE....
log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n # 文件Appender log4...
log4j.appender.Console.Target=System.out log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n # 配置文件输出 ...
log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n log4j.appender.ERROR_...
log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n # 配置文件输出 log4...