springboot添加AOP日志配置詳解
1,pom.xml配置
文件中添加以下配置
<!--spring aop 包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
2,自定義配置文件logback.xml
該配置文件位置在templates文件夾下第一層,和html文件夾位置并列。
3,logback.xml文件配置內(nèi)容
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <!--定義日志文件的存儲地址 勿在 LogBack 的配置中使用相對路徑--> <property name="LOG_HOME" value="D:/projects/log" /> <!-- 控制臺輸出 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日志消息,%n是換行符--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> </appender> <!-- 按照每天生成日志文件 --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--日志文件輸出的文件名--> <FileNamePattern>${LOG_HOME}/springbootdemo.log.%d{yyyy-MM-dd}.log</FileNamePattern> <!--日志文件保留天數(shù)--> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日志消息,%n是換行符--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> <!--日志文件最大的大小--> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <!--myibatis log configure--> <logger name="com.apache.ibatis" level="DEBUG"/> <logger name="java.sql.Connection" level="DEBUG"/> <logger name="java.sql.Statement" level="DEBUG"/> <logger name="java.sql.PreparedStatement" level="DEBUG"/> <!-- 日志輸出級別 trace<debug<info<warn<error<fatal 級別之間是包含的關(guān)系,意思是如果你設(shè)置日志級別是trace,則大于等于這個級別的日志都會輸出。 trace: 是追蹤,就是程序推進以下,你就可以寫個trace輸出,所以trace應(yīng)該會特別多,不過沒關(guān)系,我們可以設(shè)置最低日志級別不讓他輸出。 debug: 調(diào)試么,我一般就只用這個作為最低級別,trace壓根不用。是在沒辦法就用eclipse或者idea的debug功能就好了么。 info: 輸出一下你感興趣的或者重要的信息,這個用的最多了。 warn: 有些信息不是錯誤信息,但是也要給程序員的一些提示,類似于eclipse中代碼的驗證不是有error 和warn(不算錯誤但是也請注意,比如以下depressed的方法)。 error: 錯誤信息。用的也比較多。 fatal: 級別比較高了。重大錯誤,這種級別你可以直接停止程序了,是不應(yīng)該出現(xiàn)的錯誤么!不用那么緊張,其實就是一個程度的問題。 --> <root level="DEBUG"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root> <!--日志異步到數(shù)據(jù)庫 --> <!--<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">--> <!--<!–日志異步到數(shù)據(jù)庫 –>--> <!--<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">--> <!--<!–連接池 –>--> <!--<dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">--> <!--<driverClass>com.mysql.jdbc.Driver</driverClass>--> <!--<url>jdbc:mysql://127.0.0.1:3306/databaseName</url>--> <!--<user>root</user>--> <!--<password>root</password>--> <!--</dataSource>--> <!--</connectionSource>--> <!--</appender>--> </configuration>
4,寫一個LogUtil類
@Component @Aspect public class logUtil { /** * 切入點配置 */ @Pointcut(value = "execution(* com.aaa.service.*.*(..))") public void pointCutOne(){ }<br> /** * 前置通知 * @param joinPoint */ // @Before(value = "pointCutOne()") public void beforeSaveLog(JoinPoint joinPoint){ System.out.println("在調(diào)用"+joinPoint.getSignature().getName()+"方法之前,打印。。。"); } /** * 后置通知 * @param joinPoint */ @After(value = "pointCutOne()") public void afterReturningSaveLog(JoinPoint joinPoint){ System.out.println("在調(diào)用"+joinPoint.getSignature().getName()+"方法之后,打印。。。"); } /** * 異常通知 * @param joinPoint * @param e */ @AfterThrowing(value = "pointCutOne()",throwing = "e") public void afterThrowingSaveLog(JoinPoint joinPoint,Exception e){ System.out.println("在調(diào)用"+joinPoint.getSignature().getName()+"方法時出現(xiàn)了"+e.getClass().getName()+"異常,異常描述:"+e.getMessage()); }<br> /** * 最終通知 */ @AfterReturning(value = "pointCutOne()") public void afterSaveLog(JoinPoint joinPoint){ System.out.println("在調(diào)用"+joinPoint.getSignature().getName()+"方法時,無論有沒有異常都會打印。。。"); }<br> /** * 環(huán)繞通知 */ public Object aroundSaveLog(ProceedingJoinPoint proceedingJoinPoint){ Object proceed = null; System.out.println(System.currentTimeMillis()+"執(zhí)行業(yè)務(wù)方法之前---"); try { proceed = proceedingJoinPoint.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println(System.currentTimeMillis()+"執(zhí)行業(yè)務(wù)方法之后---"); return proceed; } }
到此這篇關(guān)于springboot添加AOP日志配置詳解的文章就介紹到這了,更多相關(guān)springbootAOP日志配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java配置多個過濾器優(yōu)先級以及幾個常用過濾器操作
這篇文章主要介紹了java配置多個過濾器優(yōu)先級以及幾個常用過濾器的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Spring中@ConditionalOnProperty注解的作用詳解
這篇文章主要介紹了Spring中@ConditionalOnProperty注解的作用詳解,@ConditionalOnProperty注解主要是用來判斷配置文件中的內(nèi)容來決定配置類是否生效用的,如果條件不匹配,則配置類不生效,需要的朋友可以參考下2024-01-01SpringBoot項目nohup啟動運行日志過大的解決方案
這篇文章主要介紹了SpringBoot項目nohup啟動運行日志過大的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05JavaWeb項目web.xml中出現(xiàn)Element xxx is not al
這篇文章主要介紹了JavaWeb項目web.xml中出現(xiàn)Element xxx is not allowed here問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11關(guān)于java.math.BigDecimal比較大小問題
這篇文章主要介紹了關(guān)于java.math.BigDecimal比較大小問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07使用Java代碼進行因數(shù)分解和求最小公倍數(shù)的示例
這篇文章主要介紹了使用Java代碼進行因數(shù)分解和求最小公倍數(shù)的示例,都是基于最基礎(chǔ)的算法原理實現(xiàn),需要的朋友可以參考下2015-11-11