专注JavaWeb开发 首页制式『默认式 OR 时间轴 网站地图|RSS订阅|设为首页|加入收藏
当前位置:首页 >Java >JavaEE初级 > 正文

自定义log4j的级别

来源: 开源中国社区 作者: CrazyHarry 发布时间: 2015-03-27 浏览次数:

按照常规思路要将一些项目中的记录信息要使用log4j记录下来,直接使用log4j的debug或者info等级别都是可以记录得。但是本人不想在自己的文件中出现不相关的log信息也不想看到log4j级别的名称。因而学习了自定义log4j级别,在此分享下

/**
 * 自定义LOG4j级别LIFE
 *
 * Created by CrazyHarry on 2015/3/27.
 */
public class Life extends Level {
    private static final long serialVersionUID = 1L;
 
    protected Life(int level, String levelStr, int syslogEquivalent) {
        super(level, levelStr, syslogEquivalent);
    }
 
    /**
     * 定义log的权重为介于OFF和FATAL之间,便于打印LIFE级别的log
     */
    public static final int LIFE_INT = OFF_INT - 10;
 
    public static final Level LIFE = new Life(LIFE_INT, "LIFE", 10);
 
    public static Level toLevel(String logArgument) {
        if (logArgument != null && logArgument.toUpperCase().equals("LIFE")) {
            return LIFE;
        }
        return (Level) toLevel(logArgument);
    }
 
    public static Level toLevel(int val) {
        if (val == LIFE_INT) {
            return LIFE;
        }
        return (Level) toLevel(val, Level.DEBUG);
    }
 
    public static Level toLevel(int val, Level defaultLevel) {
        if (val == LIFE_INT) {
            return LIFE;
        }
        return Level.toLevel(val, defaultLevel);
    }
 
    public static Level toLevel(String logArgument, Level defaultLevel) {
        if (logArgument != null && logArgument.toUpperCase().equals("LIFE")) {
            return LIFE;
        }
        return Level.toLevel(logArgument, defaultLevel);
    }
}

2. [代码]log4j配置文件log4j.xml 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
                     debug="false">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d - %c -%-4r [%t] %-5p %x - %m%n" />
        </layout>
    </appender>
 
    <!--每天一个文件LOG文件 -->
    <appender name="dailyRollingFile" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="encoding" value="UTF-8"></param>
        <param name="Threshold" value="ERROR"></param>
        <param name="ImmediateFlush" value="true"></param>
        <param name="File" value="${catalina.base}/logs/buglife/log"></param>
        <param name="DatePattern" value="'_'yyyyMMdd'.log'"></param>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                   value="[%d{yyyy-MM-dd HH:mm:ss\} %-5p] [%t] {%c:%L}-%m%n"></param>
        </layout>
    </appender>
 
    <!--对指定目录打LIFE日志到上面所定义的文件里-->
    <category name="cn.buglife.webutil.life">
        <priority value="LIFE" class="cn.buglife.webutil.web.util.Life" />
        <level value="LIFE" />
        <appender-ref ref="console" />
        <appender-ref ref="dailyRollingFile" />
    </category>
    <root>
        <priority value="INFO" />
        <appender-ref ref="console" />
        <appender-ref ref="dailyRollingFile" />
    </root>
</log4j:configuration>

3. [代码]web.xml中的配置

<context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.xml</param-value>
    </context-param>
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>6000</param-value>
    </context-param>

4. [代码]调用实例 

package cn.buglife.webutil.life;
 
import cn.buglife.webutil.web.util.Life;
import org.apache.log4j.Logger;
 
/**
 * Created by CrazyHarry on 2015/3/27.
 */
public class TestLogLevel {
    private static final Logger LOGGER = Logger.getLogger(TestLogLevel.class);
 
    public static void main(String args[]) {
        LOGGER.log(Life.LIFE,"生活真美好");
    }
}


QQ群: 武汉地区(142173356)验证消息JavaWeb1024

网友共有【】评论,点击查看


相关新闻:


x 微信关注我们 随时获取最新网站资讯