JAVA打印日志使用slf4j

slf4j可以使用占位符。

logger.info("receive message={}", backmessage);

如果是log4j需要用+把字符串拼接起来,logger.info("receive message="+ backmessage);

1.maven 加入jar包

       <dependency>
               <groupId>log4j</groupId>
               <artifactId>log4j</artifactId>
            <version>1.2.17</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>

2.resource文件夹下log4j.properties

#日志输出级别(INFO)和输出位置(stdout,logfile)
log4j.rootCategory=INFO,stdout,logfile
#日志输出位置为控制台
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.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=../logs/izzzzz.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS}  [%c]-[%p]-[%L] - %m%n
# 定义相应包路径下的日志输出级别
log4j.logger.org.springframework=WARN
log4j.logger.com.test.btz=INFO

3.javaTest中测试

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private Logger logger = LoggerFactory.getLogger(this.getClass());

logger.info("receive message={}", backmessage);

可以再加个包,在类上直接使用:@Slf4j  

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>

 

posted @ 2021-06-03 17:50  甜菜波波  阅读(536)  评论(0编辑  收藏  举报