Java Logging Techniques Summary(SLF4J Introduction)

本文详细介绍了SLF4J(Simple Logging Facade for Java)作为Java日志框架的统一接口,以及如何与Log4J结合使用,通过配置log4j.properties文件实现日志级别的灵活控制,并提供了简单的示例代码,展示了如何在项目中引入SLF4J和Log4J并进行日志输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Q: What is SLF4J?

    A: Simple Logging Facade for Java

    Q: Why do we use SLF4J instead of Commons-Logging?

    A: Because JCL is runtime discovery algorithm which relies on classloader hacks to find the logging framework at runtime

        but this mechanism leads to numerous problems including unexpected behavior, hard to debug classloading problems resulting in increased complexity.

 

2. A simple example of using SLF4J --> Take SLF4J + Log4j as example

    1) pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>edu.xmu.logging</groupId>
	<artifactId>Logging-Log4J</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.5</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.5</version>
		</dependency>
	</dependencies>

</project>

    2) log4j.properties in class path is the same with that in last chapter.

# Define the root logger with appender X
log = C:/YangKunLun/logging
log4j.rootLogger = TRACE, FILE, SYSOUT
#log4j.logger.edu.xmu = ERROR

# Define the sysout appender
log4j.appender.SYSOUT=org.apache.log4j.ConsoleAppender
# Define the file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
# Set the name of the file
log4j.appender.FILE.File=${log}/log.out

# Set the immediate flush to true(default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold of file to debug mode
log4j.appender.FILE.Threshold=debug
# Set the threshold of console to trace mode
log4j.appender.SYSOUT.Threshold=trace

# Set the append to true, override
log4j.appender.FILE.Append=true

# Set the maxmium file size before rollover
# log4j.appender.FILE.MaxFileSize=5KB

# Set the date pattern
log4j.appender.FILE.DatePattern='.' yyyy-MM-dd-HH-mm

# Set the backup index
# log4j.appender.FILE.MaxBackupIndex=2;

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
# Define the layout for console appender
log4j.appender.SYSOUT.layout = org.apache.log4j.PatternLayout
log4j.appender.SYSOUT.layout.conversionPattern=%m%n

    3) Test case is the same with that in last chapter. We don't have to change the code, just to change the imported package. 

package edu.xmu.log4j;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SLFLoggingExampleTest
{
	private static Logger logger = LoggerFactory.getLogger("edu.xmu");

	@Test
	public void test()
	{
		// TRACE < DEBUG < INFO < WARN < ERROR

		logger.trace("[TRACE] This is TRACE message");
		logger.debug("[DEBUG] This is DEBUG message");
		logger.info("[INFO] This is INFO message");
		logger.warn("[WARN] This is WARN message");
		logger.error("[ERROR] This is ERROR message");
	}

}

 

3. We've see in pom.xml that we imported slf4j-log4j12 in order to use Log4j in reality.

    How can we know which package to import in order to use different kind of logging system?


 

Reference Links:

    1) http://stackoverflow.com/questions/3222895/why-is-commons-logging-believed-to-be-unpopular Why we prefer SLF4J over JCL

    2) http://articles.qos.ch/thinkAgain.html Think again before adpoting the commons-logging API

    3) http://www.slf4j.org/manual.html SLF4J user manual

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值