不学无数的程序员

不学无数的程序员

SpringBoot

1.Profiles

Spring Profiles能够在不同的环境中使不同的应用配置生效。@Component和@Configuration两个注解都能够通过@Profiles来标记。下面是例子:

@Configuration
@Profile("buxuewushu")
public class ProductionConfiguration {

	// ...

}

在配置文件中可以通过spring.profiles.active这个变量来控制哪个Profiles生效。例如,可以在application.properties配置文件中配置如下:

spring.profiles.active=buxuewushu1,buxuewushu --即通过@Profiles注解标记的名为buxuewushu和buxuewushu1的文件生效

当然也可以通过命令行的形式进行配置:--spring.profiles.active= buxuewushu1, buxuewushu

1.1在代码中配置

在启动文件运行之前可以通过SpringApplication设置需要使哪一个配置生效,SpringApplication.setAdditionalProfiles(…​),也可以通过Spring的ConfigurableEnvironment接口来配置。

2.日志

SpringBoot使用 **Commons Logging**作为内部的日志门面,但是也提供了一系列的接口来实现扩展。默认的日志配置有,Java Util LoggingLog4J2和[Logback] (https://commons.apache.org/proper/commons-logging/),在这每一种的实现下,都能通过配置来实现针对于哪一些日志的输出。

2.1日志的格式

SpringBoot默认的日志输出格式如下所示,默认输出级别是INFO。默认使用的是Logback进行记录日志的。

2018-07-20 19:11:40.046  INFO 21758 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@db57326: startup date [Fri Jul 20 19:11:38 CST 2018]; root of context hierarchy
2018-07-20 19:11:40.093  INFO 21758 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.example.FirstSpringBoot.FirstSpringBootApplication.home()
2018-07-20 19:11:40.097  INFO 21758 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-07-20 19:11:40.097  INFO 21758 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
  • 时间:非常精确的时间,并且是根据时间进行排序的输出顺序。
  • 日志级别:ERROR,WARN,INFO,DEBUG,TRANCE
  • 进程ID
  • 通过---进行分割,右边为实际的日志输出内容
  • 线程名
  • 日志名称:通常是源类的名称
  • 日志的内容

2.2调整日志级别

SpringBoot默认的日志级别是INFO,如果想打印其他级别的日志的话可以通过配置。日志级别的顺序是ERROR>WARN>INFO>DEBUG>TRANCE.第一种办法是通过在启动的时候进行参数的配置--debug。

$ java -jar myapp.jar --debug

2.3在文件中记录日志

默认情况下,SpringBoot仅仅在控制台中打印日志,不会将日志记录在文件中。如果想将日志输出在文件中的话,那么可以在配置文件中配置文件的路径。logging.file或者logging.path.下面介绍logging.file和logging.path的区别。

当日志文件记录的数据达到一定量时,SpringBoot会将此文件进行压缩为.gz的压缩文件。SpringBoot默认的大小为10M。当然这个大小也可以通过logging.file.max-size进行配置,但是必须得带单位。

2.4自定义日志配置

许多的日志系统能够被相应的配置文件所配置,或者通过在application.properties中配置Spring的环境变量进行配置logging.config。

可以通过org.springframework.boot.logging.LoggingSystem的系统变量的设置从而使用特定的日志系统,系统日志的value值应该是日志实现的全路径名。

不同的日志系统所对应的配置文件如下:

为了更好配置化,一些在properties中的配置被转化为了配置在系统中的变量。对应关系如下:

2.5 Logback的扩展

SpringBoot为Logback的可扩展性提供了许多的帮助。能够在logback-spring.xml文件中进行配置想要扩展的内容。

通常在开发过程中,我们根据不同的环境进行配置不同的日志级别。所以在配置文件中可以通过<springProfile>标签进行有选择的使具体哪一步分生效。例子如下,在logback-spring.xml进行配置

--想要的配置的具体内容在<springProfile>标签中进行编写
<springProfile name="staging">
	<!-- 配置staging生效 -->
</springProfile>

<springProfile name="dev | staging">
	<!-- 配置dev或者staging生效 -->
</springProfile>

<springProfile name="!production">
	<!-- 配置除了production之外的生效 -->
</springProfile>
11-23 08:38