学习笔记-码出高效Java开发手册-日志

本文探讨了日志框架的使用,包括SLF4J、Logback和Log4J的配置方式,阐述了条件输出的重要性,以避免资源浪费,并详细介绍了Maven配置下不同日志框架的依赖项。

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

5.4 日志

框架

意义

规范

级别

//条件输出,避免资源浪费
if(logger.isDebugEnabled()){//条件输出
	logger.debug("processing trade with id: "+id+" and symbol: "+symbol);
	//占位符如下
    logger.debug("processing trade with id: {} and symbol: {}", id, symbol);
}

框架

日志门面

日志适配器

日志库

配置

Maven配置-slf4j+logback

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

Maven配置-slf4j+slf4j-log4j12+log4j

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

如果老代码直接使用了log4j日志库提供的接口来打印日志,则还需要引入日志库适配器

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

使用

//logger定义为static变量,因为与类绑定,避免每次都new一个新对象造成资源浪费,内存溢出等
private static final Logger logger=LoggerFactory.getLogger(Abc.class);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值