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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM