備忘錄三:Spring Boot+Druid+log4j2
一:pom.xml配置:去掉Spring boot 預設的spring-boot-starter-logging,引入spring-boot-starter-log4j2
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
二:應用配置application.yaml
# logging logging: config: classpath:log4j2.xml
三:log42j.xml配置
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="OFF"> <Properties> <property name="LOG_HOME">D:/logs</property> </Properties> <Appenders> <Console name="console" target="SYSTEM_OUT" immediateFlush="true"> <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" /> <PatternLayout pattern="[%-5p] [%d{yyyy-MM-dd HH:mm:ss SSS}] %c - %m%n" /> </Console> <RollingRandomAccessFile name="infoLog" immediateFlush="true" fileName="${LOG_HOME}/infoService.log" filePattern="${LOG_HOME}/infoService.log.%d{yyyy-MM-dd}.log.gz"> <PatternLayout> <pattern>[%-5p] [%d{yyyy-MM-dd HH:mm:ss SSS}] %c - %m%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> </Policies> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOM}" maxDepth="1"> <IfFileName glob="infoService*.gz" /> <IfLastModified age="7d" /> </Delete> </DefaultRolloverStrategy> <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" /> </RollingRandomAccessFile> <RollingRandomAccessFile name="errorLog" immediateFlush="true" fileName="${LOG_HOME}/errorService.log" filePattern="${LOG_HOME}/errorService.log.%d{yyyy-MM-dd}.log.gz"> <PatternLayout> <pattern>[%-5p] [%d{yyyy-MM-dd HH:mm:ss SSS}] %c - %m%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> </Policies> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOM}" maxDepth="1"> <IfFileName glob="errorService*.gz" /> <IfLastModified age="7d" /> </Delete> </DefaultRolloverStrategy> <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" /> </RollingRandomAccessFile> <RollingRandomAccessFile name="fatalLog" immediateFlush="true" fileName="${LOG_HOME}/fatalService.log" filePattern="${LOG_HOME}/fatalService.log.%d{yyyy-MM-dd}.log.gz"> <PatternLayout> <pattern>[%-5p] [%d{yyyy-MM-dd HH:mm:ss SSS}] %c - %m%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> </Policies> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOM}" maxDepth="1"> <IfFileName glob="fatalService*.gz" /> <IfLastModified age="7d" /> </Delete> </DefaultRolloverStrategy> <ThresholdFilter level="fatal" onMatch="ACCEPT" onMismatch="DENY" /> </RollingRandomAccessFile> <RollingRandomAccessFile name="druidSqlLog" immediateFlush="true" fileName="${LOG_HOME}/druidSql.log" filePattern="${LOG_HOME}/druidSql.log.%d{yyyy-MM-dd}.log.gz"> <PatternLayout> <pattern>[%-5p] [%d{yyyy-MM-dd HH:mm:ss SSS}] %c - %m%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> </Policies> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOM}" maxDepth="1"> <IfFileName glob="druidSql*.gz" /> <IfLastModified age="7d" /> </Delete> </DefaultRolloverStrategy> </RollingRandomAccessFile> </Appenders> <Loggers> <!-- 根logger的設定 --> <root level="info"> <appender-ref ref="console" /> <appender-ref ref="infoLog" /> <appender-ref ref="errorLog" /> <appender-ref ref="fatalLog" /> </root> <logger name="druid.sql.Statement" level="debug" additivity="false"> <appender-ref ref="druidSqlLog" /> </logger> <logger name="druid.sql.ResultSet" level="debug" additivity="false"> <appender-ref ref="druidSqlLog" /> </logger> </Loggers> </Configuration>
四:Druid配置
# mysql,druid spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/upin_charge?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8 username: username: password: password: druid: # 連線池的配置資訊 initial-size: 10 max-active: 100 min-idle: 10 max-wait: 60000 # 開啟PSCache,並且指定每個連線上PSCache的大小 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 # 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 time-between-eviction-runs-millis: 60000 # 配置一個連線池中最小生存的時間,單位是毫秒 min-evictable-idle-time-millis: 300000 validation-query: SELECT 1 FROM DUAL test-while-idle: true test-on-borrow: false test-on-return: false #配置DruidStatViewServlet stat-view-servlet: enabled: true url-pattern: /druid/* #login-username: admin #login-password: admin # # 配置監控統計攔截的filters,去掉後,監控介面sql無法統計,'wall'用於防火牆 filters: stat,wall,log4j2 filter: stat: log-slow-sql: true slow-sql-millis: 1000 merge-sql: true wall: config: multi-statement-allow: true log4j2: enabled: true result-set-log-enabled: true statement-executable-sql-log-enable: true statement-create-after-log-enabled: false statement-close-after-log-enabled: false result-set-open-after-log-enabled: false result-set-close-after-log-enabled: false
五:日誌檔案檢視
[DEBUG] [2019-08-06 16:58:20 570] druid.sql.Statement - {conn-10010, pstmt-20000} created. SELECT push_id,uuid,user_id,user_name,msg_type,msg_object,msg_title,msg_content,status,read_status,read_date,receive_date,receive_user_id,create_date FROM upin_msg.msg_push_info WHERE status='01' [DEBUG] [2019-08-06 16:58:20 577] druid.sql.Statement - {conn-10010, pstmt-20000} Parameters : [] [DEBUG] [2019-08-06 16:58:20 577] druid.sql.Statement - {conn-10010, pstmt-20000} Types : [] [DEBUG] [2019-08-06 16:58:20 595] druid.sql.Statement - {conn-10010, pstmt-20000} executed. SELECT push_id,uuid,user_id,user_name,msg_type,msg_object,msg_title,msg_content,status,read_status,read_date,receive_date,receive_user_id,create_date FROM upin_msg.msg_push_info WHERE status='01' [DEBUG] [2019-08-06 16:58:20 595] druid.sql.Statement - {conn-10010, pstmt-20000} executed. 19.4173 millis. SELECT push_id,uuid,user_id,user_name,msg_type,msg_object,msg_title,msg_content,status,read_status,read_date,receive_date,receive_user_id,create_date FROM upin_msg.msg_push_info WHERE status='01' [DEBUG] [2019-08-06 16:58:20 601] druid.sql.ResultSet - {conn-10010, pstmt-20000, rs-50000} Result: [2, null, 1, test, 01, test, null, test, 01, 00, null, null, null, 2019-08-05 13:41:12.0] [DEBUG] [2019-08-06 16:58:20 603] druid.sql.ResultSet - {conn-10010, pstmt-20000, rs-50000} Result: [3, null, 1, test, 01, test, null, test, 01, 00, null, null, null, 2019-08-05 13:52:05.0] [DEBUG] [2019-08-06 16:58:20 603] druid.sql.ResultSet - {conn-10010, pstmt-20000, rs-50000} Result: [4, null, 1, test, 01, test, null, test, 01, 00, null, null, null, 2019-08-05 13:53:50.0] [DEBUG] [2019-08-06 16:58:20 603] druid.sql.ResultSet - {conn-10010, pstmt-20000, rs-50000} Result: [5, null, 1, test, 01, test, null, test, 01, 00, null, null, null, 2019-08-06 10:41:10.0] [DEBUG] [2019-08-06 16:58:20 604] druid.sql.ResultSet - {conn-10010, pstmt-20000, rs-50000} Result: [6, null, 1, test, 01, test, null, test, 01, 00, null, null, null, 2019-08-06 13:49:58.0] [DEBUG] [2019-08-06 16:58:20 605] druid.sql.Statement - {conn-10010, pstmt-20000} clearParameters. [DEBUG] [2019-08-06 16:58:20 608] druid.sql.Statement - {conn-10010, pstmt-20000} Parameters : [] [DEBUG] [2019-08-06 16:58:20 608] druid.sql.Statement - {conn-10010, pstmt-20000} Types : []
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28624388/viewspace-2653262/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 備忘錄
- 【備忘錄】
- 備忘錄模式模式
- iOS備忘錄iOS
- JUnit備忘錄
- CSS備忘錄CSS
- RabbitMQ備忘錄MQ
- 備忘錄一:Spring Boot HikariCP 配置說明Spring Boot
- 備忘錄二:Spring Boot Actuator+Prometheus+GrafanaSpring BootPrometheusGrafana
- 備忘錄四:Spring Boot + P6SpySpring Boot
- 備忘錄五:Spring Boot + RabbitMQ 分散式事務Spring BootMQ分散式
- 備忘錄六:Spring Boot + Swagger_UISpring BootSwaggerUI
- 撤銷功能的實現——備忘錄模式(三)模式
- 菜鳥初學Java的備忘錄(三) (轉)Java
- Python 備忘錄Python
- Java備忘錄《集合》Java
- 網站備忘錄網站
- rman命令備忘錄
- Eigen備忘錄
- 備忘錄模式(Memento)模式
- Java備忘錄《“==” 和 “equals”》Java
- Dart 學習備忘錄Dart
- linux 備忘記錄Linux
- JavaMementoPattern(備忘錄模式)Java模式
- 19_備忘錄模式模式
- 開發 備忘錄 - 雜
- 備忘錄九:Spring Boot+Shiro許可權管理Spring Boot
- 設計模式----備忘錄模式設計模式
- [譯] Flutter 佈局備忘錄Flutter
- Docker部署禪道備忘錄Docker
- 常用工具備忘錄
- 第 22 章 備忘錄模式模式
- Android備忘錄《幀動畫》Android動畫
- 萌新(我)的Git備忘錄Git
- 設計模式 - 備忘錄模式設計模式
- Backgroundcheck:Google隱私備忘錄Go
- WPF備忘錄四(事件動畫)事件動畫
- 初學Java的備忘錄Java