码迷,mamicode.com
首页 > 编程语言 > 详细

spring boot 配置 log4j2

时间:2019-12-30 19:22:30      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:resource   enc   artifact   follow   logger   int   官网文档   log4j   tar   

版本信息:

spring cloud 版本Greenwich.SR2
spring boot 版本2.1.8.RELEASE

官网文档:

** http://logging.apache.org/log4j/2.x/manual/configuration.html **

以下每个步骤不可缺失

  1. pom.xml配置
    需要排除spring-boot-starter自带的logback依赖,不然日志无法记录在日志文件里

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
         <exclusions>
             <!-- 排除自带的logback依赖 -->
             <exclusion>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-logging</artifactId>
             </exclusion>
         </exclusions>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-log4j2</artifactId>
     </dependency>
  2. log4j配置
    新建log4j.xml放在resources目录下
    设置console,InfoLog,ErrorLog的输出配置以及日志目录

    Configuration status="INFO" 设置的是console的输出级别

     <?xml version="1.0" encoding="UTF-8"?>
     <Configuration status="INFO">
         <Appenders>
             <!--添加一个控制台追加器-->
             <Console name="Console" target="SYSTEM_OUT" follow="true">
                 <PatternLayout>
                     <pattern>[%-5p] %d %c - %m%n</pattern>
                 </PatternLayout>
             </Console>
    
             <!-- info log -->
             <RollingFile name="InfoLog" fileName="/opt/logs/scm-warehouse/info.log"
                          filePattern="/opt/logs/scm-warehouse/info-%d{yyyy-MM-dd}.log">
                 <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level %class{36}.%M[%L] - %msg%xEx%n"/>  
                 <Policies>
                     <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
                 </Policies>
                 <Filters>
                     <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/> 
                     <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
                 </Filters> 
             </RollingFile>
    
             <!-- error log -->
             <RollingFile name="ErrorLog" fileName="/opt/logs/scm-warehouse/error.log"
                          filePattern="/opt/logs/scm-warehouse/error-%d{yyyy-MM-dd}.log">
                 <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%-5level] %class{36}.%M[%L] - %msg%xEx%n"/>  
                 <Policies>
                     <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
                 </Policies>
                 <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" />  
             </RollingFile>            
         </Appenders>
         <Loggers>
             <Root level="info">
                 <AppenderRef ref="Console" />
                 <AppenderRef ref="InfoLog" />
                 <AppenderRef ref="ErrorLog" />
             </Root>
         </Loggers>
     </Configuration>
  3. yml配置,指定配置

     logging:
       config: classpath:log4j.xml
       level:
         root: info
  4. java代码内使用

     import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     private static Logger logger = LoggerFactory.getLogger(XXXclass.class);
     logger.info("xxx);

spring boot 配置 log4j2

标签:resource   enc   artifact   follow   logger   int   官网文档   log4j   tar   

原文地址:https://www.cnblogs.com/cuiyf/p/12120989.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!