gradle boot log4j2_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > gradle boot log4j2

gradle boot log4j2

 2017/10/19 18:26:12  mygodccl  程序员俱乐部  我要评论(0)
  • 摘要:[list]1.首先去掉logback依赖,在gradle中添加如下configurations{all*.excludemodule:'spring-boot-starter-logging'all*.excludemodule:'logback-classic'all*.excludemodule:'log4j-over-slf4j'}2.再添加log4j2的依赖和log4j2读取yaml文件依赖compile('org.springframework.boot:spring-boot
  • 标签:RAD
[list]
  • 1.首先去掉logback依赖, 在gradle中添加如下
  • class="java">
    configurations {
    	all*.exclude module: 'spring-boot-starter-logging'
        all*.exclude module: 'logback-classic'
        all*.exclude module: 'log4j-over-slf4j'
    }
    
  • 2.再添加log4j2的依赖和log4j2读取yaml文件依赖
  • compile ('org.springframework.boot:spring-boot-starter-log4j2')	
    compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.9.2'
    
  • 3.classpath下面添加文件log4j2.yaml, 示例内容如下(Unknown property 'Configuration'这个提示请忽略).自行更改包路径和日志写盘位置
  • Configuration:  
      status: warn  
      
      Properties: # 定义全局变量  
        Property: # 缺省配置(用于开发环境)。其他环境需要在VM参数中指定,如下:  
          #测试:-Dlog.level.console=warn -Dlog.level.ccl=trace  
          #生产:-Dlog.level.console=warn -Dlog.level.ccl=info        
          - name: log.level.console  
            value: trace  
          - name: log.level.ccl  
            value: trace         
          - name: log.path  
            value: C:/logs  
          - name: project.name  
            value: my-spring-boot  
        
      Appenders:  
        Console:  #输出到控制台  
          name: CONSOLE  
          target: SYSTEM_OUT  
          ThresholdFilter:  
            level: ${sys:log.level.console} # “sys:”表示:如果VM参数中没指定这个变量值,则使用本文件中定义的缺省全局变量值  
            onMatch: ACCEPT  
            onMismatch: DENY  
          PatternLayout:  
            pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %X{user} %t (%F:%L) - %m%n"  
        RollingFile: # 输出到文件,超过128MB归档  
          - name: ROLLING_FILE  
            ignoreExceptions: false  
            fileName: ${log.path}/${project.name}.log  
            filePattern: "${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"  
            PatternLayout:  
              pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"  
            Policies:  
              SizeBasedTriggeringPolicy:  
                size: "128 MB"  
            DefaultRolloverStrategy:  
              max: 1000  
      
      Loggers:  
        Root:  
          level: info  
          AppenderRef:  
            - ref: CONSOLE  
            - ref: ROLLING_FILE  
        Logger: # 为com.ccl包配置特殊的Log级别,方便调试  
          - name: com.ccl  
            additivity: false  
            level: ${sys:log.level.ccl}  
            AppenderRef:  
              - ref: CONSOLE  
              - ref: ROLLING_FILE  
    
  • 4.application.yaml中配置输出级别, 如下:
  • logging:
      level:
        root: warn #主要配置这个级别
    
  • 5.测试用例
  • 
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.slf4j.MDC;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import com.ccl.Application;
    
    @SpringBootTest(classes = Application.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    public class Log4j2Test {
    
    	private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    	@Test
    	public void test() throws Exception {
    		MDC.put("user", "开车不直播, 出事贴吧找老哥.");//对应配置文件pattern中%X{user}变量
    		logger.trace("I am trace log.");
    		logger.debug("I am debug log.");
    		logger.warn("I am warn log.");
    		logger.error("I am error log.");
    	}
    }
    

    [/list]
    上一篇: C#将.spl剥离成.emf文件格式 下一篇: 没有下一篇了!
    发表评论
    用户名: 匿名