SpringBoot整合 Spring Boot Admin - 在线动态查看日志

一、前言

  Spring Boot Admin 有一个强大的功能,就是实时在线查看日志,并且可以动态更改日志级别。

SpringBoot整合 Spring Boot Admin - 在线动态查看日志_第1张图片

二、代码演示

  • 2.1 pom.xml 依赖添加(版本2.1.3)

    de.codecentric
    spring-boot-admin-server


    de.codecentric
    spring-boot-admin-client


    de.codecentric
    spring-boot-admin-server-ui
  • 2.2 logback.xml 日志配置




    
    
    
    

    ${CONTEXT_NAME}

    
    
    
    

    
    
    
    

    
    

    
    
        
            ${CONSOLE_LOG_PATTERN}
        
    

    
    
        
            ${FILE_LOG_PATTERN}
        
        ${LOG_PATH}/demo-server.log
        
            ${LOG_PATH}/demo-server-%d{yyyy-MM-dd}.%i.log
            ${MAX_FILE_SIZE}
            ${MAX_HISTORY}
        
    

    
    
        
            ${FILE_LOG_PATTERN}
        
        ${LOG_PATH}/demo-server-error.log
        
            ${LOG_PATH}/sznsh-business-server-error-%d{yyyy-MM-dd}.%i.log
            ${MAX_FILE_SIZE}
            ${MAX_HISTORY}
        
        
            ERROR
            ACCEPT
            DENY
        
    

    
    
        0
        1024
        
    
    
        0
        1024
        
    

    
    
        
    

    
    

    
        
        
        
    


注意:其中重要的是 ,如果不写将导致springbootadmin找不到文件,无法查看日志

  • 2.3 Application.java 添加@EnableAdminServer
@SpringBootApplication
@EnableAdminServer
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
  • 2.4 application.yum 配置
########################################################
### spring 配置
########################################################
spring:
  application:
    name: demo-server
  http:
    encoding:
      charset: UTF-8
      enabled: true
      force: true
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
  banner:
    charset: UTF-8
    location: classpath:config/banner.txt

########################################################
### 访问路径、端口tomcat
########################################################
server:
  port: 8080
  servlet:
    contextPath: /demo-server
  tomcat:
    max-threads: 1000
    min-spare-threads: 30
    uri-encoding: UTF-8

############################## Spring boot admin start ##############################
---
spring:
  boot:
    admin:
      client:
        url: http://localhost:${server.port}/${server.servlet.context-path}

# 开启和暴露所有端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
    logfile:
      enabled: true         #可在线查看日志
########################################################
### Log configuration
### 方便Spring Boot Admin页面上实时查看日志
### logback.xml中有详细的日志配置
########################################################
logging:
  config: classpath:config/logback.xml
  level:
    root: ${LOG_LEVEL:INFO}
    com.test.demo: info
  #  方便Spring Boot Admin页面上实时查看日志
  file: logs/demo-server.log

其中banner.txt可自行设计,主要用于启动的时候界面展示,也可使用默认springboot的图标

三、SpringBoot Admin导致swagger-ui.html访问不了

在添加spring boot admin后项目有可能会遇到swagger-ui打不开的情况,需要在WebMvcConfigurer的集成类中添加以下配置:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

	/**
	 * 页面路径配置
	 *
	 * @param configure 路径配置
	 */
	@Override
	public void configurePathMatch(PathMatchConfigurer configure) {
		// 忽视路径中的 .
		configure.setUseSuffixPatternMatch(false);
	}

	/**
	 * 多时区转换 根据页面路径参数转化 ?lang=en_US
	 *
	 * @return LocaleChangeInterceptor
	 */
	@Bean
	public LocaleChangeInterceptor localeChangeInterceptor() {
		LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
		lci.setParamName("lang");
		return lci;
	}

	/**
	 * 静态资源配置
	 *
	 * @param registry ResourceHandlerRegistry
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// common resource加载顺序
		registry.addResourceHandler("/**")
			.addResourceLocations("file:./static/")
			.addResourceLocations("classpath:/resources/")
			.addResourceLocations("classpath:/static/")
			.addResourceLocations("classpath:/public/")
			.addResourceLocations("classpath:/META-INF/resources/");

		// web jars swagger-ui knife4j
		registry.addResourceHandler("swagger-ui.html")
				.addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("doc.html")
			.addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**")
			.addResourceLocations("classpath:/META-INF/resources/webjars/");

	}
}

主要是重写addResourceHandlers方法,其他有需要的可按需添加。

你可能感兴趣的:(java开发)