spring boot 日志的使用

Posted zhenghuasheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 日志的使用相关的知识,希望对你有一定的参考价值。

Spring Boot支持 Java Utile Logging、Log4J、Log4J2和Logback作为日志框架,无论使用哪种框架,Spring Boot都为当前使用日志框架及文件输出做好了配置。
默认情况下,Spring Boot使用LogBack作为默认日志框架,输出格式的文件是logback.cml

application.properties配置文件中加入配置

###日志文件####
logging.file=/export/log

###配置日志文件,按照格式loggin.level.包名=级别###
logging.level.org.springframework.web=DEBUG

###或者直接配置根级别###
logging.level.root=DEBUG

spring boot 中使用log4j
在classpa中加入log4j.properties配置文件

log4j.rootLogger=INFO,ServerDailyRollingFile,stdout
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ServerDailyRollingFile.DatePattern=’.’yyyy-MM-dd_HH
log4j.appender.ServerDailyRollingFile.File=log/log4j.log
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n
log4j.appender.ServerDailyRollingFile.Append=true

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%F:%L] %-5p :: %m%n

加入log4j的依赖

log4j
log4j

spring boot 中使用slf4j

首先加入log4j+slf4j的jar依赖

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
</dependency>
<!--日志输出使用slf4j,与logback为日志实现类,只能同时出现一个-->
 <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>1.7.21</version>
</dependency>

<!--排除多余依赖-->

   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>

                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-over-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
     </dependency>

# LOGGING
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.
logging.file= # Log file name. For instance `myapp.log`
logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`
logging.path= # Location of the log file. For instance `/var/log`
logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.
logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.
logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.
logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.

相关配置spring官方文档和spring boot的官方文档有详细描述
http://spring.cndocs.tk/overview.html#overview-logging-log4j

http://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/htmlsingle/#howto-configure-log4j-for-logging

以上是关于spring boot 日志的使用的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot - 日志记录

spring-boot中logback日志配置

Spring Boot 中使用Spring Aop实现日志记录功能

spring boot (4) 使用log4 打印日志

Spring Boot 日志详解

spring boot怎么配日志