Java中自定义日志级别的Log4j记录

5

我已经编写了一个自定义日志级别的类,即INIT

import org.apache.log4j.Logger;
import org.apache.log4j.Level;

public class InitLoggingLevel extends Level {

    public static final String INITLOGGING_LEVEL = "INITLOGGING";
    public static final Level INIT_LOGGING = new InitLoggingLevel(
        DEBUG_INT - 4, INITLOGGING_LEVEL, 7);

    protected InitLoggingLevel(int level, String levelStr, int syslogEquivalent){
        super(level, levelStr, syslogEquivalent);
    }
}

现在我需要在log4j.properties文件中做哪些更改?我该如何在我的Java类中使用INIT日志级别?

4个回答

3

您需要:

  1. Override the method toLevel(String logArgument) like this:

    public static Level toLevel(String logArgument) {
        if (logArgument != null && logArgument.toUpperCase().equals("INITLOGGING")) {
            return INIT_LOGGING;
        }
        return (Level) toLevel(logArgument);
    }
    
  2. Write a configuration line in the log4j.properties like this:

    log4j.logger.[MY_CATEGORY]=INITLOGGING#your-package.InitLoggingLevel

就这些了!

附注:'#'语法在org.apache.log4j.helpers.OptionConverter源类中有描述。


1

1
我在请求更改log4j.properties文件,将日志级别设置为INIT。 - mayur_mitkari
1
log4j.category.com.impact.qtl12.commons=INIT,com.impact.qtl12.commons.InitLoggingLevel 这样,我该如何在Java类中执行它,就像我们为普通记录器所做的那样:logger.WARN,那么我如何通过 logger.INIT 或其他方式获得 INIT 级别呢? - mayur_mitkari

0

可以像这样:

 <category  name="xxx" additivity="false">
           <priority class="your class" value="info"/>
           <appender-ref ref="xxxlog"/>
        </category>

我在询问log4j.properties文件。 - mayur_mitkari

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接