spring cloud log configuration

Keywords: Spring xml log4j REST

Spring Cloud log configuration can use Log4j or logback, but logback is officially recommended. Logback is produced by the author of log4j, which is used to replace log4j. We also use logback to process logs

 

It is very simple to configure logback in the spring boot project.

The first step is to confirm that there are logback packages in dependency packages. I've introduced spring-cloud-starter-sleuth packages, which have integrated logback packages.

 

Next, we add the configuration information to the configuration file. Add the following configuration log file location in bootstrap.properties.

logging.file=logs/${spring.application.name}.log

 

It's that simple. Let's run the program directly and generate the relevant log files (logs / application name. log) in the project directory.

Next we can control the log level, which is info as shown in the figure below. The log level can be configured according to our own needs.

logging.level.org.springframework.boot=info

 

If this default log configuration does not meet the requirements, we can configure logback-spring.xml, which is read by default when the logback-spring.xml project starts. In this xml, we can configure it according to our own needs

The general function of my logback-spring.xml configuration is to record logs. When logs are larger than 10MB, they are automatically distributed and time is added to the file name.

The ${LOG_FILE} is the value of logging.file in read properties

<configuration>
    <springProperty scope="context" name="app_name" source="spring.application.name" />
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
    <jmxConfigurator/>
        <appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
         <file>${LOG_FILE}</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                  <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i</fileNamePattern>
                <MaxFileSize>10MB</MaxFileSize>
            </rollingPolicy>
               <encoder>
            <pattern>%d{yyyy-MM-dd} %d{HH:mm:ss.SSS}  [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>

        </appender>
        <root level="INFO">
            <appender-ref ref="rollingFile"/>
            <appender-ref ref="CONSOLE"/>
        </root>
</configuration>

In addition, there are some other information.

<configuration>
    <springProperty scope="context" name="app_name" source="spring.application.name" />
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-/logs}}/app.log}"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
    <springProfile name="default">
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>
        </root>
    </springProfile>
    <jmxConfigurator/>
    <springProfile name="test,prod">
        <appender name="rollingFile"
                  class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${LOG_PATH}/${LOG_FILE}</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                <fileNamePattern>${LOG_PATH}/${LOG_FILE}.%d{yyyy-MM-dd}.%i</fileNamePattern>
                <MaxFileSize>10MB</MaxFileSize>
            </rollingPolicy>
            <!--<encoder class="net.logstash.logback.encoder.LogstashEncoder">-->
                <!--<includeCallerInfo>true</includeCallerInfo>-->
            <!--</encoder>-->
            <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
                <providers>
                    <timestamp>
                        <timeZone>UTC</timeZone>
                    </timestamp>
                    <pattern>
                        <pattern>
                            {
                            "severity": "%level",
                            "service": "${app_name:-}",
                            "trace": "%X{X-B3-TraceId:-}",
                            "span": "%X{X-B3-SpanId:-}",
                            "exportable": "%X{X-Span-Export:-}",
                            "pid": "${PID:-}",
                            "thread": "%thread",
                            "class": "%logger{40}",
                            "rest": "%message"
                            }
                        </pattern>
                    </pattern>
                </providers>
            </encoder>
        </appender>
        <root level="INFO">
            <appender-ref ref="rollingFile"/>
            <appender-ref ref="CONSOLE"/>
        </root>
    </springProfile>
</configuration>

Posted by RickChase on Sat, 29 Jun 2019 18:43:53 -0700