• Spring Boot2.0之整合log4j


    传统方式打印日志比较复杂, 每次打印需要定义全局变量

    private static final Logger logger = LoggerFactory.getLogger(SjpController.class)  

    这里我将介绍的是用lombok去简化的方式,只需要加个注解~~~~

    1、首先pom文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>ErrorCatch</groupId>
      <artifactId>com.toov5.ErrorCatch</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
      <parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.0.0.RELEASE</version>
    	</parent>
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<!-- springboot-log4j -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-log4j</artifactId>
    			<version>1.3.8.RELEASE</version>
    		</dependency>
    		<dependency>
    	<groupId>org.projectlombok</groupId>
    	<artifactId>lombok</artifactId>
    </dependency>
    
    	</dependencies> 
    
      
    </project>
    

    2、添加相关依赖到logj.properties文件

    #log4j.rootLogger=CONSOLE,info,error,DEBUG
    log4j.rootLogger=info,error,CONSOLE,DEBUG
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender     
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout     
    log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n     
    log4j.logger.info=info
    log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.info.layout=org.apache.log4j.PatternLayout     
    log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
    log4j.appender.info.datePattern='.'yyyy-MM-dd
    log4j.appender.info.Threshold = info   
    log4j.appender.info.append=true   
    #log4j.appender.info.File=/home/admin/pms-api-services/logs/info/api_services_info
    log4j.appender.info.File=/Users/dddd/Documents/testspace/pms-api-services/logs/info/api_services_info
    log4j.logger.error=error  
    log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.error.layout=org.apache.log4j.PatternLayout     
    log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
    log4j.appender.error.datePattern='.'yyyy-MM-dd
    log4j.appender.error.Threshold = error   
    log4j.appender.error.append=true   
    #log4j.appender.error.File=/home/admin/pms-api-services/logs/error/api_services_error
    log4j.appender.error.File=/Users/dddd/Documents/testspace/pms-api-services/logs/error/api_services_error
    log4j.logger.DEBUG=DEBUG
    log4j.appender.DEBUG=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.DEBUG.layout=org.apache.log4j.PatternLayout     
    log4j.appender.DEBUG.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
    log4j.appender.DEBUG.datePattern='.'yyyy-MM-dd
    log4j.appender.DEBUG.Threshold = DEBUG   
    log4j.appender.DEBUG.append=true   
    #log4j.appender.DEBUG.File=/home/admin/pms-api-services/logs/debug/api_services_debug
    log4j.appender.DEBUG.File=/Users/dddd/Documents/testspace/pms-api-services/logs/debug/api_services_debug
    
    log4ju4EE3u7801
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
    

     后端Java类代码:

     传统型:

    package com.toov5.ErrorCatch;
    
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    
    @RestController //以后经常做微服务异常捕获,要经常用到这个注解
    public class ErrorCatch {
        private static final Logger logger = LoggerFactory.getLogger(ErrorCatch.class);
        @RequestMapping("/getUser")
        public String getUser(int i ){
            int j = 1/i;    
            logger.info("日志打印记录成功");
            return "返回"+j;
        }
         
    }

    访问:

    控制台:

    lombok+log4j型

     很简单~

     

    访问效果一样:

  • 相关阅读:
    python之路---类
    python之路---走台阶(递归)
    python之路---递归函数
    python之路---filter、map、lambda函数
    python之路---封装
    python07--抽象数据类型和python类(P34)
    python06--计算机内存结构与存储管理(P27)
    匹配算法大纲
    并查集及其优化
    Hash技术初涉
  • 原文地址:https://www.cnblogs.com/toov5/p/9817386.html
Copyright © 2020-2023  润新知