0

下面是 log4j 属性和 Java 代码。所有日志都仅在调试中记录。我什至尝试添加阈值,但还没有运气。我需要根据日志级别将日志语句记录在单独的文件中。

所有日志都只记录在调试日志文件中,如果我删除调试日志,那么它会记录在信息日志文件中。

给予阈值选项也不起作用。

log4j.rootLogger = OFF

#define the console appender
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender
# now define the layout for the appender
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# now define the rollingFIle appender
log4j.category.com=error, myErrorappender
log4j.additivity.com=false
log4j.appender.myErrorappender=org.apache.log4j.RollingFileAppender
log4j.appender.myErrorappender.File=/Users/uthaman/Documents/workspace/Logging/myErrorlog.log
log4j.appender.myErrorappender.MaxFileSize=2MB
log4j.appender.myErrorappender.MaxBackupIndex=2
log4j.appender.myErrorappender.layout = org.apache.log4j.PatternLayout
log4j.appender.myErrorappender.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c (%F:%L) - %m%n


log4j.category.com=info,myInfoappender
log4j.additivity.com=false
log4j.appender.myInfoappender=org.apache.log4j.RollingFileAppender
log4j.appender.myInfoappender.File=/Users/user/Documents/workspace/Logging/myInfolog.log
log4j.appender.myInfoappender.MaxFileSize=2MB
log4j.appender.myInfoappender.MaxBackupIndex=2
log4j.appender.myInfoappender.layout = org.apache.log4j.PatternLayout
log4j.appender.myInfoappender.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c (%F:%L) - %m%n

log4j.category.com=debug,myDebugappender
log4j.additivity.com=false
log4j.appender.myDebugappender=org.apache.log4j.RollingFileAppender
log4j.appender.myDebugappender.File=/Users/user/Documents/workspace/Logging/myDebuglog.log
log4j.appender.myDebugappender.MaxFileSize=2MB
log4j.appender.myDebugappender.MaxBackupIndex=2
log4j.appender.myDebugappender.layout = org.apache.log4j.PatternLayout
log4j.appender.myDebugappender.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c (%F:%L) - %m%n

这是记录日志语句的 java 代码:

public class Logging {

    private static Logger logger = Logger.getLogger(Logging.class);

    public static void main(String[] args) {
        logger.error("This is error");
        logger.info("This is info");
        logger.debug("This is debug");
    }

}
4

3 回答 3

1

我更喜欢使用log4j.xml配置而不是log4j.properties. 以下配置(基本配置)按需要工作...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
    <appender name="fileAppender" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="file" value="log.info" />
        <param name="datePattern" value="'.'yyyy-MM-dd" />
        <param name="append" value="true" />
        <param name="threshold" value="info"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %C{6} - %m%n" />
        </layout>
    </appender>

    <appender name="fileAppenderDebug" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="file" value="log.debug" />
        <param name="datePattern" value="'.'yyyy-MM-dd" />
        <param name="append" value="true" />
        <param name="threshold" value="debug"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %C{6} - %m%n" />
        </layout>
    </appender>

    <appender name="fileAppenderError" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="file" value="log.error" />
        <param name="datePattern" value="'.'yyyy-MM-dd" />
        <param name="append" value="true" />
        <param name="threshold" value="error"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %C{6} - %m%n" />
        </layout>
    </appender>

    <root>
        <priority value="debug" />
        <appender-ref ref="fileAppender" />
        <appender-ref ref="fileAppenderDebug" />
        <appender-ref ref="fileAppenderError" />
    </root>
</log4j:configuration>
于 2013-01-19T20:57:17.433 回答
1

您可以尝试filter在您的配置文件中使用指定一个specific level要写入的appender. 请附上示例代码

    <appender name="DEBUG" class="org.apache.log4j.ConsoleAppender">
            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern" value="[%t] %-5p %c - %m%n" />
            </layout>
            <filter class="org.apache.log4j.varia.LevelRangeFilter">
                    <param name="levelMin" value="DEBUG" />
                    <param name="levelMax" value="DEBUG" />
            </filter>
    </appender>
于 2013-02-06T12:31:07.037 回答
1

问题:

您在 log4j 配置中定义了重复键:

    log4j.category.com=error, myErrorappender
    log4j.additivity.com=false

    log4j.category.com=info,myInfoappender
    log4j.additivity.com=false

    log4j.category.com=debug,myDebugappender
    log4j.additivity.com=false
  • Log4j使用最后一个调试附加程序--->“所有日志都只记录在调试日志文件中”
  • 当您删除调试附加程序时,信息附加程序将是最后一个 --->“如果我删除调试日志,那么它将被记录到信息日志文件中。”

解决方案:

    log4j.category.com=debug,myDebugappender,myInfoappender,myErrorappender
    log4j.additivity.com=false
    log4j.appender.myDebugappender.Threshold=DEBUG
    log4j.appender.myInfoappender.Threshold=INFO
    log4j.appender.myErrorappender.Threshold=ERROR

如果您必须通过精确级别匹配过滤事件:

     log4j.appender.myDebugappender.filter=org.apache.log4j.varia.LevelMatchFilter
     log4j.appender.myDebugappender.filter.levelToMatch=DEBUG
     ...
于 2013-01-20T07:40:26.343 回答