log4j自定义日志等级;数据库缓冲池存储到数据库;数据库和输出到文件终端分离;发送邮件

27 篇文章 0 订阅

依赖库:

compile 'log4j:log4j:1.2.17'

compile 'org.slf4j:slf4j-log4j12:1.7.21'

compile 'javax.mail:mail:1.4.7'

compile 'javax.activation:activation:1.1.1'

compile 'mysql:mysql-connector-java:5.1.39'

compile 'com.zaxxer:HikariCP:2.4.6'

compile 'org.apache.commons:commons-pool2:2.4.2'

1.继承log4j的level类,定义一个自己的日志级别:

package com.panda.core.log.impl;


import org.apache.log4j.Level;

/**
 * 自定义日志等级
 *
 * Created by Lovell on 03/11/2016.
 */
/*
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @@@@        @@        @@       @@@@
 @@@@  @@@@@@@@  @@@@  @@  @@@   @@@
 @@@@  @@@   @@  @@@@  @@  @@@@  @@@
 @@@@  @@@@  @@  @@@@  @@  @@@   @@@
 @@@@        @@        @@       @@@@
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 */

public class LogLevel extends Level {
    /**
     * Instantiate a Level object.
     *
     * @param level
     * @param levelStr
     * @param syslogEquivalent
     */
    protected LogLevel(int level, String levelStr, int syslogEquivalent) {
        super(level, levelStr, syslogEquivalent);
    }
}

2.继承Filter类,定义一个自己的过滤器:

FileFilter.java

package com.panda.core.log.impl;

import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;

import static org.apache.log4j.Priority.ERROR_INT;
import static org.apache.log4j.Priority.INFO_INT;

/**
 * 存储文件过滤器
 * Created by Lovell on 04/11/2016.
 */
/*
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @@@@        @@        @@       @@@@
 @@@@  @@@@@@@@  @@@@  @@  @@@   @@@
 @@@@  @@@   @@  @@@@  @@  @@@@  @@@
 @@@@  @@@@  @@  @@@@  @@  @@@   @@@
 @@@@        @@        @@       @@@@
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 */

public class FileFilter extends Filter{

    /**
     * Do we return ACCEPT when a match occurs. Default is
     * <code>false</code>, so that later filters get run by default
     */
    boolean acceptOnMatch = false;

    int levelMin;
    int levelMax;

    /**
     * Return the decision of this filter.
     */
    @Override
    public int decide(LoggingEvent event) {
        int inputLevel = event.getLevel().toInt();
        if (inputLevel >= levelMin && inputLevel <= levelMax) {
            if (inputLevel == ERROR_INT || inputLevel == INFO_INT) {
                return Filter.ACCEPT;
            }
        }
        return Filter.DENY;
    }

    /**
     * Get the value of the <code>LevelMax</code> option.
     */
    public int getLevelMax() {
        return levelMax;
    }

    /**
     * Get the value of the <code>LevelMin</code> option.
     */
    public int getLevelMin() {
        return levelMin;
    }

    /**
     * Get the value of the <code>AcceptOnMatch</code> option.
     */
    public boolean getAcceptOnMatch() {
        return acceptOnMatch;
    }

    /**
     * Set the <code>LevelMax</code> option.
     */
    public void setLevelMax(int levelMax) {
        this.levelMax = levelMax;
    }

    /**
     * Set the <code>LevelMin</code> option.
     */
    public void setLevelMin(int levelMin) {
        this.levelMin = levelMin;
    }

    /**
     * Set the <code>AcceptOnMatch</code> option.
     */
    public void setAcceptOnMatch(boolean acceptOnMatch) {
        this.acceptOnMatch = acceptOnMatch;
    }

}

LogFilter.java

package com.panda.core.log.impl;

import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;

import static org.apache.log4j.Priority.ERROR_INT;
import static org.apache.log4j.Priority.INFO_INT;

/**
 * 存储文件过滤器
 *
 * Created by Lovell on 03/11/2016.
 */
/*
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @@@@        @@        @@       @@@@
 @@@@  @@@@@@@@  @@@@  @@  @@@   @@@
 @@@@  @@@   @@  @@@@  @@  @@@@  @@@
 @@@@  @@@@  @@  @@@@  @@  @@@   @@@
 @@@@        @@        @@       @@@@
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 */

public class LogFilter extends Filter {
    /**
     * Do we return ACCEPT when a match occurs. Default is
     * <code>false</code>, so that later filters get run by default
     */
    boolean acceptOnMatch = false;

    int levelMin;
    int levelMax;

    /**
     * Return the decision of this filter.
     */
    @Override
    public int decide(LoggingEvent event) {
        int inputLevel = event.getLevel().toInt();
        if (inputLevel >= levelMin && inputLevel <= levelMax) {
            if (inputLevel == ERROR_INT || inputLevel == INFO_INT) {
                return Filter.NEUTRAL
            }
            return Filter.ACCEPT;
        }
        return Filter.DENY;
    }

    /**
     * Get the value of the <code>LevelMax</code> option.
     */
    public int getLevelMax() {
        return levelMax;
    }

    /**
     * Get the value of the <code>LevelMin</code> option.
     */
    public int getLevelMin() {
        return levelMin;
    }

    /**
     * Get the value of the <code>AcceptOnMatch</code> option.
     */
    public boolean getAcceptOnMatch() {
        return acceptOnMatch;
    }

    /**
     * Set the <code>LevelMax</code> option.
     */
    public void setLevelMax(int levelMax) {
        this.levelMax = levelMax;
    }

    /**
     * Set the <code>LevelMin</code> option.
     */
    public void setLevelMin(int levelMin) {
        this.levelMin = levelMin;
    }

    /**
     * Set the <code>AcceptOnMatch</code> option.
     */
    public void setAcceptOnMatch(boolean acceptOnMatch) {
        this.acceptOnMatch = acceptOnMatch;
    }

}

3.在不侵入log4j源代码的情况下,对log4j的logger输出进行封装,并且添加自己定义的级别的输出方法:

LogLogger.java

package com.panda.core.log.impl;

import org.apache.log4j.Level;
import org.apache.log4j.net.SyslogAppender;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.ThrowableInformation;
import org.slf4j.Marker;
import org.slf4j.event.LoggingEvent;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;
import org.slf4j.spi.LocationAwareLogger;

import static org.slf4j.event.EventConstants.NA_SUBST;

/**
 * 自定义Logger,实现logger.playerInfo接口
 *
 * Created by Lovell on 03/11/2016.
 */
/*
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @@@@        @@        @@       @@@@
 @@@@  @@@@@@@@  @@@@  @@  @@@   @@@
 @@@@  @@@   @@  @@@@  @@  @@@@  @@@
 @@@@  @@@@  @@  @@@@  @@  @@@   @@@
 @@@@        @@        @@       @@@@
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 */

public class LogLogger {

    private final static int priority = 40100;

    /**
     * define playerInfo level
     */
    public static final Level PLAYER_INFO_LEVEL = new LogLevel(priority, "PLAYER_INFO", SyslogAppender.LOG_LOCAL0);


    final transient org.apache.log4j.Logger logger;

    /**
     * Following the pattern discussed in pages 162 through 168 of "The complete
     * log4j manual".
     */
    final static String FQCN = LogLogger.class.getName();

    // Does the log4j version in use recognize the TRACE level?
    // The trace level was introduced in log4j 1.2.12.
    final boolean traceCapable;

    private LogLogger(Class<?> clazz) {
        logger = org.apache.log4j.Logger.getLogger(clazz);
        traceCapable = isTraceCapable();
    }

    private LogLogger() {
        logger = org.apache.log4j.Logger.getRootLogger();
        traceCapable = isTraceCapable();
    }

    public static LogLogger getLogger(Class<?> clazz) {
        return new LogLogger(clazz);
    }

    public static LogLogger getRootLogger() {
        return new LogLogger();
    }

    private boolean isTraceCapable() {
        try {
            logger.isTraceEnabled();
            return true;
        } catch (NoSuchMethodError e) {
            return false;
        }
    }

    /**
     * Is this logger instance enabled for the TRACE level?
     *
     * @return True if this Logger is enabled for level TRACE, false otherwise.
     */
    public boolean isTraceEnabled() {
        if (traceCapable) {
            return logger.isTraceEnabled();
        } else {
            return logger.isDebugEnabled();
        }
    }
    /**
     * Log a message object at level TRACE.
     *
     * @param msg
     *          - the message object to be logged
     */
    public void trace(String msg) {
        logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, msg, null);
    }

    /**
     * Log a message at level TRACE according to the specified format and
     * argument.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for level TRACE.
     * </p>
     *
     * @param format
     *          the format string
     * @param arg
     *          the argument
     */
    public void trace(String format, Object arg) {
        if (isTraceEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg);
            logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log a message at level TRACE according to the specified format and
     * arguments.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the TRACE level.
     * </p>
     *
     * @param format
     *          the format string
     * @param arg1
     *          the first argument
     * @param arg2
     *          the second argument
     */
    public void trace(String format, Object arg1, Object arg2) {
        if (isTraceEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
            logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log a message at level TRACE according to the specified format and
     * arguments.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the TRACE level.
     * </p>
     *
     * @param format
     *          the format string
     * @param arguments
     *          an array of arguments
     */
    public void trace(String format, Object... arguments) {
        if (isTraceEnabled()) {
            FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments);
            logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log an exception (throwable) at level TRACE with an accompanying message.
     *
     * @param msg
     *          the message accompanying the exception
     * @param t
     *          the exception (throwable) to log
     */
    public void trace(String msg, Throwable t) {
        logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, msg, t);
    }

    /**
     * Is this logger instance enabled for the DEBUG level?
     *
     * @return True if this Logger is enabled for level DEBUG, false otherwise.
     */
    public boolean isDebugEnabled() {
        return logger.isDebugEnabled();
    }

    /**
     * Log a message object at level DEBUG.
     *
     * @param msg
     *          - the message object to be logged
     */
    public void debug(String msg) {
        logger.log(FQCN, Level.DEBUG, msg, null);
    }

    /**
     * Log a message at level DEBUG according to the specified format and
     * argument.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for level DEBUG.
     * </p>
     *
     * @param format
     *          the format string
     * @param arg
     *          the argument
     */
    public void debug(String format, Object arg) {
        if (logger.isDebugEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg);
            logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log a message at level DEBUG according to the specified format and
     * arguments.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the DEBUG level.
     * </p>
     *
     * @param format
     *          the format string
     * @param arg1
     *          the first argument
     * @param arg2
     *          the second argument
     */
    public void debug(String format, Object arg1, Object arg2) {
        if (logger.isDebugEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
            logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log a message at level DEBUG according to the specified format and
     * arguments.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the DEBUG level.
     * </p>
     *
     * @param format
     *          the format string
     * @param arguments an array of arguments
     */
    public void debug(String format, Object... arguments) {
        if (logger.isDebugEnabled()) {
            FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments);
            logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log an exception (throwable) at level DEBUG with an accompanying message.
     *
     * @param msg
     *          the message accompanying the exception
     * @param t
     *          the exception (throwable) to log
     */
    public void debug(String msg, Throwable t) {
        logger.log(FQCN, Level.DEBUG, msg, t);
    }

    /**
     * Is this logger instance enabled for the INFO level?
     *
     * @return True if this Logger is enabled for the INFO level, false otherwise.
     */
    public boolean isInfoEnabled() {
        return logger.isInfoEnabled();
    }

    /**
     * Log a message object at the INFO level.
     *
     * @param msg
     *          - the message object to be logged
     */
    public void info(String msg) {
        logger.log(FQCN, Level.INFO, msg, null);
    }

    /**
     * Log a message at level INFO according to the specified format and argument.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the INFO level.
     * </p>
     *
     * @param format
     *          the format string
     * @param arg
     *          the argument
     */
    public void info(String format, Object arg) {
        if (logger.isInfoEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg);
            logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log a message at the INFO level according to the specified format and
     * arguments.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the INFO level.
     * </p>
     *
     * @param format
     *          the format string
     * @param arg1
     *          the first argument
     * @param arg2
     *          the second argument
     */
    public void info(String format, Object arg1, Object arg2) {
        if (logger.isInfoEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
            logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log a message at level INFO according to the specified format and
     * arguments.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the INFO level.
     * </p>
     *
     * @param format
     *          the format string
     * @param argArray
     *          an array of arguments
     */
    public void info(String format, Object... argArray) {
        if (logger.isInfoEnabled()) {
            FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
            logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log an exception (throwable) at the INFO level with an accompanying
     * message.
     *
     * @param msg
     *          the message accompanying the exception
     * @param t
     *          the exception (throwable) to log
     */
    public void info(String msg, Throwable t) {
        logger.log(FQCN, Level.INFO, msg, t);
    }

    /**
     * Is this logger instance enabled for the WARN level?
     *
     * @return True if this Logger is enabled for the WARN level, false otherwise.
     */
    public boolean isWarnEnabled() {
        return logger.isEnabledFor(Level.WARN);
    }

    /**
     * Log a message object at the WARN level.
     *
     * @param msg
     *          - the message object to be logged
     */
    public void warn(String msg) {
        logger.log(FQCN, Level.WARN, msg, null);
    }

    /**
     * Log a message at the WARN level according to the specified format and
     * argument.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the WARN level.
     * </p>
     *
     * @param format
     *          the format string
     * @param arg
     *          the argument
     */
    public void warn(String format, Object arg) {
        if (logger.isEnabledFor(Level.WARN)) {
            FormattingTuple ft = MessageFormatter.format(format, arg);
            logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log a message at the WARN level according to the specified format and
     * arguments.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the WARN level.
     * </p>
     *
     * @param format
     *          the format string
     * @param arg1
     *          the first argument
     * @param arg2
     *          the second argument
     */
    public void warn(String format, Object arg1, Object arg2) {
        if (logger.isEnabledFor(Level.WARN)) {
            FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
            logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log a message at level WARN according to the specified format and
     * arguments.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the WARN level.
     * </p>
     *
     * @param format
     *          the format string
     * @param argArray
     *          an array of arguments
     */
    public void warn(String format, Object... argArray) {
        if (logger.isEnabledFor(Level.WARN)) {
            FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
            logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log an exception (throwable) at the WARN level with an accompanying
     * message.
     *
     * @param msg
     *          the message accompanying the exception
     * @param t
     *          the exception (throwable) to log
     */
    public void warn(String msg, Throwable t) {
        logger.log(FQCN, Level.WARN, msg, t);
    }

    /**
     * Is this logger instance enabled for level ERROR?
     *
     * @return True if this Logger is enabled for level ERROR, false otherwise.
     */
    public boolean isErrorEnabled() {
        return logger.isEnabledFor(Level.ERROR);
    }

    /**
     * Log a message object at the ERROR level.
     *
     * @param msg
     *          - the message object to be logged
     */
    public void error(String msg) {
        logger.log(FQCN, Level.ERROR, msg, null);
    }

    /**
     * Log a message at the ERROR level according to the specified format and
     * argument.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the ERROR level.
     * </p>
     *
     * @param format
     *          the format string
     * @param arg
     *          the argument
     */
    public void error(String format, Object arg) {
        if (logger.isEnabledFor(Level.ERROR)) {
            FormattingTuple ft = MessageFormatter.format(format, arg);
            logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log a message at the ERROR level according to the specified format and
     * arguments.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the ERROR level.
     * </p>
     *
     * @param format
     *          the format string
     * @param arg1
     *          the first argument
     * @param arg2
     *          the second argument
     */
    public void error(String format, Object arg1, Object arg2) {
        if (logger.isEnabledFor(Level.ERROR)) {
            FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
            logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log a message at level ERROR according to the specified format and
     * arguments.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the ERROR level.
     * </p>
     *
     * @param format
     *          the format string
     * @param argArray
     *          an array of arguments
     */
    public void error(String format, Object... argArray) {
        if (logger.isEnabledFor(Level.ERROR)) {
            FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
            logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log an exception (throwable) at the ERROR level with an accompanying
     * message.
     *
     * @param msg
     *          the message accompanying the exception
     * @param t
     *          the exception (throwable) to log
     */
    public void error(String msg, Throwable t) {
        logger.log(FQCN, Level.ERROR, msg, t);
    }

    /**
     * Is this logger instance enabled for level PLAYER_INFO_LEVEL?
     *
     * @return True if this Logger is enabled for level PLAYER_INFO_LEVEL, false otherwise.
     */
    public boolean isPlayerInfoEnabled() {
        return logger.isEnabledFor(PLAYER_INFO_LEVEL);
    }

    /**
     * Log a message object at the PLAYER_INFO_LEVEL level.
     *
     * @param msg
     *          - the message object to be logged
     */
    public void playerInfo(String msg) {
        logger.log(FQCN, PLAYER_INFO_LEVEL, msg, null);
    }

    /**
     * Log a message at the PLAYER_INFO_LEVEL level according to the specified format and
     * argument.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the PLAYER_INFO_LEVEL level.
     * </p>
     *
     * @param format
     *          the format string
     * @param arg
     *          the argument
     */
    public void playerInfo(String format, Object arg) {
        if (logger.isEnabledFor(PLAYER_INFO_LEVEL)) {
            FormattingTuple ft = MessageFormatter.format(format, arg);
            logger.log(FQCN, PLAYER_INFO_LEVEL, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log a message at the PLAYER_INFO_LEVEL level according to the specified format and
     * arguments.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the PLAYER_INFO_LEVEL level.
     * </p>
     *
     * @param format
     *          the format string
     * @param arg1
     *          the first argument
     * @param arg2
     *          the second argument
     */
    public void playerInfo(String format, Object arg1, Object arg2) {
        if (logger.isEnabledFor(PLAYER_INFO_LEVEL)) {
            FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
            logger.log(FQCN, PLAYER_INFO_LEVEL, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log a message at level PLAYER_INFO_LEVEL according to the specified format and
     * arguments.
     *
     * <p>
     * This form avoids superfluous object creation when the logger is disabled
     * for the PLAYER_INFO_LEVEL level.
     * </p>
     *
     * @param format
     *          the format string
     * @param argArray
     *          an array of arguments
     */
    public void playerInfo(String format, Object... argArray) {
        if (logger.isEnabledFor(PLAYER_INFO_LEVEL)) {
            FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
            logger.log(FQCN, PLAYER_INFO_LEVEL, ft.getMessage(), ft.getThrowable());
        }
    }

    /**
     * Log an exception (throwable) at the PLAYER_INFO_LEVEL level with an accompanying
     * message.
     *
     * @param msg
     *          the message accompanying the exception
     * @param t
     *          the exception (throwable) to log
     */
    public void playerInfo(String msg, Throwable t) {
        logger.log(FQCN, PLAYER_INFO_LEVEL, msg, t);
    }

    public void log(Marker marker, String callerFQCN, int level, String msg, Object[] argArray, Throwable t) {
        Level log4jLevel = toLog4jLevel(level);
        logger.log(callerFQCN, log4jLevel, msg, t);
    }

    private Level toLog4jLevel(int level) {
        Level log4jLevel;
        switch (level) {
            case LocationAwareLogger.TRACE_INT:
                log4jLevel = traceCapable ? Level.TRACE : Level.DEBUG;
                break;
            case LocationAwareLogger.DEBUG_INT:
                log4jLevel = Level.DEBUG;
                break;
            case LocationAwareLogger.INFO_INT:
                log4jLevel = Level.INFO;
                break;
            case LocationAwareLogger.WARN_INT:
                log4jLevel = Level.WARN;
                break;
            case LocationAwareLogger.ERROR_INT:
                log4jLevel = Level.ERROR;
                break;
            default:
                throw new IllegalStateException("Level number " + level + " is not recognized.");
        }
        return log4jLevel;
    }

    public void log(LoggingEvent event) {
        Level log4jLevel = toLog4jLevel(event.getLevel().toInt());
        if (!logger.isEnabledFor(log4jLevel))
            return;

        org.apache.log4j.spi.LoggingEvent log4jevent = toLog4jEvent(event, log4jLevel);
        logger.callAppenders(log4jevent);

    }

    private org.apache.log4j.spi.LoggingEvent toLog4jEvent(LoggingEvent event, Level log4jLevel) {

        FormattingTuple ft = MessageFormatter.format(event.getMessage(), event.getArgumentArray(), event.getThrowable());

        LocationInfo locationInfo = new LocationInfo(NA_SUBST, NA_SUBST, NA_SUBST, "0");

        ThrowableInformation ti = null;
        Throwable t = ft.getThrowable();
        if (t != null)
            ti = new ThrowableInformation(t);

        org.apache.log4j.spi.LoggingEvent log4jEvent = new org.apache.log4j.spi.LoggingEvent(FQCN, logger, event.getTimeStamp(), log4jLevel, ft.getMessage(),
                event.getThreadName(), ti, null, locationInfo, null);

        return log4jEvent;
    }
}

4.自定义Appender

package com.panda.core.log.control;

import com.panda.core.log.util.LogHelper;
import org.apache.log4j.jdbc.JDBCAppender;
import org.apache.log4j.spi.LoggingEvent;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 基层JDBCAppender
 * Created by Lovell on 26/10/2016.
 */
/*
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @@@@        @@        @@       @@@@
 @@@@  @@@@@@@@  @@@@  @@  @@@   @@@
 @@@@  @@@   @@  @@@@  @@  @@@@  @@@
 @@@@  @@@@  @@  @@@@  @@  @@@   @@@
 @@@@        @@        @@       @@@@
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 */

public class DBAppender extends JDBCAppender {

    private Connection connection;

    @Override
    protected Connection getConnection() throws SQLException {
        return connection = LogHelper.getInstance().dbService.getConnection();
    }

    @Override
    protected void closeConnection(Connection con) {
        try {
            if (connection != null && connection.isClosed())
                LogHelper.getInstance().dbService.evictConnection(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行sql语句
     *
     * @param sql
     * @throws SQLException
     */
    @Override
    protected void execute(String sql) throws SQLException {
        Connection con = null;
        PreparedStatement preparedStmt = null;

        try {
            con = getConnection();
            preparedStmt = getConnection().prepareStatement(sql);
            preparedStmt.executeUpdate();
        } finally {
            if(preparedStmt != null) {
                preparedStmt.close();
            }
            closeConnection(con);
        }
        //System.out.println("Execute: " + sql);
    }

    @Override
    protected String getLogStatement(LoggingEvent event) {

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(layout.format(event));
        return stringBuffer.toString();
    }
}
6.添加数据库缓冲池和缓冲池接口封装

DBService.java (用数据库连接池连接数据库)

package com.panda.core.db.impl;


import com.panda.core.tools.Tools;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 数据库服务
 * Created by Lovell on 16/6/18.
 */
public class DBService {
    //private static Logger logger = LoggerFactory.getLogger(DBService.class);

    private static String DB_CONFIG_FILE = "/db.properties";

    // 数据库连接数
    private short dbMaxConn = 0;

    // 数据库服务器addr
    private String dbUrl = null;

    // 数据库连接端口
    private short dbPort = 0;

    // 数据库名称
    private String dbName = null;

    // 数据库登录用户名
    private String dbUsername = null;

    // 数据库登录密码
    private String dbPassword = null;

    // 数据库缓冲池
    private HikariDataSource dataSource;

    private static DBService dBService;

    public static DBService getInstance() {
        if (dBService == null) {
            dBService = new DBService();
        }
        return dBService;
    }

    public DBService() {
    }

    public DBService(final String fileName) {
        this.setConfigFileName(fileName);
    }

    public void setConfigFileName(final String fileName) {
        DB_CONFIG_FILE = "/" + fileName;
    }

    public void start() throws IOException, SQLException {
        Properties properties = new Properties();

        String strTTT = Tools.getPath();
        File file = new File(strTTT + DB_CONFIG_FILE);

//        System.out.println("读取数据库配置:" + strTTT + DB_CONFIG_FILE);

//        InputStream in = DBService.class.getClass().getResourceAsStream(DB_CONFIG_FILE);

        InputStream in = new FileInputStream(file);
        properties.load(in);

        dbMaxConn = Short.valueOf(properties.getProperty("db_max_conn"));
        dbUrl = String.valueOf(properties.getProperty("db_url"));
        dbPort = Short.valueOf(properties.getProperty("db_port"));
        dbName = String.valueOf(properties.getProperty("db_name"));
        dbUsername = String.valueOf(properties.getProperty("db_username"));
        dbPassword = String.valueOf(properties.getProperty("db_password"));

        if (dbUrl == null || dbUrl.length() == 0) {
            //logger.error("配置的数据库ip地址错误!");
            System.exit(0);
        }

        HikariConfig config = new HikariConfig();
        config.setMaximumPoolSize(dbMaxConn);
        config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
        config.addDataSourceProperty("serverName", dbUrl);
        config.addDataSourceProperty("port", dbPort);
        config.addDataSourceProperty("databaseName", dbName);
        config.addDataSourceProperty("user", dbUsername);
        config.addDataSourceProperty("password", dbPassword);
        dataSource = new HikariDataSource(config);
    }

    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void evictConnection(Connection connection) throws SQLException {
        if (connection != null && connection.isClosed()){
            connection.close();
            dataSource.evictConnection(connection);
        }
    }

    public boolean stop() throws SQLException {
        dataSource.close();
        return true;
    }
}


LogDao.java (日志连接数据库接口)

package com.panda.core.log.dao;

import com.panda.core.db.impl.DBService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.sql.SQLException;

/**
 * 日志接口类
 * Created by Lovell on 26/10/2016.
 */
/*
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @@@@        @@        @@       @@@@
 @@@@  @@@@@@@@  @@@@  @@  @@@   @@@
 @@@@  @@@   @@  @@@@  @@  @@@@  @@@
 @@@@  @@@@  @@  @@@@  @@  @@@   @@@
 @@@@        @@        @@       @@@@
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 */

public class LogDao{
    private static Logger logger = LoggerFactory.getLogger(LogDao.class);

//    private static final String LOG_CONFIG_FILE = "/log4j.xml";

    public DBService dbService;

    public LogDao() {
        this.dbService = new DBService();
    }

    /**
     * 设置MySQL服务器配置文件路径
     *
     * @param fileName
     */
    public void setDBConfigFileName(final String fileName) {
        if (fileName == null || fileName.length() == 0) {
            return;
        }
        dbService.setConfigFileName(fileName);
    }

    /**
     * 以配置文件启动数据库
     *
     * @param fileName
     */
    public void startDB(final String fileName) throws IOException, SQLException {
        setDBConfigFileName(fileName);
        dbService.start();
        logger.info("Start DB.");
    }

    /**
     * 停止MySQL
     */
    public void stopDB() throws IOException, SQLException {
        dbService.stop();
        logger.info("MySQL was stopped.");
    }
}
LogHelper.java (日志操作接口)

package com.panda.core.log.util;

import com.panda.core.log.dao.LogDao;

/**
 * 日志操作接口
 * Created by Lovell on 27/10/2016.
 */
/*
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @@@@        @@        @@       @@@@
 @@@@  @@@@@@@@  @@@@  @@  @@@   @@@
 @@@@  @@@   @@  @@@@  @@  @@@@  @@@
 @@@@  @@@@  @@  @@@@  @@  @@@   @@@
 @@@@        @@        @@       @@@@
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 */

public class LogHelper extends LogDao {

    private static LogHelper logHelper;
    public static LogHelper getInstance() {
        if (logHelper == null) {
            logHelper = new LogHelper();
        }
        return logHelper;
    }
}

5.修改log4j的xml配置文件,添加自定义的Appender

<?xml version="1.0" encoding="UTF-8"?>
<!--写法一:-->
<!--<!DOCTYPE log4j:configuration SYSTEM-->
<!--"http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">-->
<!--写法二-->
<!DOCTYPE log4j:configuration SYSTEM
        "build/classes/main/log_config/log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j="http://jakarta.apache.org/log4j/">
<!--<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">-->
    <!--# output to console-->
    <appender name="stdout"
              class="org.apache.log4j.ConsoleAppender">
        <filter class="com.panda.core.log.impl.FileFilter">
            <param name="LevelMin" value="20000"/>
            <param name="LevelMax" value="40000"/>
        </filter>
        <param name="Threshold" value="INFO"/>
        <!--<target class="System.out"/>-->
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{ISO8601}]%t %p [%c] - %-90m %l %n"/>
        </layout>
    </appender>

    <!--# output INFO to info.log-->
    <appender name="D" class="org.apache.log4j.DailyRollingFileAppender">
        <filter class="com.panda.core.log.impl.FileFilter">
            <param name="LevelMin" value="20000"/>
            <param name="LevelMax" value="40000"/>
        </filter>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{ISO8601}]%t %p [%c] - %-90m %l %n"/>
        </layout>
        <param name="File" value="./logs/info.log"/>
        <param name="Append" value="true"/>
        <param name="Threshold" value="INFO"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{ISO8601}]%t %p [%c] - %-90m %l %n"/>
        </layout>

    </appender>

    <!--# output ERROR to error.log-->
    <appender name="E" class="org.apache.log4j.DailyRollingFileAppender">
        <filter class="com.panda.core.log.impl.FileFilter">
            <param name="LevelMin" value="20000"/>
            <param name="LevelMax" value="40000"/>
        </filter>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{ISO8601}]%t %p [%c] - %-90m %l %n"/>
        </layout>

        <param name="File" value="./logs/error.log"/>
        <param name="Append" value="true"/>
        <param name="Threshold" value="ERROR"/>
    </appender>

    <!--# jdbc-->
    <appender name="db" class="com.panda.core.log.control.DBAppender">
        <filter class="com.panda.core.log.impl.LogFilter">
            <param name="LevelMin" value="40100"/>
            <param name="LevelMax" value="40100"/>
        </filter>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n %L"/>
        </layout>
        <param name="driver" value="com.mysql.jdbc.Driver"/>
        <param name="Threshold" value="PLAYER_INFO"/>
        <param name="user" value="root"/>
        <param name="password" value="root"/>
        <param name="URL"
               value="jdbc:mysql://192.168.199.132:3306/mind?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false"/>
        <param name="sql"
               value="insert into t_login (c_id, c_username, c_password) VALUES (%X{userId}, '%d{yyyy-MM-dd HH:mm:ss}', '%m')"/>
    </appender>

    <!--# send email-->
    <appender name="MAIL" class="org.apache.log4j.net.SMTPAppender">
        <filter class="com.panda.core.log.impl.FileFilter">
            <param name="LevelMin" value="20000"/>
            <param name="LevelMax" value="40000"/>
        </filter>
        <param name="Threshold" value="ERROR"/>
        <!--缓存文件大小,日志数目达到2时发送邮件-->
        <param name="BufferSize" value="2"/>
        <!--发送邮件的服务器-->
        <param name="SMTPHost" value="smtp.163.com"/>
        <!--邮件主题-->
        <param name="Subject" value="Log4JErrorMessage"/>
        <!--发件人邮箱登录账号-->
        <param name="SMTPUsername" value="minhowe@163.com"/>
        <!--发件人邮箱登录密码-->
        <param name="SMTPPassword" value="你自己的邮箱密码"/>
        <!--发件人邮箱-->
        <param name="From" value="minhowe@163.com"/>
        <!--收件人邮箱-->
        <param name="To" value="952445939@qq.com"/>
        <!--发送邮件的格式-->
        <layout class="org.apache.log4j.HTMLLayout">
        </layout>
    </appender>

    <!--# set level-->
    <root>
        <priority value="PLAYER_INFO"/>
        <appender-ref ref="db"/>

        <priority value="INFO"/>
        <appender-ref ref="D"/>

        <priority value="ERROR"/>
        <appender-ref ref="E"/>

        <priority value="ERROR"/>
        <appender-ref ref="MAIL"/>
    </root>

    <!--# async -->
    <appender name="async" class="org.apache.log4j.AsyncAppender">
        <appender-ref ref="db"/>
        <param name="BufferedIO" value="true"/>
        <param name="BufferSize" value="8192"/>
    </appender>

</log4j:configuration>

5.测试工具类

package com.panda.core.tools;

import com.panda.core.log.impl.LogLogger;
import com.panda.core.log.util.LogHelper;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Random;

public class Tools {

    private static Random random = new Random();

    /**
     * 闭合随机数
     *
     * @param nMin 最小值(包含)
     * @param nMax 最大值(包含)
     * @return
     */
    public static int RandomInt(int nMin, int nMax) {
        // 为能够随机到最大值,这里需要+1,nextInt(N)的随机数不包含N
        return random.nextInt(nMax - nMin + 1) + nMin;
    }

    /**
     * 获取jar运行时路径
     *
     * @return
     */
    public static String getPath() {
        URL url = Tools.class.getProtectionDomain().getCodeSource().getLocation();
        String filePath = null;
        try {
            filePath = URLDecoder.decode(url.getPath(), "utf-8");// 转化为utf-8编码
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (filePath.endsWith(".jar")) {// 可执行jar包运行的结果里包含".jar"
            // 截取路径中的jar包名
            filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
        }

        File file = new File(filePath);

        filePath = file.getAbsolutePath();//得到windows下的正确路径
        return filePath;
    }

    public static Logger getFileLogger(String strFileName, Class<?> clazz) {
        File file = new File(getPath() + strFileName);
        Logger logger = null;
        try {
            FileInputStream istream = new FileInputStream(file);
            if (strFileName.endsWith(".xml")) {
                Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(istream);
                DOMConfigurator.configure(doc.getDocumentElement());
            } else {
                Properties props = new Properties();
                props.load(istream);
                PropertyConfigurator.configure(props);
            }
            logger = LoggerFactory.getLogger(clazz);

        } catch (java.io.IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return logger;
    }

    public static LogLogger getDBLogger(String strDBFileName, String strLog4jFileName, Class<?> clazz) {
        File file = new File(getPath() + strLog4jFileName);
        LogLogger logger = null;

        try {
            LogHelper.getInstance().startDB(strDBFileName);

            FileInputStream istream = new FileInputStream(file);
            // 判断文件是xml文件还是properties配置文件
            if (strLog4jFileName.endsWith(".xml")) {
                Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(istream);
                DOMConfigurator.configure(doc.getDocumentElement());
                logger = LogLogger.getLogger(clazz);
            }
        } catch (java.io.IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return logger;
    }
}
6.测试 及 结果

package com.panda.core.log;

import com.panda.core.log.impl.LogLogger;
import com.panda.core.tools.Tools;
import org.apache.log4j.MDC;
import org.junit.Test;

/**
 * Created by Lovell on 26/10/2016.
 */
/*
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @@@@        @@        @@       @@@@
 @@@@  @@@@@@@@  @@@@  @@  @@@   @@@
 @@@@  @@@   @@  @@@@  @@  @@@@  @@@
 @@@@  @@@@  @@  @@@@  @@  @@@   @@@
 @@@@        @@        @@       @@@@
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 */

public class LogDaoTest {

    private static LogLogger logger = Tools.getDBLogger("/log_config/log4jdb.properties", "/log_config/log4j.xml", LogDaoTest.class);

    @Test
    public void test1() throws Exception {

        logger.info("this is message ==>");
        MDC.put("userId", 84);
        logger.playerInfo("Lovell 自定义logger.info ==>");
        MDC.put("userId", 85);
        logger.info("Lovell 官方 logger.info ==>");
        MDC.put("userId", 86);
        logger.error("Lovell 官方 logger.error ==>");
    }
    
    @Test
    public void test2() throws Exception {

        MDC.put("userId", 103);
        logger.playerInfo("{}, {}, {}, {}", 103, "Lovell", "logger", "error");

        MDC.put("userId",104);
        logger.info("Lovell 官方 logger.info ==>");
        MDC.put("userId", 105);
        logger.error("{}, {}, {}, {}", 102, "Lovell", "logger", "error");
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VCHH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值