Detailed explanation of springboot project log configuration file

Spring boot project specifies log configuration file

In a Spring Boot project, logging can be configured by specifying a logging configuration file in the application.propertiesor file.application.yml

1. Use application.propertiesfiles:

In application.properties, you can specify the log profile using the following properties:

logging.config=classpath:custom-logback.xml

The above configuration will tell Spring Boot to use the file located on the classpath custom-logback.xmlas the logging configuration file. You can change the file path to the path of your own logging configuration file.

2. Use application.ymlfiles:

In application.yml, you can specify the log profile using the following properties:

logging:
  config: classpath:custom-logback.xml

Same as the above application.propertiesconfiguration, this configuration will tell Spring Boot to use the file located on the classpath custom-logback.xmlas the logging configuration file.

Please ensure that the log configuration file is on the classpath or specify the correct file path if necessary.

Using the above configuration, you can specify a custom logging configuration file in your Spring Boot project to override the default logging configuration.

The difference between logback-spring.xml and logback.xml

Option One

When using Logback as a logging framework, logback-spring.xml and  logback.xml are two commonly used configuration files, they differ slightly in how they load and process configurations.

  1. logback-spring.xml

    • logback-spring.xml It is a Logback configuration file specially prepared for Spring Boot projects.
    • When using Spring Boot,  logback-spring.xml Spring Boot will automatically load and replace the default Logback configuration if the file exists.
    • logback-spring.xml${} Supports Spring Boot features and extensions, such as dynamic configuration  using Spring's property placeholders  .
    • When Spring Boot is loaded  , it will also perform automatic configuration of the log level, and  set the log level logback-spring.xml according to the configuration of the application  application.properties or  application.yml in  the application.logging.level.*
    • logback-spring.xml The loading order takes precedence over  logback.xml.
  2. logback.xml

    • logback.xml It is a general Logback configuration file, suitable for non-Spring Boot projects or projects that do not require Spring Boot features.
    • If the file exists  logback.xml , it will be loaded and used, ignoring any  logback-spring.xml.
    • logback.xml Spring's property placeholders cannot be used in the configuration because they will not be processed by Spring.
    • logback.xml Log levels and other related settings need to be manually configured and are not automatically affected by the application's other configuration files.

Summarize:

  • If you use a Spring Boot project, it is recommended to use  logback-spring.xml a configuration file to enjoy Spring Boot's automatic configuration and property placeholder features.
  • For non-Spring Boot projects or projects that do not require Spring Boot features,  logback.xml just use the configuration file.

It should be noted that whether they are  logback-spring.xml or  logback.xml, their configuration syntax and elements are the same and can be configured according to specific needs.

Option II

logback-spring.xml and  logback.xml are files used to configure the Logback logging framework. They have some differences in usage and loading mechanisms.

logback-spring.xml It is a specific Logback configuration file for Spring Framework applications. Its integration with Spring provides some additional functions and features. When a file exists  logback-spring.xml , Spring Boot applications will load it first. This specific file name is used to take advantage of Spring Boot's auto-configuration mechanism, which allows  logging  to be configured dynamically logback-spring.xml from an application's  application.properties or  file by using Spring property placeholders. application.ymlThis means that you can change the behavior of Logback logging at runtime through Spring's property configuration.

In contrast, logback.xml it is a general-purpose Logback configuration file used for non-Spring applications or applications that do not require integration with Spring. logback-spring.xml Logback attempts to load  the file when it does not exist  logback.xml . This file is typically used for standalone Java applications or other applications in non-Spring environments.

If you are using the Spring Boot framework, it is recommended to use  logback-spring.xml files to configure Logback first for better integration and dynamic configuration capabilities. If you are not using Spring Boot, or do not need to integrate with Spring, you can  logback.xml configure Logback using .

The spring boot project log configuration file specifies the log input directory

In a Spring Boot project, to specify the output directory for log files, you can use the following method:

1. Use application.propertiesthe configuration file:

In application.properties, you can use the following properties to specify the output directory for log files:

logging.file.path=/path/to/logs

Replace /path/to/logswith the path to the directory where you want the log files to be output.

2. Use application.ymlthe configuration file:

In application.yml, you can use the following properties to specify the output directory for log files:

logging:
  file:
    path: /path/to/logs

Again, /path/to/logsreplace with the path to the directory where you want the log files to be output.

Make sure that the directory you specify has the appropriate permissions so that the application can create and write log files in the directory.

With the above configuration, you can output log files to a specified directory instead of the default log directory.

Log file configuration example

Case number one

Configuration file logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <contextName>logback</contextName>
    <!--输出到控制台-->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!--按天生成日志-->
    <appender name="logFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Prudent>true</Prudent>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>
                applog/%d{yyyy-MM-dd}/%d{yyyy-MM-dd}.log
            </FileNamePattern>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} -%msg%n
            </Pattern>
        </layout>
    </appender>
    <!-- logger节点,可选节点,作用是指明具体的包或类的日志输出级别,以及要使用的<appender>(可以把<appender>理解为一个日志模板)。addtivity:非必写属性,是否向上级loger传递打印信息。默认是true-->
    <logger name="com.leshangju.project" additivity="false">
        <appender-ref ref="console"/>
        <appender-ref ref="logFile"/>
    </logger>

    <root level="info">
        <appender-ref ref="console"/>
        <appender-ref ref="logFile"/>
    </root>

</configuration>

Log generation directory display

Case 2

Configuration file logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" scanPeriod="60 seconds" debug="false">

    <springProperty scope="context" name="logPath" source="gjsp.logs.path" defaultValue="${user.home}/data/app/cars-gjsp/logs"/>

    <property name="APP_NAME" value="cars-gjsp"/>
    <property name="LOG_HOME" value="data/app/gjsp/logs/${APP_NAME}"/>
    <springProfile name="prod">
        <property name="LOG_HOME" value="/${logPath}/${APP_NAME}"/>
    </springProfile>

    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </layout>
    </appender>

    <appender name="appLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/${APP_NAME}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${APP_NAME}-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
            <MaxHistory>30</MaxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern>
        </layout>
    </appender>

    <logger name="org.springframework" level="info" additivity="false"/>

    <logger name="io.swagger" level="OFF"/>
    <logger name="springfox.documentation" level="OFF"/>
    <logger name="io.lettuce" level="info" additivity="false"/>
    <logger name="io.netty" level="info" additivity="false"/>
    <logger name="org.apache.http.wire" level="info" additivity="false"/>
    <logger name="jdbc.connection" level="OFF"/>
    <logger name="jdbc.resultset" level="OFF"/>
    <logger name="jdbc.resultsettable" level="OFF"/>
    <logger name="jdbc.audit" level="OFF"/>
    <logger name="jdbc.sqltiming" level="INFO"/>
    <logger name="jdbc.sqlonly" level="OFF"/>

    <logger name="org.hibernate.SQL" additivity="false">
        <level value="info"/>
        <appender-ref ref="console"/>
        <appender-ref ref="appLogAppender"/>
    </logger>

    <!-- 2. 输出SQL 的参数到控制台和文件-->
    <logger name="org.hibernate.type.descriptor.sql.BasicBinder" additivity="false">
        <level value="info"/>
        <appender-ref ref="console"/>
        <appender-ref ref="appLogAppender"/>
    </logger>

    <root level="info">
        <appender-ref ref="console"/>
        <appender-ref ref="appLogAppender"/>
    </root>
</configuration>

 Log generation directory display

 

Guess you like

Origin blog.csdn.net/m0_69057918/article/details/132389719