Browse Source

日志优化--优化本地的日志记录方式,增加druid的sql的日志打印和监控面板,修改druid的配置方式

pull/346/head
chenjialing 3 years ago
parent
commit
c8b9e590ef
  1. 4
      pom.xml
  2. 2
      src/main/java/com/genersoft/iot/vmp/VManageBootstrap.java
  3. 64
      src/main/java/com/genersoft/iot/vmp/conf/druid/DruidConfiguration.java
  4. 24
      src/main/java/com/genersoft/iot/vmp/conf/druid/EnableDruidSupport.java
  5. 90
      src/main/resources/logback-spring-local.xml

4
pom.xml

@ -90,8 +90,8 @@
<!-- druid数据库连接池 --> <!-- druid数据库连接池 -->
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>druid</artifactId> <artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.3</version> <version>1.1.22</version>
</dependency> </dependency>
<!-- mysql数据库 --> <!-- mysql数据库 -->

2
src/main/java/com/genersoft/iot/vmp/VManageBootstrap.java

@ -2,6 +2,7 @@ package com.genersoft.iot.vmp;
import java.util.logging.LogManager; import java.util.logging.LogManager;
import com.genersoft.iot.vmp.conf.druid.EnableDruidSupport;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.ServletComponentScan;
@ -17,6 +18,7 @@ import springfox.documentation.oas.annotations.EnableOpenApi;
@SpringBootApplication @SpringBootApplication
@EnableScheduling @EnableScheduling
@EnableOpenApi @EnableOpenApi
@EnableDruidSupport
public class VManageBootstrap extends LogManager { public class VManageBootstrap extends LogManager {
private static String[] args; private static String[] args;
private static ConfigurableApplicationContext context; private static ConfigurableApplicationContext context;

64
src/main/java/com/genersoft/iot/vmp/conf/druid/DruidConfiguration.java

@ -0,0 +1,64 @@
package com.genersoft.iot.vmp.conf.druid;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import javax.servlet.Filter;
import javax.servlet.Servlet;
/**
* druid监控配置
* @author
*/
public class DruidConfiguration {
@Value("${rj-druid-manage.allow:127.0.0.1}")
private String allow;
@Value("${rj-druid-manage.deny:}")
private String deny;
@Value("${rj-druid-manage.loginUsername:admin}")
private String loginUsername;
@Value("${rj-druid-manage.loginPassword:admin}")
private String loginPassword;
@Value("${rj-druid-manage.resetEnable:false}")
private String resetEnable;
/**
* druid监控页面开启
*/
@Bean
public ServletRegistrationBean druidServlet() {
ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
// IP白名单
servletRegistrationBean.addInitParameter("allow", allow);
// IP黑名单(共同存在时,deny优先于allow)
servletRegistrationBean.addInitParameter("deny", deny);
//控制台管理用户
servletRegistrationBean.addInitParameter("loginUsername", loginUsername);
servletRegistrationBean.addInitParameter("loginPassword", loginPassword);
//是否能够重置数据 禁用HTML页面上的“Reset All”功能
servletRegistrationBean.addInitParameter("resetEnable", resetEnable);
return servletRegistrationBean;
}
/**
* druid url监控配置
*/
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}

24
src/main/java/com/genersoft/iot/vmp/conf/druid/EnableDruidSupport.java

@ -0,0 +1,24 @@
package com.genersoft.iot.vmp.conf.druid;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* druid监控支持注解
*
* @author
* {@link DruidConfiguration} druid监控页面安全配置支持
* {@link ServletComponentScan} druid监控页面需要扫描servlet
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({
DruidConfiguration.class,
})
@ServletComponentScan
public @interface EnableDruidSupport {
}

90
src/main/resources/logback-spring-local.xml

@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<!--定义日志文件的存储地址 -->
<springProperty scop="context" name="spring.application.name" source="spring.application.name" defaultValue=""/>
<property name="LOG_HOME" value="logs/${spring.application.name}" />
<!--<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)" />-->
<!-- 控制台输出 -->
<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}:%L - %msg%n</pattern>
</encoder>
</appender>
<!-- 按照每天生成日志文件 DEBUG以上级别的日志,仅用于测试环境,正式环境为info级别以上的日志-->
<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 文件路径 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--历史日志文件输出的文件名 -->
<FileNamePattern>${LOG_HOME}/wvp-%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<!--日志文件保留天数 -->
<MaxHistory>30</MaxHistory>
<maxFileSize>20MB</maxFileSize>
</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}:%L - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<!--与ThresholdFilter的区别,允许onmatch-->
<!--设置日志级别 接收info级别的日志-->
<level>INFO</level>
</filter>
</appender>
<!-- 生成 error格式日志开始 -->
<appender name="RollingFileError" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--历史日志文件输出的文件名 -->
<FileNamePattern>${LOG_HOME}/error-%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<!--日志文件保留天数 -->
<MaxHistory>30</MaxHistory>
<maxFileSize>20MB</maxFileSize>
</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}:%L - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<!--设置日志级别,过滤掉info日志,只输入error日志-->
<level>WARN</level>
<!-- <onMatch>ACCEPT</onMatch> &lt;!&ndash; 用过滤器,只接受ERROR级别的日志信息,其余全部过滤掉 &ndash;&gt;-->
<!-- <onMismatch>DENY</onMismatch>-->
</filter>
</appender>
<!-- 生成 druid日志追加 -->
<appender name="druidSqlRollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--历史日志文件输出的文件名 -->
<FileNamePattern>${LOG_HOME}/druid-%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<!--日志文件保留天数 -->
<MaxHistory>30</MaxHistory>
<maxFileSize>50MB</maxFileSize>
</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}:%L - %msg%n</pattern>
</encoder>
</appender>
<!-- 日志输出级别 -->
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="RollingFile" />
<appender-ref ref="RollingFileError" />
</root>
<!--记录druid-sql的记录-->
<logger name="druid.sql.Statement" level="debug" additivity="true">
<!--AppenderRef ref="Console"/-->
<!-- <appender-ref ref="RollingFile"/>-->
<appender-ref ref="RollingFileError"/>
<appender-ref ref="druidSqlRollingFile"/>
</logger>
</configuration>
Loading…
Cancel
Save