Location>code7788 >text

SpringBoot multi-environment logging configuration

Popularity:282 ℃/2024-08-26 17:08:02

SpringBoot multi-environment logging configuration

SpringBoot uses the LogBack logging system by default.

By default, SpringBoot project logs will only be entered in the console.

If you want to query the history log can not be found, we need a logging system to manage logs in a unified way.

Generally formal projects will have a separate logging system that stores logged operations in a database.

The first way is

existmaybeFile Add

Simultaneous setuprespond in singingconjunction used express contrast with a previous sentence or clauseIt will be ignored.

# Set the specific name of the log file
=. /

# Or set the directory of the log file
= /logfile/

More detailed configuration can also be written in this configuration file

Second way Use configuration files

1. Introducing dependencies

The slf4j-api was introduced to simplify operations using the @Slf4j annotation.

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
    <groupId></groupId>
    <artifactId>lombok</artifactId>
</dependency>

2、Multi-environment configuration

image

3. Create logback xml

Under resource, create the

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <springProperty scope="context" name="applicationName"
                    source="" defaultValue="spring-restful-api"/>

    <!-- Maximum size of a single log file,sizes -->
    <springProperty scope="context" name="maxFileSize"
                    source="-size" defaultValue="10MB"/>
    <!-- Length of log retention (sky) -->
    <springProperty scope="context" name="maxHistory"
                    source="-history" defaultValue="180"/>
    <!-- Log file path -->
    <springProperty scope="context" name="logDir"
                    source="" defaultValue="./logs"/>

    <contextName>${applicationName}</contextName>

    <!-- Color Log -->
    <!-- Color Log依赖的渲染类 -->
    <conversionRule conversionWord="clr" converterClass=""/>
    <conversionRule conversionWord="wex"
                    converterClass=""/>

    <!-- Color Log格式 -->
    <property name="CONSOLE_LOG_PATTERN"
              value="%clr(%d{yyyy-MM-dd HH:mm:}){faint} %clr(%5p) %clr(${PID:- }) %clr(---){faint} %clr([%10t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wex"/>
    <property name="FILE_LOG_PATTERN"
              value="%d{yyyy-MM-dd HH:mm:} %5p ${PID:- } --- [%t] %-40.40logger{39} : %m%n%wex"/>

    <!-- Console Output -->
    <appender name="STDOUT" class="">
        <encoder class="" charset="UTF-8">
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <!-- infolog (computing) appender  -->
    <appender name="INFO" class="">

        <!-- 正在记录的log (computing)文件的路径及文件名 -->
        <file>${logDir}/</file>
        <!-- 追加方式记录log (computing)默认是true log (computing)被追加到文件结尾 -->
        <append>true</append>

        <rollingPolicy class="">
            <!-- 按sky回滚 daily -->
            <!-- 归档的log (computing)文件的路径,%d{yyyy-MM-dd}Specify date format,%iSpecify Index -->
            <fileNamePattern>${logDir}/info/info-%d{yyyy-MM-dd}-%</fileNamePattern>
            <!-- log (computing)最大的历史 180sky -->
            <maxHistory>${maxHistory}</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="">
                <!-- maxFileSize:This is the size of the active file,The default value is10MB,This is set to20MB -->
                <maxFileSize>1MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>


        <encoder class="" charset="UTF-8">
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <filter class="">
            <!-- print onlyinfolog (computing) -->
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- DEBUGlog (computing) appender  -->
    <appender name="DEBUG" class="">

        <!-- 正在记录的log (computing)文件的路径及文件名 -->
        <file>${logDir}/</file>
        <!-- 追加方式记录log (computing)默认是true log (computing)被追加到文件结尾 -->
        <append>true</append>

        <rollingPolicy class="">
            <!-- 按sky回滚 daily -->
            <fileNamePattern>${logDir}/debug/debug-%d{yyyy-MM-dd}-%</fileNamePattern>
            <!-- log (computing)最大的历史 180sky -->
            <maxHistory>${maxHistory}</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="">
                <!-- maxFileSize:This is the size of the active file,The default value is10MB,This is set to20MB -->
                <maxFileSize>${maxFileSize}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>

        <encoder class="" charset="UTF-8">
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <filter class="">
            <!-- print onlyDEBUGlog (computing) -->
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- WARNlog (computing) appender  -->
    <appender name="WARN" class="">

        <!-- 正在记录的log (computing)文件的路径及文件名 -->
        <file>${logDir}/</file>
        <!-- 追加方式记录log (computing)默认是true log (computing)被追加到文件结尾 -->
        <append>true</append>

        <rollingPolicy class="">
            <!-- 按sky回滚 daily -->
            <fileNamePattern>${logDir}/warn/warn-%d{yyyy-MM-dd}-%</fileNamePattern>
            <!-- log (computing)最大的历史 180sky -->
            <maxHistory>${maxHistory}</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="">
                <!-- maxFileSize:This is the size of the active file,The default value is10MB,This is set to20MB -->
                <maxFileSize>${maxFileSize}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>


        <encoder class="" charset="UTF-8">
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <filter class="">
            <!-- print onlyWARNlog (computing) -->
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- error log (computing) appender  -->
    <appender name="ERROR" class="">

        <!-- 正在记录的log (computing)文件的路径及文件名 -->
        <file>${logDir}/</file>
        <!-- 追加方式记录log (computing)默认是true log (computing)被追加到文件结尾 -->
        <append>true</append>

        <rollingPolicy class="">
            <!-- 按sky回滚 daily -->
            <fileNamePattern>${logDir}/error/error-%d{yyyy-MM-dd}-%</fileNamePattern>
            <!-- log (computing)最大的历史 180sky -->
            <maxHistory>${maxHistory}</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="">
                <!-- maxFileSize:This is the size of the active file,The default value is10MB,This is set to20MB -->
                <maxFileSize>1MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>

        <encoder class="" charset="UTF-8">
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <filter class="">
            <!-- print only错误log (computing) -->
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!--  Development and test environments,log (computing)输出到控制台  -->
    <springProfile name="dev|test">
        <!--控制台和log (computing)文件输出step (of stairs)别-->
        <root level="INFO">
            <appender-ref ref="STDOUT"/>
        </root>
         <logger name="" level="debug"/> <!-- Development and test environments, 指定某包log (computing)为debugstep (of stairs) -->
    </springProfile>

    <!--  production environment,log (computing)输出到文件 -->
    <springProfile name="prod">
        <root level="INFO">
            <appender-ref ref="INFO"/>
            <appender-ref ref="ERROR"/>
            <appender-ref ref="DEBUG"/>
            <appender-ref ref="WARN"/>
        </root>
        <logger name="" level="warn"/> <!-- production environment, 指定某包log (computing)为warnstep (of stairs) -->
        <logger name="" level="info"/> <!-- Printing for a specific classinfolog (computing), for exampleapplicationPrompt after successful startup -->
    </springProfile>

</configuration>

account for


    <! --
        <logger> is used to set the level of log printing for a package or a specific class,
        and to specify <appender>. <logger> has only a name attribute, an optional level, and an optional appender>.
        an optional level and an optional addtivity attribute.
        name: Specifies the package or class to which the logger is bound.
        level: sets the print level, which is case-insensitive: TRACE, DEBUG, INFO, WARN, ERROR, ALL and OFF.
              There is also a special value INHERITED or synonymously NULL, which enforces the higher level.
              If this attribute is not set, then the current logger will inherit the parent level.
        addtivity: Whether to pass print information to the parent logger. Default is true.
        <logger name="" level="info"/>
        <logger name="" level="INFO"/>
    -->

    <! --
        When using mybatis, sql statements are printed only under debug, and here we have only configured info, so if you want to view the sql statements, there are two operations:
        The first is to change <root level="info"> to <root level="DEBUG"> so that the sql will be printed, but so that the log side will appear a lot of other messages!
        The second is a separate directory to the dao under the configuration of debug mode, the code is as follows, so that the configuration of the sql statement will be printed, the other is still the normal info level:
        ==debug =debug]
     -->

    <! --
        The root node is mandatory and is used to specify the most basic log output level, with only one level attribute
        level:Used to set the print level, case-insensitive: TRACE, DEBUG, INFO, WARN, ERROR, ALL and OFF, cannot be set to INHERITED or synonym NULL.
        Cannot be set to INHERITED or the synonym NULL, the default is DEBUG.
        Can contain zero or more elements identifying the appender to be added to the logger.
    -->

4. Use

image

Complete Strategy

/rwjnb/p/

Disadvantages (starting a project may report an error)

spring will give priority to loading the project or jar package and read the path (/workspace/), and then loaded after loading our log files, the project is deployed to run on the server will not generally be used to root the privileges of such a large number of users to execute the privileges of a small number of paths in the path may not have the operating privileges will result in the failure of the project startup.