解決引用slf4j中Logger.info隻打印出文字沒有數據的問題
slf4j Logger.info隻打印出文字沒有數據
引的是 slf4j 包
import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static Logger logger = LoggerFactory.getLogger(TsfTest.class); logger.info("打印參數:",map);
隻能打印出:
2019-06-14 17:52:07.246 [http-apr-8080-exec-10] INFO c.q.m.p.b.rest.test – 打印參數:
解決方案
在第一個參數中加入花括號{ }即可。
logger.info("打印參數:{}",map);
解決!
啟用設置org.slf4j.Logger打印並輸出日志
在resouces目錄下面新建logback.xml(此為Logback推薦目錄)
內容配置如下
logback 分為兩種設置:
1. 輸出到控制臺 STDOUT
2. 輸出到文件 FILE
pom.xml配置
<properties> <slf4j.version>1.7.25</slf4j.version> </properties> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency>
logback.xml配置
下面的配置同時配置輸出到文件和輸出到控制臺
<?xml version="1.0" encoding="UTF-8" ?> <configuration scan="true" scanPeriod="3 seconds"> <!--設置日志輸出為控制臺--> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%X{userId}] [%X{requestId}] %logger - %msg%n</pattern> </encoder> </appender> <!--設置日志輸出為文件--> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>logFile.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logFile.%d{yyyy-MM-dd_HH-mm}.log.zip</FileNamePattern> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern>%d{HH:mm:ss,SSS} [%thread] %-5level %logger{32} - %msg%n</Pattern> </layout> </appender> <root> <level value="DEBUG"/> <appender-ref ref="STDOUT"/> <appender-ref ref="FILE"/> </root> </configuration>
程序調用
1.申明 logger 變量
private Logger logger = LoggerFactory.getLogger(LoginLogDao.class);
2.在程序中調用日志
logger.debug(INSERT_LOGIN_LOG_SQL);
官方介紹網址:https://logback.qos.ch/demo.html
下面為官網介紹
logback-classic with two appenders: a ConsoleAppender and a RollingFileAppender. The RollingFileAppender sends logging events to a file called logFile.log and will rollover the active file every minute. The old file will be renamed and compressed to a zip file. The ConsoleAppender will output the logging requests to the console, and shorten the logger names to gain space on the console window, without loss of legibility. For example, ch.qos.logback.demo.prime.NumberCruncherImpl will be abbreviated as c.q.l.d.prime.NumberCruncherImpl.
輸出結果如下
isDebugEnabled true
2017-04-23 23:58:35,502 DEBUG [http-nio-8080-exec-6] (LoginLogDao.java:32) – INSERT INTO t_login_log(user_id,ip,login_datetime) VALUES(?,?,?)
2017-04-23 23:58:35,503 DEBUG [http-nio-8080-exec-6] (JdbcTemplate.java:869) – Executing prepared SQL update
2017-04-23 23:58:35,503 DEBUG [http-nio-8080-exec-6] (JdbcTemplate.java:616) – Executing prepared SQL statement [INSERT INTO t_login_log(user_id,ip,login_datetime) VALUES(?,?,?)]
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Java logback日志的簡單使用
- Springboot允許logger.debug輸出日志方式
- SpringBoot3集成SLF4J+logback進行日志記錄的實現
- log4j配置失效日志中打印Debug信息問題
- springboot2.0整合logback日志的詳細代碼