SpringBoot應用與原理之集成Log4j2

一 本章概述

SpringBoot應用替換默認的Logback日之框架,採用Log4j2實現

二 SpringBoot應用集成Log4j2

1 替換默認的logback

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2 加入基於SpringBoot的Log4j2 Starter

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

3 配置log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--設置log4j2的自身log級別爲warn -->
<configuration status="warn">
    <properties>

        <Property name="app_name">springboot-web</Property>
        <Property name="log_path">logs/${app_name}</Property>

    </properties>
    <appenders>
        <console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%d][%t][%p][%l] %m%n" />
        </console>

        <RollingFile name="RollingFileInfo" fileName="${log_path}/info.log"
                     filePattern="${log_path}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz">
            <Filters>
                <ThresholdFilter level="INFO" />
                <ThresholdFilter level="WARN" onMatch="DENY"
                                 onMismatch="NEUTRAL" />
            </Filters>
            <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
            <Policies>
                <!-- 歸檔每天的文件 -->
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                <!-- 限制單個文件大小 -->
                <SizeBasedTriggeringPolicy size="2 MB" />
            </Policies>
            <!-- 限制每天文件個數 -->
            <DefaultRolloverStrategy compressionLevel="0" max="10"/>
        </RollingFile>

        <RollingFile name="RollingFileWarn" fileName="${log_path}/warn.log"
                     filePattern="${log_path}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz">
            <Filters>
                <ThresholdFilter level="WARN" />
                <ThresholdFilter level="ERROR" onMatch="DENY"
                                 onMismatch="NEUTRAL" />
            </Filters>
            <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
            <Policies>
                <!-- 歸檔每天的文件 -->
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                <!-- 限制單個文件大小 -->
                <SizeBasedTriggeringPolicy size="2 MB" />
            </Policies>
            <!-- 限制每天文件個數 -->
            <DefaultRolloverStrategy compressionLevel="0" max="10"/>
        </RollingFile>

        <RollingFile name="RollingFileError" fileName="${log_path}/error.log"
                     filePattern="${log_path}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz">
            <ThresholdFilter level="ERROR" />
            <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
            <Policies>
                <!-- 歸檔每天的文件 -->
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                <!-- 限制單個文件大小 -->
                <SizeBasedTriggeringPolicy size="2 MB" />
            </Policies>
            <!-- 限制每天文件個數 -->
            <DefaultRolloverStrategy compressionLevel="0" max="10"/>
        </RollingFile>

        <!-- 配置mongdb appender -->
    </appenders>

    <loggers>
        <!--過濾掉spring和hibernate的一些無用的debug信息 -->

        <root level="info">
            <appender-ref ref="Console" />
            <appender-ref ref="RollingFileInfo" />
            <appender-ref ref="RollingFileWarn" />
            <appender-ref ref="RollingFileError" />
            <!-- 輸出日誌到mongodb -->
        </root>

    </loggers>

</configuration>

4 配置appliaction.properties

logging.config=classpath:log4j2.xml

三 Log4j2的應用

package com.ekeyfund.framework.springboot.web.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * IndexController
 *
 * @author tony [email protected]
 * @create 2017-09-14-下午4:51
 * @see
 * @since JDK1.8u141
 */
@RestController
public class IndexController {


    private static final Logger logger = LogManager.getLogger(IndexController.class);

    @GetMapping("/index")
    public String index(){

        for(int i=0;i<10_0000;i++){
            logger.info("info execute index method");
            logger.warn("warn execute index method");
            logger.error("error execute index method");

        }


        return "My First SpringBoot Application";
    }
}

啓動應用會發現日誌會根據不同的級別存儲在不同的文件,當日志文件大小超過2M以後會分多個文件壓縮存儲,生產環境的日誌文件大小建議調整爲20-50MB。

重要說明:
移動終端更好的閱讀方案,關注個人微信公衆號: ittimeline,CSDN文章內容會實時同步到該公衆號中。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章