** 参考文献 **官网 http://dreamoftch.iteye.com/blog/1899948 最详细的Log4J使用教程
log4j2比log4j1.x有显著的性能提高 ,并且增加了许多特性。所以最好使用log4j2。 推荐使用xml文件配置,可以配置一些像properties文件无法配置的功能。slf4 The Simple Logging Facade for Java ,是对诸多日志框架(e.g. java.util.logging, logback, log4j) 的外观模式封装,可以不修改源码只通过替换jar包来切换日志框架。
先简单介绍一下slf4j的强大吧,legacy 如图所示,一个应用依赖了外部两个框架,这两个框架分别使用的日志框架是Commons-logging和log4j,而这个应用本身使用的日志框架是jdk14,那么问题来了,如何在我jdk14的日志里看到这两个框架的日志记录呢? 很简单,Commons-logging我使用jcl-over-slf4j.jar来桥接一下,即虽然你调用的是Commons-logging日志接口,但是内部实际使用的是slf4j接口,同理,log4j我使用log4j-over-slf4j.jar来桥接,这样它们实际调用的都是slf4j接口,那我再使用slf4j-jdk14.jar就可以调用jdk14日志了。 在maven中,可以通过这种xxx-slf4j.jar来exclusion多余的日志框架,主要是可以把不同日志框架给统一起来了。 使用时要注意版本对应。
slf4j + log4j2 依赖 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <properties > <log4j2Version > 2.8.2</log4j2Version > </properties > <dependency > <groupId > org.apache.logging.log4j</groupId > <artifactId > log4j-api</artifactId > <version > ${log4j2Version}</version > </dependency > <dependency > <groupId > org.apache.logging.log4j</groupId > <artifactId > log4j-core</artifactId > <version > ${log4j2Version}</version > </dependency > <dependency > <groupId > org.apache.logging.log4j</groupId > <artifactId > log4j-slf4j-impl</artifactId > <version > ${log4j2Version}</version > </dependency >
xml配置文件 日志打印级别
1 2 3 4 5 6 7 8 OFF (most specific, no logging) FATAL (most specific, little data) ERROR WARN INFO DEBUG TRACE (least specific, a lot of data) ALL (least specific, all data)
默认在classpath下,log4j2-test.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?xml version="1.0" encoding="UTF-8" ?> <Configuration status ="WARN" > <Properties > <Property name ="LOG_LEVEL" value ="WARN" /> </Properties > <Appenders > <Console name ="Console" target ="SYSTEM_OUT" > <PatternLayout pattern ="%-5level %d{yy-MM-dd HH:mm:ss.SSS} [%t] %logger{36} - %msg%n" /> </Console > </Appenders > <Loggers > <Root level ="INFO" > <AppenderRef ref ="Console" level ="${LOG_LEVEL}" /> </Root > </Loggers > </Configuration >
log4j2的语法与log4j1.x的有所不同,log4j2配置文件语法 。
java中使用 1 2 3 private static final Logger logger = LogManager.getLogger(MyApp.class);logger.info("name={} passwd={} remember={}" , username, password, rememberMe);
@deprecated log4j1实例 指定logger,并且不向rootLogger输出 log4j1
1 2 3 4 5 6 <dependency > <groupId > log4j</groupId > <artifactId > log4j</artifactId > <version > 1.2.17</version > </dependency >
log4j.properties文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 log4j.rootLogger =INFO,DAILY_ROLLING_FILE,stdout log4j.appender.stdout =org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout =org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern =%d %p [%c] - %m%n log4j.appender.DAILY_ROLLING_FILE =org.apache.log4j.DailyRollingFileAppender log4j.appender.DAILY_ROLLING_FILE.Append =true log4j.appender.DAILY_ROLLING_FILE.Threshold =debug log4j.appender.DAILY_ROLLING_FILE.Encoding =UTF-8 log4j.appender.DAILY_ROLLING_FILE.File =/home/hero/temp/log.txt log4j.appender.DAILY_ROLLING_FILE.DatePattern ='.'yyyy-MM-dd log4j.appender.DAILY_ROLLING_FILE.layout =org.apache.log4j.PatternLayout log4j.appender.DAILY_ROLLING_FILE.layout.ConversionPattern =[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} [%c] %m%n 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 =%d{yyyy-MM-dd HH:mm:ss} %5p (%c:%L) - %m%n log4j.logger.MYOWNLOG =INFO,specifylog log4j.appender.specifylog =org.apache.log4j.FileAppender log4j.appender.specifylog.File =/home/hero/temp/test2.log log4j.appender.specifylog.layout =org.apache.log4j.PatternLayout log4j.appender.specifylog.layout.ConversionPattern =%d{yyyy-MM-dd HH:mm:ss} %5p (%F:%L) - %m%n log4j.appender.specifylog.Encoding =UTF-8 log4j.additivity.MYOWNLOG = false
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package common;import org.apache.log4j.LogManager;import org.apache.log4j.Logger;public class Log4jTest { static Logger logger = LogManager.getLogger(Log4jTest.class); public static void main (String[] args) { logger.info("hello" ); Logger logge = LogManager.getLogger("MYOWNLOG" ); logge.info("world" ); try { throw new IllegalArgumentException ("abcefg" ); } catch (Exception ex) { logge.error("fjsdlajflaj" , ex); } } }