log4j日志的打印

时间:2021-07-04 09:33:22

1、加入日志属性文件

log4j.properties

log4j.rootLogger=DEBUG,Console,fileout

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%5p [%c] %m%n

log4j.appender.fileout=org.apache.log4j.RollingFileAppender   #表示需要输出的文件,有些日志在控制台不会输出,会在
log4j.appender.fileout.File=ibatis.log
log4j.appender.fileout.MaxFileSize=10000KB
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.ConversionPattern=%5p [%c] %m%n

2、

有时候系统找不到日志属性文件,这里加上路径指定下

package com.mycompany.app;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator; public class Copy_2_of_App { public static void main(String[] args) throws IOException {
PropertyConfigurator.configure("D:/test_8_25/my-app/src/main/java/log4j.properties");
Logger LOG = Logger.getLogger(Copy_2_of_App.class);
LOG.info("test"); System.out.print("ok");
} }

3、log4j的配置,在wire.log里面打印http请求的数据

log4j.rootLogger=INFO

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n

log4j.appender.F=org.apache.log4j.FileAppender

log4j.appender.F.File=wire.log

log4j.appender.F.layout=org.apache.log4j.PatternLayout

log4j.appender.F.layout.ConversionPattern =%5p [%c] %m%n

log4j.logger.httpclient.wire=DEBUG, F

log4j.logger.org.apache.commons.httpclient=DEBUG, stdout