Set spring-boot logging and spring-bootlogging

Source: Internet
Author: User
Tags syslog

Set spring-boot logging and spring-bootlogging

 

By default, spring-boot uses logback to record logger and spring-boot packages.org.springframework.boot.logging.logbackThere are some configuration files under the path, which will be used by defaultbase.xml, Which outputs logs to the console and files. Its content is:

<included>    <include resource="org/springframework/boot/logging/logback/defaults.xml" />    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />    <root level="INFO">        <appender-ref ref="CONSOLE" />        <appender-ref ref="FILE" />    </root></included>

If logging is set for the output to a file. this value is used as the file name if logging is set. the path uses logging. path/spring. log is used as the file name. If none of them are set, it is put into the temporary file.

So the question is, if you want to write a file, will it not be too large?
At this time, you can seebase.xmlInsideincludeNoworg/springframework/boot/logging/logback/file-appender.xml

Let's take a look.org/springframework/boot/logging/logback/file-appender.xmlWhat is in it?

<included>    <appender name="FILE"        class="ch.qos.logback.core.rolling.RollingFileAppender">        <encoder>            <pattern>${FILE_LOG_PATTERN}</pattern>        </encoder>        <file>${LOG_FILE}</file>        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">            <fileNamePattern>${LOG_FILE}.%i</fileNamePattern>        </rollingPolicy>        <triggeringPolicy            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">            <MaxFileSize>10MB</MaxFileSize>        </triggeringPolicy>    </appender></included>

Yes, the default configuration containsSizeBasedTriggeringPolicyTo indicate the file size. When each file reaches 10 MB, it is re-opened and named${LOG_FILE}.%iIn this way, the log file is too large.

Some people may want to ask, these are all default configurations. If I don't want to use the default configuration, what should I do if I want to cut logs every day?

You can createlogback-spring.xmlAnd provide the desired configuration. Here, a simple configuration is provided. The log file can be cut by day.

<?xml version="1.0" encoding="UTF-8"?> <configuration>    <include resource="org/springframework/boot/logging/logback/defaults.xml" />    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />     <appender name="TIME_FILE"              class="ch.qos.logback.core.rolling.RollingFileAppender">        <encoder>            <pattern>${FILE_LOG_PATTERN}</pattern>        </encoder>        <file>${LOG_FILE}</file>        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">            <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i</fileNamePattern>            <maxHistory>365</maxHistory>            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">                <maxFileSize>100MB</maxFileSize>            </timeBasedFileNamingAndTriggeringPolicy>        </rollingPolicy>     </appender>     <root level="INFO">        <appender-ref ref="CONSOLE" />        <appender-ref ref="TIME_FILE" />     </root></configuration>

Above%d{yyyy-MM-dd}Indicates that the name of the named file is suffixed with a date after the original name,<maxFileSize>100MB</maxFileSize>The size of each file is 100 MB. In this way, log files are cut by date and saved.

Therefore, if you want to customize the log format, you can write it by yourself.logback-spring.xmlThis file is defined by yourself.

Finally, in the configuration fileappication.yamlSet the file name and log level.

spring:    application:        name: spring-boot-logginglogging:    file: ./logs/spring-boot-logging.log    level:        com.dragon.study.spring.boot: DEBUG

If you wantlogback-spring.xmlCustomize some configuration files, which can be used

    <springProperty scope="context" name="SYSLOG_HOST" source="logging.syslog.host" defaultValue="syslog.hostname" />    <springProperty scope="context" name="SYSLOG_FACILITY" source="logging.syslog.facility" defaultValue="LOCAL7"/>    <appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">        <syslogHost>${SYSLOG_HOST}</syslogHost>        <facility>${SYSLOG_FACILITY}</facility>    </appender>

Thenappication.yamlAdd to the configuration file

logging.syslog.facility: syslog.change.hostnamelogging.syslog.facility: LOCAL1

Configure to replace the default value

PS: If you encounter a pitfall, you just want to turn off the logs under a certain package.

logging:    level:        com.dragon.study.spring.boot: OFF

Because OFF is the key word of yaml, writing above does not work.
So if you want to disable the logs under a certain package, the correct method is to OFF and put a single quotation mark on it.

logging:    file: ./logs/spring-boot-logging.log    level:        com.dragon.study.spring.boot: 'OFF'

 

Finally, if you think that what you are talking about can help you and want to learn more in detail, you are welcome to join group 632109190 for discussion and study.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.