SpringBoot使用log4j2

本文记录一下log4j2的使用
其实就三步即可
第一步:添加log4j2依赖,排除spring-boot-starter-logging依赖

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

        <!-- Remove the internal dependency log spring-boot-starter-logging  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

第二步:编写log4j2的配置文件,文件名为log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="60">
    <Properties>
        <!-- 设置应用的英文名称 -->
        <Property name="App">logs</Property>
        <!-- 设置日志目录 -->
        <Property name="logDir">logs</Property>
        <Property name="splitSize">10 MB</Property>
        <property name="pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level  %msg \t %logger{36} %F %M %L %n </property>
    </Properties>

    <Appenders>
        <!-- 输出控制台日志的配置 -->
        <Console name="console" target="SYSTEM_OUT">
            <!-- 记录info和warn级别信息 -->
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            <!-- 输出日志的格式 -->
            <PatternLayout pattern="${pattern}"/>
        </Console>

        <!-- 打印出所有的信息 -->
        <RollingFile name="info"
                     fileName="${logDir}/${App}-info.log"
                     filePattern="${logDir}/${App}-info-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout pattern="${pattern}"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="${splitSize}"/>
            </Policies>
            <Filters>
                <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <DefaultRolloverStrategy max="20"/>
        </RollingFile>

        <!-- 存储所有error信息 -->
        <RollingFile name="error"
                     fileName="${logDir}/${App}-error.log"
                     filePattern="${logDir}/${App}-error-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout pattern="${pattern}" />
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="${splitSize}"/>
            </Policies>
            <Filters>
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <DefaultRolloverStrategy max="20"/>
        </RollingFile>
    </Appenders>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="console"/>
            <AppenderRef ref="info"/>
            <AppenderRef ref="error"/>
        </Root>
    </Loggers>
</Configuration>

注意:log4j2.xml文件只需放在/src/main/resources文件夹下即可,不需要在application.yml文件中配置。
第三步:编写Log工具类,我就随便写一个仅供参考

package com.felix.firstdemo.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogUtil {
    private static final Logger LOGGING = LoggerFactory.getLogger("fist_demo_log");

    public static void debug(String message){
        LOGGING.debug(message);
    }

    public static void info(String message){
        LOGGING.info(message);
    }

    public static void warn(String message){
        LOGGING.warn(message);
    }

    public static void error(String message){
        LOGGING.error(message);
    }
}

所有步骤已完成,可以使用了,贴一个使用的接口的demo

package com.felix.firstdemo.controller;

import com.felix.firstdemo.utils.LogUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @GetMapping("/log")
    public String log(@RequestParam(name = "logType", required = false) String logType) {
        String result = "debug";
        if (!logType.isEmpty()) {
            result = logType;
        }

        String message="response is ; " + result;

        switch (logType.toLowerCase()) {
            case "debug":
                LogUtil.debug(message);
                break;
            case "info":
                LogUtil.info(message);
                break;
            case "warn":
                LogUtil.warn(message);
                break;
            case "error":
                LogUtil.error(message);
                break;
        }

        return result;
    }

}

运行程序,输入地址 /hello/log?logType=error
可以看到有日志输出:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值