log4j写日志
上一篇是用slf4j+logback写日志,这一篇改为用log4j写日志。
首先是换一下jar包,使用log4j.jar。
然后是配置log4j的配置文件log4j.properties(log4j.properties可以直接放到src下,无须配置。或者在web.xml中指定log4j.properties的位置)。
代码如下:
log4j.rootLogger=INFO,CONSOLE,FILE,STDOUT #DEBUG,CONSOLE,FILE,ROLLING_FILE,MAIL,DATABASE log4j.addivity.org.apache=true ######################## #Console Appender ######################## log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.Threshold=DEBUG log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n ######################## #stdout Appender ######################## log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender 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 ######################## # File Appender ######################## log4j.appender.FILE=org.apache.log4j.FileAppender log4j.appender.FILE.File=D:/test.log log4j.appender.FILE.Append=true log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=[heda-log] %d - %c -%-4r [%t] %-5p %c %x - %m%n # Use this layout for LogFactor 5 analysis log4j.logger.org.hibernate=debug log4j.logger.org.hibernate.type=info log4j.logger.org.hibernate.tool.hbm2ddl=debug
最后稍微改一下代码即可。
import java.util.Date; import org.apache.log4j.Logger;//导入log4j public class TestForJar { private static final Logger log = Logger.getLogger(TestForJar.class);//声明log4j的日志类 /** * @param args */ public static void main(String[] args) { log.info("---------------start-------------"); System.out.println(new Date()); log.info("---------------end-------------"); } }
相关推荐
chw0 2020-11-04
sdaq 2020-07-26
MrLiar 2020-07-07
sdaq 2020-06-16
丨Fanny丨Cri 2020-06-13
CXC0 2020-06-08
MrLiar 2020-06-04
丨Fanny丨Cri 2020-06-03
丨Fanny丨Cri 2020-05-17
JavaWinner 2020-05-10
JavaWinner 2020-05-04
CXC0 2020-04-20
CXC0 2020-04-10
丨Fanny丨Cri 2020-02-18
MrLiar 2020-02-16
CXC0 2020-02-09