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

Posted naturoying

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了log4j2 扩展日志级别,支持将系统日志与业务处理日志拆分(转)相关的知识,希望对你有一定的参考价值。

项目中,有时候需要对系统中已处理的一些业务数据日志进行提取分析,通常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 扩展日志级别,支持将系统日志与业务处理日志拆分(转)的主要内容,如果未能解决你的问题,请参考以下文章

使用log4j2分离系统日志与业务日志(转)

使用log4j2分离系统日志与业务日志(转)

概念,原理,到例子,全解析logback ,学会日志系统

日志框架之Logback的使用与详细配置

《游戏系统设计九》从青铜到王者,如何从0到1搭建一个完善的日志系统

在 Spring Boot 应用程序中未针对休眠和弹簧过滤 Log4j2 日志级别