啟用設(shè)置org.slf4j.Logger打印并輸出日志方式
org.slf4j.Logger打印并輸出日志
在resouces目錄下面新建logback.xml(此為Logback推薦目錄)
內(nèi)容配置如下:
logback 分為兩種設(shè)置:
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"> <!--設(shè)置日志輸出為控制臺--> <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> <!--設(shè)置日志輸出為文件--> <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>
程序調(diào)用
1.申明 logger 變量
private Logger logger = LoggerFactory.getLogger(LoginLogDao.class);
2.在程序中調(diào)用日志
logger.debug(INSERT_LOGIN_LOG_SQL);
官方介紹網(wǎng)址:https://logback.qos.ch/demo.html
下面為官網(wǎng)介紹
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.
輸出結(jié)果如下
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(?,?,?)]
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 基于注解的 Redis 緩存使用詳解
本篇文章主要介紹了Spring Boot 基于注解的 Redis 緩存使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05servlet監(jiān)聽器的學(xué)習(xí)使用(三)
這篇文章主要為大家詳細(xì)介紹了servlet監(jiān)聽器學(xué)習(xí)使用的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09springboot整合日志處理Logback的實現(xiàn)示例
Logback是由log4j創(chuàng)始人設(shè)計的又一個開源日志組件,本文主要介紹了springboot整合日志處理Logback,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01Spring Boot高可用限流三種實現(xiàn)解決方案
限流是對某一時間窗口內(nèi)的請求數(shù)進(jìn)行限制,保持系統(tǒng)的可用性和穩(wěn)定性,本文就介紹了Spring Boot高可用限流三種實現(xiàn)解決方案,具有一定的參考價值,感興趣的可以了解一下2023-08-08SpringBoot中的自定義FailureAnalyzer詳解
這篇文章主要介紹了SpringBoot中的自定義FailureAnalyzer詳解,FailureAnalyzer是一種很好的方式在啟動時攔截異常并將其轉(zhuǎn)換為易讀的消息,并將其包含在FailureAnalysis中, Spring Boot為應(yīng)用程序上下文相關(guān)異常、JSR-303驗證等提供了此類分析器,需要的朋友可以參考下2023-12-12SpringCloud之熔斷監(jiān)控Hystrix Dashboard的實現(xiàn)
這篇文章主要介紹了SpringCloud之熔斷監(jiān)控Hystrix Dashboard的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09