栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 官方文档示例:(42)使用log4j2取代默认的日志系统Logback

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 官方文档示例:(42)使用log4j2取代默认的日志系统Logback

一、配置pom.xml排除默认的logback并加入log4j2依赖,
完整Pom如下:



    4.0.0

    cn.edu.tju
    springbootconfigtest3
    1.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.6
    
    
        
            org.springframework.boot
            spring-boot-starter-web

            
                
                                
                                    org.springframework.boot
                                    spring-boot-starter-logging
                                
            

        

        
            org.springframework.boot
            spring-boot-starter-validation
        


        
            mysql
            mysql-connector-java
            5.1.30
        

        
        
            org.springframework.boot
            spring-boot-starter-log4j2
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                

            
        
    




二、在resources目录新建log4j2.xml文件:


    
    
        
    

    
        
            
        


        
            
                %d %p %c{1.} [%t] %m%n
            
        
        
            
        

    

    
        
            
            
        
    



三、在代码中使用日志记录:

package cn.edu.tju.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class TestController2 {
    Logger logger= LoggerFactory.getLogger(TestController2.class);

    @RequestMapping("/test")
    public String getInfo(){
        logger.info("this is test info......");
        logger.error("this is test error......");
        return new Date().toString();
    }
}

四、调用接口,查看日志输出:


五、如果不想使用默认的log4j2配置文件名,则需要在application.proerties中通过logging.config来配置,配置的值为配置文件的具体路径

logging.config=classpath:log4j2.xml
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/860071.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号