• log4j2 扩展日志级别,支持将系统日志与业务处理日志拆分


      项目中,有时候需要对系统中已处理的一些业务数据日志进行提取分析,通常log4j默认提供的日志级别可能不够用,这时候我们就需要对日志级别进行扩展,以满足我们的需求.

    本文就简单介绍一下log4j2的日志级别扩展,直接上代码:

      1 import org.apache.logging.log4j.Level;
      2 import org.apache.logging.log4j.LogManager;
      3 import org.apache.logging.log4j.Logger;
      4 import org.apache.logging.log4j.Marker;
      5 import org.apache.logging.log4j.message.Message;
      6 import org.apache.logging.log4j.message.MessageFactory;
      7 import org.apache.logging.log4j.spi.AbstractLogger;
      8 import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;
      9 import org.apache.logging.log4j.util.MessageSupplier;
     10 import org.apache.logging.log4j.util.Supplier;
     11 
     12 /**log4j2日志扩展,支持业务日志
     13  * 业务日志为最高级别
     14  * 使用方式与扩展前基本相同:</br>
     15  * 使用CustomLogger  log=CustomLogger.getLogger(loggerName);</br>
     16  * 替代原来的:Logger log=LogManager.getLogger(loggerName);</br>
     17  * 记录用于数据分析的业务日志 使用log.business(msg);</br>
     18  * @author jessezeng
     19  *
     20  */
     21 public final class CustomLogger extends ExtendedLoggerWrapper {
     22     private static final long serialVersionUID = 103418572168532L;
     23     private final ExtendedLoggerWrapper logger;
     24 
     25     private static final String FQCN = CustomLogger.class.getName();
     26     private static final Level BUSINESS = Level.forName("BUSINESS", 50);
     27 
     28     private CustomLogger(final Logger logger) {
     29         super((AbstractLogger) logger, logger.getName(), logger.getMessageFactory());
     30         this.logger = this;
     31     }
     32 
     33     public static CustomLogger getLogger() {
     34         final Logger wrapped = LogManager.getLogger();
     35         return new CustomLogger(wrapped);
     36     }
     37 
     38     public static CustomLogger getLogger(final Class<?> loggerName) {
     39         final Logger wrapped = LogManager.getLogger(loggerName);
     40         return new CustomLogger(wrapped);
     41     }
     42 
     43     public static CustomLogger getLogger(final Class<?> loggerName, final MessageFactory factory) {
     44         final Logger wrapped = LogManager.getLogger(loggerName, factory);
     45         return new CustomLogger(wrapped);
     46     }
     47 
     48     public static CustomLogger getLogger(final Object value) {
     49         final Logger wrapped = LogManager.getLogger(value);
     50         return new CustomLogger(wrapped);
     51     }
     52 
     53     public static CustomLogger getLogger(final Object value, final MessageFactory factory) {
     54         final Logger wrapped = LogManager.getLogger(value, factory);
     55         return new CustomLogger(wrapped);
     56     }
     57 
     58     public static CustomLogger getLogger(final String name) {
     59         final Logger wrapped = LogManager.getLogger(name);
     60         return new CustomLogger(wrapped);
     61     }
     62 
     63     public static CustomLogger getLogger(final String name, final MessageFactory factory) {
     64         final Logger wrapped = LogManager.getLogger(name, factory);
     65         return new CustomLogger(wrapped);
     66     }
     67 
     68     public void business(final Marker marker, final Message msg) {
     69         logger.logIfEnabled(FQCN, BUSINESS, marker, msg, (Throwable) null);
     70     }
     71 
     72     public void business(final Marker marker, final Message msg, final Throwable t) {
     73         logger.logIfEnabled(FQCN, BUSINESS, marker, msg, t);
     74     }
     75 
     76     public void business(final Marker marker, final Object message) {
     77         logger.logIfEnabled(FQCN, BUSINESS, marker, message, (Throwable) null);
     78     }
     79 
     80     public void business(final Marker marker, final Object message, final Throwable t) {
     81         logger.logIfEnabled(FQCN, BUSINESS, marker, message, t);
     82     }
     83 
     84     public void business(final Marker marker, final String message) {
     85         logger.logIfEnabled(FQCN, BUSINESS, marker, message, (Throwable) null);
     86     }
     87 
     88     public void business(final Marker marker, final String message, final Object... params) {
     89         logger.logIfEnabled(FQCN, BUSINESS, marker, message, params);
     90     }
     91 
     92     public void business(final Marker marker, final String message, final Throwable t) {
     93         logger.logIfEnabled(FQCN, BUSINESS, marker, message, t);
     94     }
     95 
     96     public void business(final Message msg) {
     97         logger.logIfEnabled(FQCN, BUSINESS, null, msg, (Throwable) null);
     98     }
     99 
    100     public void business(final Message msg, final Throwable t) {
    101         logger.logIfEnabled(FQCN, BUSINESS, null, msg, t);
    102     }
    103 
    104     public void business(final Object message) {
    105         logger.logIfEnabled(FQCN, BUSINESS, null, message, (Throwable) null);
    106     }
    107 
    108     public void business(final Object message, final Throwable t) {
    109         logger.logIfEnabled(FQCN, BUSINESS, null, message, t);
    110     }
    111 
    112     public void business(final String message) {
    113         logger.logIfEnabled(FQCN, BUSINESS, null, message, (Throwable) null);
    114     }
    115 
    116     public void business(final String message, final Object... params) {
    117         logger.logIfEnabled(FQCN, BUSINESS, null, message, params);
    118     }
    119 
    120     public void business(final String message, final Throwable t) {
    121         logger.logIfEnabled(FQCN, BUSINESS, null, message, t);
    122     }
    123 
    124     public void business(final Supplier<?> msgSupplier) {
    125         logger.logIfEnabled(FQCN, BUSINESS, null, msgSupplier, (Throwable) null);
    126     }
    127 
    128     public void business(final Supplier<?> msgSupplier, final Throwable t) {
    129         logger.logIfEnabled(FQCN, BUSINESS, null, msgSupplier, t);
    130     }
    131 
    132     public void business(final Marker marker, final Supplier<?> msgSupplier) {
    133         logger.logIfEnabled(FQCN, BUSINESS, marker, msgSupplier, (Throwable) null);
    134     }
    135 
    136     public void business(final Marker marker, final String message, final Supplier<?>... paramSuppliers) {
    137         logger.logIfEnabled(FQCN, BUSINESS, marker, message, paramSuppliers);
    138     }
    139 
    140     public void business(final Marker marker, final Supplier<?> msgSupplier, final Throwable t) {
    141         logger.logIfEnabled(FQCN, BUSINESS, marker, msgSupplier, t);
    142     }
    143 
    144     public void business(final String message, final Supplier<?>... paramSuppliers) {
    145         logger.logIfEnabled(FQCN, BUSINESS, null, message, paramSuppliers);
    146     }
    147 
    148     public void business(final Marker marker, final MessageSupplier msgSupplier) {
    149         logger.logIfEnabled(FQCN, BUSINESS, marker, msgSupplier, (Throwable) null);
    150     }
    151 
    152     public void business(final Marker marker, final MessageSupplier msgSupplier, final Throwable t) {
    153         logger.logIfEnabled(FQCN, BUSINESS, marker, msgSupplier, t);
    154     }
    155 
    156     public void business(final MessageSupplier msgSupplier) {
    157         logger.logIfEnabled(FQCN, BUSINESS, null, msgSupplier, (Throwable) null);
    158     }
    159 
    160     public void business(final MessageSupplier msgSupplier, final Throwable t) {
    161         logger.logIfEnabled(FQCN, BUSINESS, null, msgSupplier, t);
    162     }
    163 }

    代码实际上很简单,需要注意的地方是:

     26     private static final Level BUSINESS = Level.forName("BUSINESS", 50);  // 50用于指定级别

    注意上面红色的数字,这个数字用于指定级别的高低,可以根据自己的需要定义不同的值,log4j2中默认级别值可以参考如下:

     1     OFF(0),
     2 
     3 
     4     FATAL(100),
     5 
     6 
     7     ERROR(200),
     8 
     9  
    10     WARN(300),
    11 
    12 
    13     INFO(400),
    14 
    15 
    16     DEBUG(500),
    17 
    18 
    19     TRACE(600),
    20 
    21 
    22     ALL(Integer.MAX_VALUE);
  • 相关阅读:
    子页面与父页面相互调用函数、元素、变量
    springboot项目多数据源及其事务
    mybatis逆向工程
    PageHelper 分页插件
    spring boot 在eclipse中打war包,及jar包
    Spring 定时任务之 @Scheduled cron表达式
    发送邮件
    spring+springmvc+hibernate 框架搭建
    MySQL驱动和数据库字符集设置不搭配
    Oracle与MySQL区别
  • 原文地址:https://www.cnblogs.com/jessezeng/p/5446518.html
Copyright © 2020-2023  润新知