log Log: Print a log to the console, files, log files, partitions, print the error log to a file

springboot default will load the classpath: logback-spring.xml file
if you need to customize the file name, configuration options in application.properties in logging.config can
create logback-spring.xml file in src / main / resources, as follows

To print a log with a class Lombok (simple, convenient than the conventional kind of printing log configuration)
is added in the required log print log based annotation
@ Slf4j is import lombok.extern.slf4j.Slf4j; packets, error packets do not lead

In places like the output to be used

原本要输出的内容System.out.println(this.getId());用下面打印日志的方式代替

// {}就是占位符,会将this.getId放入{}中打印输出,可以添加多个,例如第二句,“就是get到的id”这句话会添加到第二个{}后面
log.info("info级别的日志:{}",this.getId());
log.info("info级别的日志:{}","我的id是:{}",this.getId(),"就是get到的id");

Case

The journal printed to the console

<?xml version="1.0" encoding="UTF-8"?>
<!--configuration:根节点,以下3个属性都是根节点的配置,一般用默认参数即可-->
<!--scan(扫描): 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true-->
<!--scanPeriod:多长时间扫描一次,默认60毫秒扫描一次-->
<!--debug:为true时打印出logback内部日志信息,实时查看logback运行状态,默认是false-->
<!--日志分几个级别:debug(输出调试信息,一般情况应用程序不会使用debug日志)、info(输出主要信息)、error(输出错误信息)-->
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!--property:子节点,定义一个变量-->
<!--name:LOG_PATH日志定义在哪个文件下-->
<property name="LOG_PATH" value="./logs"></property>
    <!--appender:指定日志输出的目的地,目的地可以是控制台、文件等-->
    <!--ConsoleAppender:控制台输出-->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!--layout:布局,输出的日志布局是什么,负责把事件转换成字符串,格式化的日志信息输出-->
        <layout>
            <!--%d:日期,代表几月几日输出的-->
            <!--%thread:线程名,web工程都是多线程-->
            <!--%5-level:代表输出级别;级别从左显示5个字符宽度-->
            <!--%logger{35}:打出的类名最长35个字符,否则按照句点分割-->
            <!--%msg:日志消息-->
            <!--%n:换行符-->
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n
            </pattern>
        </layout>
    </appender>
    <!--root:根节点设置日志级别,设置使用哪些根节点,使用的是appender节点name的值-->
    <root level="info">
        <appender-ref ref="STDOUT"></appender-ref>
    </root>
</configuration>

Log output in the same order expression

%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n

Here Insert Picture Description

The log Print to File

<?xml version="1.0" encoding="UTF-8"?>
 <configuration debug="false">
 <appender name="FILE" class="ch.qos.logback.core.FileAppender">
  <file>${LOG_PATH}/testFile.log</file>
 <append>true</append>
 <encoder>
 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} -
%msg%n</pattern>
 </encoder>
 </appender>
 <root level="info">
 <appender-ref ref="FILE" />
 </root>
 </configuration>

The following child nodes:

<file>:被写入的文件名,可以是相对目录,也可以是绝对目录,如果上级目录不存在会自动创建,没有默认值。
<append>:如果是 true,日志被追加到文件结尾,如果是 false,清空现存文件,默认是true。
<encoder>:对记录事件进行格式化。
<prudent>:如果是 true,日志会被安全的写入文件,即使其他的FileAppender也在向此文件做写入操作,效率低,默认是 false。

After finished, under logs document root folder will have a print format of the testFile.log file, the file is

 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} -
%msg%n</pattern>

Here Insert Picture Description

Automatic backup RollingFileAppender

Scroll log file, logging to the specified file first,
when the meet a certain criteria (date, log size, days of storage), logging into other files.

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<property name="LOG_PATH" value="./logs"></property>
<appender name="MAXDATEFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!--日志文件输出的文件名-->
        <fileNamePattern>${LOG_PATH}/logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
        <!--日志文件保留天数-->
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} -
            %msg%n</pattern>
    </encoder>
    <!--当文件大于10MB时,生成新的日志文件-->
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <MaxFileSize>10MB</MaxFileSize>
    </triggeringPolicy>
</appender>
<!--root:根节点设置日志级别,设置使用哪些根节点,使用的是appender节点-->
<root level="info">
    <appender-ref ref="MAXDATEFILE"></appender-ref>
</root>
</configuration>

Print Error Log

<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
 <FileNamePattern>${LOG_PATH}/error.%d{yyyy-MM-dd}.log</FileNamePattern>
</rollingPolicy>
<layout>
<pattern>[%date{yyyy-MM-dd HH:mm:ss}] [%-5level] [%logger:%line] --%mdc{client} %msg%n</pattern>
</layout>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
 <level>ERROR</level>
 <onMatch>ACCEPT</onMatch>
 <onMismatch>DENY</onMismatch>
</filter>
</appender>

Print Error Log to add the output level of error in the log output:

log.error("这是错误日志");

Log Level

info级别: info error
debug级别:info debug error
error级别:error
trace级别:trace info debug error

Guess you like

Origin blog.csdn.net/qq_41767337/article/details/89406239
log