日志框架

日志框架

日志框架接口

  • commons-logging
    • log4j: 实现
  • slf4j
    • logback: 实现

commons-logginsslf4j 是两种不一样的日志框架,定义了不同的日志接口,一般的,项目中只需要一种日志框架就行,如果混着用,可能日志冲突,小问题就是日志输出不了, 严重一点的就是项目启动不了…

commons-loggins

commons-loggings 的实现为 log4j

加入依赖


<log4j.version>2.13.1</log4j.version>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>${log4j.version}</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>${log4j.version}</version>
</dependency>

log4j2.xml 文件加入类路径中(resources):


<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="TRACE">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

测试:

public class Log4jTest {

	private static final Logger logger = LogManager.getLogger(Log4jTest.class);

	public static void main(String[] args) {
		logger.trace("trace level");
		logger.debug("debug level");
		logger.info("info level");
		logger.warn("warn level");
		logger.error("error level");
		logger.fatal("fatal level");
	}
}

这里的Logger类是 org.apache.logging.log4j.Logger

结果:

14:49:03.098 [main] TRACE com.g.log.logfj.Log4jTest - trace level
14:49:03.103 [main] DEBUG com.g.log.logfj.Log4jTest - debug level
14:49:03.103 [main] INFO  com.g.log.logfj.Log4jTest - info level
14:49:03.103 [main] WARN  com.g.log.logfj.Log4jTest - warn level
14:49:03.103 [main] ERROR com.g.log.logfj.Log4jTest - error level
14:49:03.103 [main] FATAL com.g.log.logfj.Log4jTest - fatal level


slf4j

slf4j 的实现为 logback

引入依赖

<slf4j.version>1.7.30</slf4j.version>
<logback.version>1.2.3</logback.version>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j.version}</version>
</dependency>

<!-- logback -->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>${logback.version}</version>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>${logback.version}</version>
</dependency>


将 logback.xml 配置文件放入类路径(resources)中:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="TRACE">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

测试:

public class LogbackTest {

	private static final Logger logger = LoggerFactory.getLogger(LogbackTest.class);

	public static void main(String[] args) {
		logger.trace("trace level");
		logger.debug("debug level");
		logger.info("info level");
		logger.warn("warn level");
		logger.error("error level");
	}
}

这里的Logger类是 org.slf4j.Logger

结果:


14:59:45.729 [main] TRACE com.g.log.logfj.LogbackTest - trace level
14:59:45.731 [main] DEBUG com.g.log.logfj.LogbackTest - debug level
14:59:45.731 [main] INFO  com.g.log.logfj.LogbackTest - info level
14:59:45.732 [main] WARN  com.g.log.logfj.LogbackTest - warn level
14:59:45.732 [main] ERROR com.g.log.logfj.LogbackTest - error level

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值