Commit c8b9e590efaf5e7a10724b5f26f14de741e3c8a7
1 parent
a42dda2b
日志优化--优化本地的日志记录方式,增加druid的sql的日志打印和监控面板,修改druid的配置方式
Showing
5 changed files
with
182 additions
and
2 deletions
pom.xml
| @@ -90,8 +90,8 @@ | @@ -90,8 +90,8 @@ | ||
| 90 | <!-- druid数据库连接池 --> | 90 | <!-- druid数据库连接池 --> |
| 91 | <dependency> | 91 | <dependency> |
| 92 | <groupId>com.alibaba</groupId> | 92 | <groupId>com.alibaba</groupId> |
| 93 | - <artifactId>druid</artifactId> | ||
| 94 | - <version>1.2.3</version> | 93 | + <artifactId>druid-spring-boot-starter</artifactId> |
| 94 | + <version>1.1.22</version> | ||
| 95 | </dependency> | 95 | </dependency> |
| 96 | 96 | ||
| 97 | <!-- mysql数据库 --> | 97 | <!-- mysql数据库 --> |
src/main/java/com/genersoft/iot/vmp/VManageBootstrap.java
| @@ -2,6 +2,7 @@ package com.genersoft.iot.vmp; | @@ -2,6 +2,7 @@ package com.genersoft.iot.vmp; | ||
| 2 | 2 | ||
| 3 | import java.util.logging.LogManager; | 3 | import java.util.logging.LogManager; |
| 4 | 4 | ||
| 5 | +import com.genersoft.iot.vmp.conf.druid.EnableDruidSupport; | ||
| 5 | import org.springframework.boot.SpringApplication; | 6 | import org.springframework.boot.SpringApplication; |
| 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; | 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 7 | import org.springframework.boot.web.servlet.ServletComponentScan; | 8 | import org.springframework.boot.web.servlet.ServletComponentScan; |
| @@ -17,6 +18,7 @@ import springfox.documentation.oas.annotations.EnableOpenApi; | @@ -17,6 +18,7 @@ import springfox.documentation.oas.annotations.EnableOpenApi; | ||
| 17 | @SpringBootApplication | 18 | @SpringBootApplication |
| 18 | @EnableScheduling | 19 | @EnableScheduling |
| 19 | @EnableOpenApi | 20 | @EnableOpenApi |
| 21 | +@EnableDruidSupport | ||
| 20 | public class VManageBootstrap extends LogManager { | 22 | public class VManageBootstrap extends LogManager { |
| 21 | private static String[] args; | 23 | private static String[] args; |
| 22 | private static ConfigurableApplicationContext context; | 24 | private static ConfigurableApplicationContext context; |
src/main/java/com/genersoft/iot/vmp/conf/druid/DruidConfiguration.java
0 → 100644
| 1 | +package com.genersoft.iot.vmp.conf.druid; | ||
| 2 | + | ||
| 3 | +import com.alibaba.druid.support.http.StatViewServlet; | ||
| 4 | +import com.alibaba.druid.support.http.WebStatFilter; | ||
| 5 | +import org.springframework.beans.factory.annotation.Value; | ||
| 6 | +import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
| 7 | +import org.springframework.boot.web.servlet.ServletRegistrationBean; | ||
| 8 | +import org.springframework.context.annotation.Bean; | ||
| 9 | + | ||
| 10 | +import javax.servlet.Filter; | ||
| 11 | +import javax.servlet.Servlet; | ||
| 12 | + | ||
| 13 | +/** | ||
| 14 | + * druid监控配置 | ||
| 15 | + * @author | ||
| 16 | + */ | ||
| 17 | +public class DruidConfiguration { | ||
| 18 | + | ||
| 19 | + @Value("${rj-druid-manage.allow:127.0.0.1}") | ||
| 20 | + private String allow; | ||
| 21 | + | ||
| 22 | + @Value("${rj-druid-manage.deny:}") | ||
| 23 | + private String deny; | ||
| 24 | + | ||
| 25 | + @Value("${rj-druid-manage.loginUsername:admin}") | ||
| 26 | + private String loginUsername; | ||
| 27 | + | ||
| 28 | + @Value("${rj-druid-manage.loginPassword:admin}") | ||
| 29 | + private String loginPassword; | ||
| 30 | + | ||
| 31 | + @Value("${rj-druid-manage.resetEnable:false}") | ||
| 32 | + private String resetEnable; | ||
| 33 | + | ||
| 34 | + /** | ||
| 35 | + * druid监控页面开启 | ||
| 36 | + */ | ||
| 37 | + @Bean | ||
| 38 | + public ServletRegistrationBean druidServlet() { | ||
| 39 | + ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); | ||
| 40 | + // IP白名单 | ||
| 41 | + servletRegistrationBean.addInitParameter("allow", allow); | ||
| 42 | + // IP黑名单(共同存在时,deny优先于allow) | ||
| 43 | + servletRegistrationBean.addInitParameter("deny", deny); | ||
| 44 | + //控制台管理用户 | ||
| 45 | + servletRegistrationBean.addInitParameter("loginUsername", loginUsername); | ||
| 46 | + servletRegistrationBean.addInitParameter("loginPassword", loginPassword); | ||
| 47 | + //是否能够重置数据 禁用HTML页面上的“Reset All”功能 | ||
| 48 | + servletRegistrationBean.addInitParameter("resetEnable", resetEnable); | ||
| 49 | + return servletRegistrationBean; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * druid url监控配置 | ||
| 54 | + */ | ||
| 55 | + @Bean | ||
| 56 | + public FilterRegistrationBean filterRegistrationBean() { | ||
| 57 | + FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>(new WebStatFilter()); | ||
| 58 | + filterRegistrationBean.addUrlPatterns("/*"); | ||
| 59 | + filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); | ||
| 60 | + return filterRegistrationBean; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + | ||
| 64 | +} | ||
| 0 | \ No newline at end of file | 65 | \ No newline at end of file |
src/main/java/com/genersoft/iot/vmp/conf/druid/EnableDruidSupport.java
0 → 100644
| 1 | +package com.genersoft.iot.vmp.conf.druid; | ||
| 2 | + | ||
| 3 | +import org.springframework.boot.web.servlet.ServletComponentScan; | ||
| 4 | +import org.springframework.context.annotation.Import; | ||
| 5 | + | ||
| 6 | +import java.lang.annotation.*; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * druid监控支持注解 | ||
| 10 | + * | ||
| 11 | + * @author | ||
| 12 | + * {@link DruidConfiguration} druid监控页面安全配置支持 | ||
| 13 | + * {@link ServletComponentScan} druid监控页面需要扫描servlet | ||
| 14 | + */ | ||
| 15 | +@Target(ElementType.TYPE) | ||
| 16 | +@Retention(RetentionPolicy.RUNTIME) | ||
| 17 | +@Documented | ||
| 18 | +@Inherited | ||
| 19 | +@Import({ | ||
| 20 | + DruidConfiguration.class, | ||
| 21 | +}) | ||
| 22 | +@ServletComponentScan | ||
| 23 | +public @interface EnableDruidSupport { | ||
| 24 | +} |
src/main/resources/logback-spring-local.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | +<configuration debug="false"> | ||
| 3 | + <!--定义日志文件的存储地址 --> | ||
| 4 | + <springProperty scop="context" name="spring.application.name" source="spring.application.name" defaultValue=""/> | ||
| 5 | + <property name="LOG_HOME" value="logs/${spring.application.name}" /> | ||
| 6 | + | ||
| 7 | + <!--<property name="COLOR_PATTERN" value="%black(%contextName-) %red(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta( %replace(%caller{1}){'\t|Caller.{1}0|\r\n', ''})- %gray(%msg%xEx%n)" />--> | ||
| 8 | + <!-- 控制台输出 --> | ||
| 9 | + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
| 10 | + <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> | ||
| 11 | + <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 --> | ||
| 12 | + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n</pattern> | ||
| 13 | + </encoder> | ||
| 14 | + </appender> | ||
| 15 | + | ||
| 16 | + <!-- 按照每天生成日志文件 DEBUG以上级别的日志,仅用于测试环境,正式环境为info级别以上的日志--> | ||
| 17 | + <appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
| 18 | + | ||
| 19 | + <!-- 文件路径 --> | ||
| 20 | + <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> | ||
| 21 | + <!--历史日志文件输出的文件名 --> | ||
| 22 | + <FileNamePattern>${LOG_HOME}/wvp-%d{yyyy-MM-dd}.%i.log</FileNamePattern> | ||
| 23 | + <!--日志文件保留天数 --> | ||
| 24 | + <MaxHistory>30</MaxHistory> | ||
| 25 | + <maxFileSize>20MB</maxFileSize> | ||
| 26 | + </rollingPolicy> | ||
| 27 | + <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> | ||
| 28 | + <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 --> | ||
| 29 | + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n</pattern> | ||
| 30 | + </encoder> | ||
| 31 | + <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> | ||
| 32 | + <!--与ThresholdFilter的区别,允许onmatch--> | ||
| 33 | + <!--设置日志级别 接收info级别的日志--> | ||
| 34 | + <level>INFO</level> | ||
| 35 | + </filter> | ||
| 36 | + </appender> | ||
| 37 | + | ||
| 38 | + <!-- 生成 error格式日志开始 --> | ||
| 39 | + <appender name="RollingFileError" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
| 40 | + | ||
| 41 | + <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> | ||
| 42 | + <!--历史日志文件输出的文件名 --> | ||
| 43 | + <FileNamePattern>${LOG_HOME}/error-%d{yyyy-MM-dd}.%i.log</FileNamePattern> | ||
| 44 | + <!--日志文件保留天数 --> | ||
| 45 | + <MaxHistory>30</MaxHistory> | ||
| 46 | + <maxFileSize>20MB</maxFileSize> | ||
| 47 | + </rollingPolicy> | ||
| 48 | + <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> | ||
| 49 | + <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 --> | ||
| 50 | + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n</pattern> | ||
| 51 | + </encoder> | ||
| 52 | + <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> | ||
| 53 | + <!--设置日志级别,过滤掉info日志,只输入error日志--> | ||
| 54 | + <level>WARN</level> | ||
| 55 | + <!-- <onMatch>ACCEPT</onMatch> <!– 用过滤器,只接受ERROR级别的日志信息,其余全部过滤掉 –>--> | ||
| 56 | + <!-- <onMismatch>DENY</onMismatch>--> | ||
| 57 | + </filter> | ||
| 58 | + </appender> | ||
| 59 | + | ||
| 60 | + <!-- 生成 druid日志追加 --> | ||
| 61 | + <appender name="druidSqlRollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
| 62 | + <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> | ||
| 63 | + <!--历史日志文件输出的文件名 --> | ||
| 64 | + <FileNamePattern>${LOG_HOME}/druid-%d{yyyy-MM-dd}.%i.log</FileNamePattern> | ||
| 65 | + <!--日志文件保留天数 --> | ||
| 66 | + <MaxHistory>30</MaxHistory> | ||
| 67 | + <maxFileSize>50MB</maxFileSize> | ||
| 68 | + </rollingPolicy> | ||
| 69 | + <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> | ||
| 70 | + <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 --> | ||
| 71 | + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n</pattern> | ||
| 72 | + </encoder> | ||
| 73 | + </appender> | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + <!-- 日志输出级别 --> | ||
| 77 | + <root level="INFO"> | ||
| 78 | + <appender-ref ref="STDOUT" /> | ||
| 79 | + <appender-ref ref="RollingFile" /> | ||
| 80 | + <appender-ref ref="RollingFileError" /> | ||
| 81 | + </root> | ||
| 82 | + | ||
| 83 | + <!--记录druid-sql的记录--> | ||
| 84 | + <logger name="druid.sql.Statement" level="debug" additivity="true"> | ||
| 85 | + <!--AppenderRef ref="Console"/--> | ||
| 86 | + <!-- <appender-ref ref="RollingFile"/>--> | ||
| 87 | + <appender-ref ref="RollingFileError"/> | ||
| 88 | + <appender-ref ref="druidSqlRollingFile"/> | ||
| 89 | + </logger> | ||
| 90 | +</configuration> | ||
| 0 | \ No newline at end of file | 91 | \ No newline at end of file |