Foreword
There are several logging framework java web, such as: logback, log4j, log4j2 (slj4f not a logging framework, which is equivalent to defined specifications to achieve this standard logging framework can be called with slj4f). Which should make logback highest performance, and it also logback springboot default log to use, so Benpian will explain in detail logback logging configuration.
Data in this chapter as follows:
- Constitute logback profile
- How to log output to a file
- How by time, by size segmentation log
- How to make a log file only one log level
- How to specify the next packet logging output to the specified log file
Simple to use
If the log without complex configuration, perform a simple set the log level of print, direct print mode can be re-configured application.yml.
By default, Spring Boot info level log output to the console, not to the log file, and can not be complicated configurations.
Print to file
You want to log into the output file, by the following two configurations:
logging:
# 配置输出额日志文件名,可以带路径
# file: out.log
# 配置日志存放路径,日志文件名为:spring.log
path: ./log
file:
# 设置日志文件大小
max-size: 10MB
Note : file and path are not configured the same time, if both the configuration path
does not take effect.
Print level control
Log printing by configuring the control level, in the following format:
logging.level.*=TRACE/DEBUG/INFO/...
* Logger can be a package name or name, as follows:
logging:
level:
# root日志以WARN级别输出
root: info
# 此包下所有class以DEBUG级别输出
com.example.log_demo.log1: warn
logback detailed configuration
Next, how to configure log print by separate xml configuration file. Although springboot is to destroy xml, but some complex functions still have to write xml. To application.yml after use xml configuration removed, to avoid conflicts.
Depending on the logging system, in accordance with the rules of the organization designated profile name, and placed under the resources directory can be automatically loaded spring boot:
- Logback:logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy
- Log4j: log4j-spring.properties, log4j-spring.xml, log4j.properties, log4j.xml
- Log4j2: log4j2-spring.xml, log4j2.xml
- JDK (Java Util Logging): logging.properties
Want to customize the file name can be configured: logging.config
specify the configuration file name:
logging.config=classpath:logging-config.xml
Spring Boot official recommended to use the file name with -spring as you log configuration (such as the use logback-spring.xml, rather than logback.xml), named logback-spring.xml log configuration file, spring boot can be add some spring boot-specific configuration items (mentioned below).
Composition logback profile
Root <configuration>
has five sub-nodes below to introduced one by one.
<root>
node
Required root node is a node, is used to specify the most basic log output level, only one level property for setting printing level, optional follows: TRACE
, DEBUG
, INFO
, WARN
, ERROR
, ALL
, OFF
.
root node may contain 0 or more elements will appender
be added. as follows:
<root level="debug">
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
appender is also one of the child node, as will be described later.
<contextName>
node
Context name is provided, as a default default
, can %contextName
be printed context name, generally do not use this property.
<property>
node
It is used to define a variable, easy to use. There are two attributes: name, value. After you define a variable, you can use ${}
to use variables. as follows:
<property name="path" value="./log"/>
<property name="appname" value="app"/>
<appender>
node
appender used to format the log output node, the most important. There are two attributes:
- name: The name of this appender
- class: Specifies output policy, there are usually two: console output, file output
By the following example to illustrate how this use:
- Output to the console / log output time
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<!--设置存储路径变量-->
<property name="LOG_HOME" value="./log"/>
<!--控制台输出appender-->
<appender name="console" 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} - %msg%n</pattern>
<!--设置编码-->
<charset>UTF-8</charset>
</encoder>
</appender>
<!--文件输出,时间窗口滚动-->
<appender name="timeFileOutput" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--日志名,指定最新的文件名,其他文件名使用FileNamePattern -->
<File>${LOG_HOME}/timeFile/out.log</File>
<!--文件滚动模式-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件名,可设置文件类型为gz,开启文件压缩-->
<FileNamePattern>${LOG_HOME}/timeFile/info.%d{yyyy-MM-dd}.%i.log.gz</FileNamePattern>
<!--日志文件保留天数-->
<MaxHistory>30</MaxHistory>
<!--按大小分割同一天的-->
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</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} - %msg%n</pattern>
<!--设置编码-->
<charset>UTF-8</charset>
</encoder>
</appender>
<!--指定基础的日志输出级别-->
<root level="INFO">
<!--appender将会添加到这个loger-->
<appender-ref ref="console"/>
<appender-ref ref="timeFileOutput"/>
</root>
</configuration>
- Level output only a single set
Provided the appender, filter child nodes, then filtered on this default level, arranged onMatch, onMismatch level can be achieved only output a single
<appender ...>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<!--接受匹配-->
<onMatch>ACCEPT</onMatch>
<!--拒绝不匹配的-->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<logger>
node
Structure
This node is used to set a particular package or class level of a print log, and specifies <appender>
, there are the following three properties:
- name: must. Loger to specify a package bound by this or a specific class
- level: Optional. Set the print level. The default is the root level.
- addtivity: Optional. Whether print information passed to the higher loger (that is, root node). The default is true.
Use example:
- No level is specified, do not specify the appender
<!-- 控制com.example.service下类的打印,使用root的level和appender -->
<logger name="com.example.service"/>
2. Specify the level is not specified appender
<!-- 控制com.example.service下类的打印,使用root的appender打印warn级别日志 -->
<logger name="com.example.service" level="WARN"/>
3. Specify the level specified appender
<!-- 控制com.example.service下类的打印,使用console打印warn级别日志 -->
<!-- 设置addtivity是因为这里已经指定了appender,如果再向上传递就会被root下的appender再次打印 -->
<logger name="com.example.service" level="WARN" addtivity="false">
<appender-ref ref="console">
</logger>
You will be able to log in the specified packet to the specified print file by specifying appender.
Multiple Environments log output
By the end of the file name setting -spring, separable environment configuration logger, examples are as follows:
<configuration>
<!-- 测试环境+开发环境. 多个使用逗号隔开. -->
<springProfile name="test,dev">
<logger name="com.example.demo.controller" level="DEBUG" additivity="false">
<appender-ref ref="console"/>
</logger>
</springProfile>
<!-- 生产环境. -->
<springProfile name="prod">
<logger name="com.example.demo" level="INFO" additivity="false">
<appender-ref ref="timeFileOutput"/>
</logger>
</springProfile>
</configuration>
By configuring spring.profiles.active
can be done to switch print settings above logger
End
Print log can be very complex, here only shows that the common logback configuration, see the code: GitHub
Original article published on: http://www.tapme.top/blog/detail/2019-03-21-10-38