Spring Boot默认配置只会输出到控制台,并不会记录到位置文件中,但是我们通常生产环境使用时都需要以文件方式记录。
格式化日志输出
一般如果你是使用springboot中的starters,都会默认使用logback日志框架,而且在不需要你任何配置的情况下,logback框架就可以执行
springboot的日志默认输出格式:(这里使用的是idea社区版,跟专业版显示可能有点不太一样)
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
2019-08-25 16:22:20.588 INFO 10692 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on T440-150310-PC with PID 10692 (started by Administrator in D:\Program Files (x86)\idea community\demo)
2019-08-25 16:22:20.595 DEBUG 10692 --- [ main] com.example.demo.DemoApplication : Running with Spring Boot v2.1.7.RELEASE, Spring v5.1.9.RELEASE
2019-08-25 16:22:20.597 INFO 10692 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2019-08-25 16:22:25.493 INFO 10692 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 5.94 seconds (JVM running for 7.117)
- 输出内容元素具体如下:
- 时间日期 —— 精确到毫秒
- 日志级别 ——
TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
- 进程ID
- 分隔符 —— 标识实际日志的开始
- 线程名 —— 括号括起来(可能会截断控制台输出)
- Logger名 —— 通常使用源代码的类名
- 日志内容
控制台输出
在springboot中默认的日志输出级别有 Error、Wran、Info
比如我们在application.properties文件中写入以下配置,debug设置为true的时候会输出logger的核心内容
debug = true
控制台输出如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
2019-08-25 22:09:22.844 INFO 8904 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on T440-150310-PC with PID 8904 (started by Administrator in D:\Program Files (x86)\idea community\demo)
2019-08-25 22:09:22.845 INFO 8904 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2019-08-25 22:09:22.846 DEBUG 8904 --- [ main] o.s.boot.SpringApplication : Loading source class com.example.demo.DemoApplication
2019-08-25 22:09:23.083 DEBUG 8904 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'file:/D:/Program%20Files%20(x86)/idea%20community/demo/target/classes/application.properties' (classpath:/application.properties)
2019-08-25 22:09:23.084 DEBUG 8904 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4b168fa9
2019-08-25 22:09:26.183 DEBUG 8904 --- [ main] .s.b.w.e.t.TomcatServletWebServerFactory : Code archive: D:\eclipse\apache-maven-3.6.1\repo\org\springframework\boot\spring-boot\2.1.7.RELEASE\spring-boot-2.1.7.RELEASE.jar
2019-08-25 22:09:26.183 DEBUG 8904 --- [ main] .s.b.w.e.t.TomcatServletWebServerFactory : Code archive: D:\eclipse\apache-maven-3.6.1\repo\org\springframework\boot\spring-boot\2.1.7.RELEASE\spring-boot-2.1.7.RELEASE.jar
2019-08-25 22:09:26.183 DEBUG 8904 --- [ main] .s.b.w.e.t.TomcatServletWebServerFactory : None of the document roots [src/main/webapp, public, static] point to a directory and will be ignored.
2019-08-25 22:09:26.245 INFO 8904 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-08-25 22:09:26.325 INFO 8904 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-08-25 22:09:26.326 INFO 8904 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-08-25 22:09:26.797 INFO 8904 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-08-25 22:09:26.797 DEBUG 8904 --- [ main] o.s.web.context.ContextLoader : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2019-08-25 22:09:26.797 INFO 8904 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3713 ms
2019-08-25 22:09:26.860 DEBUG 8904 --- [ main] o.s.b.w.s.ServletContextInitializerBeans : Mapping filters: springSecurityFilterChain urls=[/*], characterEncodingFilter urls=[/*], hiddenHttpMethodFilter urls=[/*], formContentFilter urls=[/*], requestContextFilter urls=[/*]
2019-08-25 22:09:26.861 DEBUG 8904 --- [ main] o.s.b.w.s.ServletContextInitializerBeans : Mapping servlets: dispatcherServlet urls=[/]
2019-08-25 22:09:26.914 DEBUG 8904 --- [ main] o.s.b.w.s.f.OrderedRequestContextFilter : Filter 'requestContextFilter' configured for use
2019-08-25 22:09:26.916 DEBUG 8904 --- [ main] .s.b.w.s.f.OrderedHiddenHttpMethodFilter : Filter 'hiddenHttpMethodFilter' configured for use
2019-08-25 22:09:26.919 DEBUG 8904 --- [ main] s.b.w.s.f.OrderedCharacterEncodingFilter : Filter 'characterEncodingFilter' configured for use
2019-08-25 22:09:26.920 DEBUG 8904 --- [ main] .DelegatingFilterProxyRegistrationBean$1 : Filter 'springSecurityFilterChain' configured for use
2019-08-25 22:09:26.921 DEBUG 8904 --- [ main] o.s.b.w.s.f.OrderedFormContentFilter : Filter 'formContentFilter' configured for use
2019-08-25 22:09:27.507 INFO 8904 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@5432050b, org.springframework.security.web.context.SecurityContextPersistenceFilter@5f78de22, org.springframework.security.web.header.HeaderWriterFilter@69eb86b4, org.springframework.security.web.csrf.CsrfFilter@3122b117, org.springframework.security.web.authentication.logout.LogoutFilter@56781d96, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@1136b469, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@4d8539de, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@375b5b7f, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@75f2099, org.springframework.security.web.session.SessionManagementFilter@5bb8f9e2, org.springframework.security.web.access.ExceptionTranslationFilter@74fef3f7, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@35c09b94]
2019-08-25 22:09:27.615 DEBUG 8904 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Patterns [/**/favicon.ico] in 'faviconHandlerMapping'
2019-08-25 22:09:27.834 INFO 8904 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-08-25 22:09:28.022 DEBUG 8904 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
2019-08-25 22:09:28.176 DEBUG 8904 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : 5 mappings in 'requestMappingHandlerMapping'
2019-08-25 22:09:28.186 INFO 8904 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index
2019-08-25 22:09:28.221 DEBUG 8904 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
2019-08-25 22:09:28.251 DEBUG 8904 --- [ main] .m.m.a.ExceptionHandlerExceptionResolver : ControllerAdvice beans: 1 @ExceptionHandler, 1 ResponseBodyAdvice
2019-08-25 22:09:28.613 DEBUG 8904 --- [ main] ConditionEvaluationReportLoggingListener :
============================
CONDITIONS EVALUATION REPORT
============================
/*以下更多省略*/
以文件形式输出
我们在ssm配置的时候也会配置log.properties文件,里面设置吧日志信息输出到某个自定义的文件上,而在springboot中根本不需要单独新建log.properties文件,只需要在application.properties中配置就行
- springboot中有 logging.file 和 logging.path用来输出日志信息
- 1、logging.file:可以设置绝对路径或者是相对路径
- 如
logging.file = springboot.log
- 2、logging.path :设置目录,启动项目后会在该目录下创建一个spring.log文件
- 如
logging.path = D:/Program Files (x86)/spring_boot_log/my.log
日志级别控制
TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
springboot默认级别是INFO,可以在 application.properties 中设置级别
比如:logging.level.<logger-name>=<level>
logging日志模块
可以通过在Maven添加logging依赖,如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
添加依赖之后即可以做到“开箱即用”,不需要做任何多余的配置