java 日志框架详解
框架图
1、日志框架类型
Log4j,Log4j2,Commons Logging,Slf4j,Logback,Jul
1.1、Log4j Apache Log4j是一个基于Java的日志记录工具。它是由Ceki Gülcü首创的,现在则是Apache软件基金会的一个项目。 Log4j是几种Java日志框架之一
1.2、Log4j2 Apache Log4j2是apache开发的一款Log4j的升级产品。
1.3、Commons Logging Apache基金会所属的项目,是一套Java日志接口,之前叫Jakarta Commons Logging,后更名为Commons Logging。
1.4、Slf4j 类似于Commons Logging,是一套简易Java日志门面,本身并无日志的实现。(Simple Logging Facade for Java,缩写Slf4j)。
1.5、Logback 一套日志组件的实现(slf4j阵营)。
1.6、Jul (Java Util Logging),自Java1.4以来的官方日志实现。
2、常用框架使用
2.1、log4j(slf4j + log4j)
<!-- jar 依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
//使用
public class Log4jDemo {
private static Logger log4jLog = LoggerFactory.getLogger(Log4jDemo.class);
public static void main(String[] args) {
log4jLog.info("log4jLog--log4jLog");
log4jLog.debug("log4jLog--log4jLog: {}", "测试debug");
}
}
2.2、logback(slf4j + logback)
<!-- jar 依赖-->
<!-- slf4j + logback -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.7</version>
</dependency>
public class LogBackDemo {
private static Logger logger = LoggerFactory.getLogger(LogBackDemo.class);
public static void main(String[] args) {
logger.info("LogBackDemo-log:");
}
}
2.3、jul
public class JdkLogDemo {
private static Logger jdkLog = Logger.getLogger("com.spring.springFrame.log.jdklog.JdkLogDemo");
static {
Handler handler = new ConsoleHandler();
handler.setLevel(Level.ALL);
jdkLog.addHandler(handler);
}
public static void main(String[] args) {
jdkLog.setLevel(Level.ALL);
jdkLog.severe("jdkLog-severe");
jdkLog.warning("jdkLog-warning");
jdkLog.info("jdkLog-info");
jdkLog.config("jdkLog-config");
jdkLog.fine("jdkLog-fine");
jdkLog.finer("jdkLog-finer");
jdkLog.finest("jdkLog-finest");
}
}
2.4、common-logging(common-logging + log4j)
<!-- jar 依赖-->
<!-- common-logging + log4j -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
public class CommonLoggingLog4jDemo {
private static Log log = LogFactory.getLog(CommonLoggingLog4jDemo.class);
public static void main(String[] args) {
log.info("commonLoggingLog4jDemo-log:");
}
}