log4j自定义日志级别和属性文件

时间:2011-06-09 11:56:30

标签: log4j

我有以下自定义日志记录级别

public class SAMLLoggingLevel extends Level{

public static final String SAMLLOGGING_LEVEL = "SAMLLOGGING";
public static final Level SAML_EXCEPTION_LOGGING = new SAMLLoggingLevel(FATAL_INT + 1, SAMLLOGGING_LEVEL, 7);
public static final Level SAML_DEBUG_LOGGING = new SAMLLoggingLevel(DEBUG_INT+1, SAMLLOGGING_LEVEL, 7);
public static final Level SAML_ERROR_LOGGING = new SAMLLoggingLevel(ERROR_INT+1, SAMLLOGGING_LEVEL, 7);

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

我尝试使用以下

进行记录
public static Logger logger = Logger.getLogger(xyz.class);

logger.log(SAMLLoggingLevel.SAML_DEBUG_LOGGING, "logger SAML_DEBUG_LOGGING");
logger.log(SAMLLoggingLevel.SAML_ERROR_LOGGING, "logger SAML_ERROR_LOGGING");
logger.log(SAMLLoggingLevel.SAML_EXCEPTION_LOGGING, "logger SAML_EXCEPTION_LOGGING");

我的log4j.properteis文件看起来像这样

log4j.logger.com.xyz=SAML_DEBUG_LOGGING, SAMLLoggingLevel
log4j.appender.SAMLLoggingLevel=org.apache.log4j.RollingFileAppender
log4j.appender.SAMLLoggingLevel.File=/SAML.log
log4j.appender.SAMLLoggingLevel.layout=org.apache.log4j.PatternLayout
log4j.appender.SAMLLoggingLevel.layout.ConversionPattern=%d{MMM dd yyyy HH:mm:ss,SSS zzz} %5p %c{1}:%L - %m%n

我从com.xyz中获取所有类的所有debub消息。我想仅限于使用logger.log(SAMLLoggingLevel ....)

记录的一次

1 个答案:

答案 0 :(得分:2)

更改您的代码
log4j.logger.com.xyz=SAML_DEBUG_LOGGING, SAMLLoggingLevel

log4j.logger.com.xyz=SAML_DEBUG_LOGGING#com.yourpackagename.SAMLLoggingLevel, SAMLLoggingLevel