码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-06-05 13:27:36      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:row   return   tps   msu   wrapper   自己   直接   war   指定   

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

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

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.MessageFactory;
import org.apache.logging.log4j.spi.AbstractLogger;
import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;
import org.apache.logging.log4j.util.MessageSupplier;
import org.apache.logging.log4j.util.Supplier;

/**log4j2日志扩展,支持业务日志
 * 业务日志为最高级别
 * 使用方式与扩展前基本相同:</br>
 * 使用CustomLogger  log=CustomLogger.getLogger(loggerName);</br>
 * 替代原来的:Logger log=LogManager.getLogger(loggerName);</br>
 * 记录用于数据分析的业务日志 使用log.business(msg);</br>
 * @author jessezeng
 *
 */
public final class CustomLogger extends ExtendedLoggerWrapper {
    private static final long serialVersionUID = 103418572168532L;
    private final ExtendedLoggerWrapper logger;

    private static final String FQCN = CustomLogger.class.getName();
    private static final Level BUSINESS = Level.forName("BUSINESS", 50);

    private CustomLogger(final Logger logger) {
        super((AbstractLogger) logger, logger.getName(), logger.getMessageFactory());
        this.logger = this;
    }

    public static CustomLogger getLogger() {
        final Logger wrapped = LogManager.getLogger();
        return new CustomLogger(wrapped);
    }

    public static CustomLogger getLogger(final Class<?> loggerName) {
        final Logger wrapped = LogManager.getLogger(loggerName);
        return new CustomLogger(wrapped);
    }

    public static CustomLogger getLogger(final Class<?> loggerName, final MessageFactory factory) {
        final Logger wrapped = LogManager.getLogger(loggerName, factory);
        return new CustomLogger(wrapped);
    }

    public static CustomLogger getLogger(final Object value) {
        final Logger wrapped = LogManager.getLogger(value);
        return new CustomLogger(wrapped);
    }

    public static CustomLogger getLogger(final Object value, final MessageFactory factory) {
        final Logger wrapped = LogManager.getLogger(value, factory);
        return new CustomLogger(wrapped);
    }

    public static CustomLogger getLogger(final String name) {
        final Logger wrapped = LogManager.getLogger(name);
        return new CustomLogger(wrapped);
    }

    public static CustomLogger getLogger(final String name, final MessageFactory factory) {
        final Logger wrapped = LogManager.getLogger(name, factory);
        return new CustomLogger(wrapped);
    }

    public void business(final Marker marker, final Message msg) {
        logger.logIfEnabled(FQCN, BUSINESS, marker, msg, (Throwable) null);
    }

    public void business(final Marker marker, final Message msg, final Throwable t) {
        logger.logIfEnabled(FQCN, BUSINESS, marker, msg, t);
    }

    public void business(final Marker marker, final Object message) {
        logger.logIfEnabled(FQCN, BUSINESS, marker, message, (Throwable) null);
    }

    public void business(final Marker marker, final Object message, final Throwable t) {
        logger.logIfEnabled(FQCN, BUSINESS, marker, message, t);
    }

    public void business(final Marker marker, final String message) {
        logger.logIfEnabled(FQCN, BUSINESS, marker, message, (Throwable) null);
    }

    public void business(final Marker marker, final String message, final Object... params) {
        logger.logIfEnabled(FQCN, BUSINESS, marker, message, params);
    }

    public void business(final Marker marker, final String message, final Throwable t) {
        logger.logIfEnabled(FQCN, BUSINESS, marker, message, t);
    }

    public void business(final Message msg) {
        logger.logIfEnabled(FQCN, BUSINESS, null, msg, (Throwable) null);
    }

    public void business(final Message msg, final Throwable t) {
        logger.logIfEnabled(FQCN, BUSINESS, null, msg, t);
    }

    public void business(final Object message) {
        logger.logIfEnabled(FQCN, BUSINESS, null, message, (Throwable) null);
    }

    public void business(final Object message, final Throwable t) {
        logger.logIfEnabled(FQCN, BUSINESS, null, message, t);
    }

    public void business(final String message) {
        logger.logIfEnabled(FQCN, BUSINESS, null, message, (Throwable) null);
    }

    public void business(final String message, final Object... params) {
        logger.logIfEnabled(FQCN, BUSINESS, null, message, params);
    }

    public void business(final String message, final Throwable t) {
        logger.logIfEnabled(FQCN, BUSINESS, null, message, t);
    }

    public void business(final Supplier<?> msgSupplier) {
        logger.logIfEnabled(FQCN, BUSINESS, null, msgSupplier, (Throwable) null);
    }

    public void business(final Supplier<?> msgSupplier, final Throwable t) {
        logger.logIfEnabled(FQCN, BUSINESS, null, msgSupplier, t);
    }

    public void business(final Marker marker, final Supplier<?> msgSupplier) {
        logger.logIfEnabled(FQCN, BUSINESS, marker, msgSupplier, (Throwable) null);
    }

    public void business(final Marker marker, final String message, final Supplier<?>... paramSuppliers) {
        logger.logIfEnabled(FQCN, BUSINESS, marker, message, paramSuppliers);
    }

    public void business(final Marker marker, final Supplier<?> msgSupplier, final Throwable t) {
        logger.logIfEnabled(FQCN, BUSINESS, marker, msgSupplier, t);
    }

    public void business(final String message, final Supplier<?>... paramSuppliers) {
        logger.logIfEnabled(FQCN, BUSINESS, null, message, paramSuppliers);
    }

    public void business(final Marker marker, final MessageSupplier msgSupplier) {
        logger.logIfEnabled(FQCN, BUSINESS, marker, msgSupplier, (Throwable) null);
    }

    public void business(final Marker marker, final MessageSupplier msgSupplier, final Throwable t) {
        logger.logIfEnabled(FQCN, BUSINESS, marker, msgSupplier, t);
    }

    public void business(final MessageSupplier msgSupplier) {
        logger.logIfEnabled(FQCN, BUSINESS, null, msgSupplier, (Throwable) null);
    }

    public void business(final MessageSupplier msgSupplier, final Throwable t) {
        logger.logIfEnabled(FQCN, BUSINESS, null, msgSupplier, t);
    }
}

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

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

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

OFF(0),


    FATAL(100),


    ERROR(200),


    WARN(300),


    INFO(400),


    DEBUG(500),


    TRACE(600),


    ALL(Integer.MAX_VALUE);

 

转载自 作者:jessezeng

https://www.cnblogs.com/jessezeng/p/5446518.html

 

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

标签:row   return   tps   msu   wrapper   自己   直接   war   指定   

原文地址:https://www.cnblogs.com/NaturoYing/p/13048862.html

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