• 自定义log4j2的PatternLayout参数


    1.添加类

    package com.yuanian.micro.config;
    
    import org.apache.logging.log4j.core.LogEvent;
    import org.apache.logging.log4j.core.config.plugins.Plugin;
    import org.apache.logging.log4j.core.pattern.ConverterKeys;
    import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
    import org.apache.logging.log4j.core.pattern.PatternConverter;
    
    /**
     * @author liujy
     * @date 2019/12/18 16:46
     * 增加serviceName到日志的pattern
     **/
    @Plugin(name = "ServiceNamePatternConverter", category = PatternConverter.CATEGORY)
    @ConverterKeys({"s", "serviceName"})
    public class ServiceNamePatternConverter extends LogEventPatternConverter {
        private  String SERVICENAME = "ms-job-console";
    
        private static final ServiceNamePatternConverter INSTANCE =
                new ServiceNamePatternConverter();
    
        public static ServiceNamePatternConverter newInstance(
                final String[] options) {
            return INSTANCE;
        }
    
        private ServiceNamePatternConverter() {
            super("serviceName", "serviceName");
        }
    
        @Override
        public void format(LogEvent logEvent, StringBuilder toAppendTo) {
            toAppendTo.append(SERVICENAME);
        }
    }
    
    1. 修改log4j2.yml文件,添加变量Configuration.packages为刚才创建类所在的包
    Configuration:
      status: warn
      packages: com.yuanian.micro.config
    

    3.在log4j2.yml中使用自定义的参数

    Configuration:
      status: warn
      packages: com.yuanian.micro.config
      Appenders:
        Console:  #输出到控制台
          name: CONSOLE
          target: SYSTEM_OUT
          ThresholdFilter:
            level: info
            onMatch: ACCEPT
            onMismatch: DENY
          PatternLayout:
            pattern: "%d %p %c [serviceName = %s] [Class = %c] [File = %F] [Line = %L] [Method = %M] [%t] %m%n"
    

    4.效果如下:

    2019-12-18 17:24:00,183 WARN com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder [serviceName = ms-job-console] [Class = com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder] [File = NacosPropertySourceBuilder.java] [Line = 86] [Method = loadNacosData] [main] Ignore the empty nacos configuration and get it based on dataId[ms-job-console] & group[DEFAULT_GROUP]
    

    5.不可以把参数定义重复,log4j2自定义的参数可以参考下面的地址 https://blog.csdn.net/guoquanyou/article/details/5689652


    本文参考链接:https://www.cnblogs.com/zr520/p/6406443.html

  • 相关阅读:
    Kinect 开发 —— 硬件设备解剖
    Kinect 开发 —— 引言
    (转)OpenCV 基本知识框架
    OpenCV —— 摄像机模型与标定
    OpenCV —— 跟踪与运动
    OpenCV —— 图像局部与分割(二)
    OpenCV —— 图像局部与部分分割(一)
    OpenCV —— 轮廓
    OpenCV —— 直方图与匹配
    OpenCV —— 图像变换
  • 原文地址:https://www.cnblogs.com/yechen2019/p/12061192.html
Copyright © 2020-2023  润新知