有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java没有日志被打印在日志文件中,但它正在cosole[log4j]上打印

我试图在我的java项目中实现log4j,我创建了log4j。目录target/classes/log4j下的属性。性质

下面是我的属性文件内容

#Root logger option 
log4j.rootLogger=INFO, stdout, Rollfile

#Direct log messages to file
log4j.appender.file.File=E:\\TestLogger\\azuremigrationclient_log.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

#Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

我的日志将在控制台中打印出来,但日志文件“azuremigrationclient_log.log”将变为空。那里没有任何记录


共 (3) 个答案

  1. # 1 楼答案

    rootLogger配置中的引用称为Rollfile,而Appender的标识符称为file。这不匹配。与配置文件中stdout的用法进行比较

    1:缺少您的类型配置,例如:

    log4j.appender.file = org.apache.log4j.RollingFileAppender
    

    2:您应该尝试将引用命名为从Rollfilefile

    log4j.rootLogger=INFO, stdout, file
    

    或者将配置的所有file标识符重命名为Rollfile

    log4j.appender.Rollfile.File=E:\\TestLogger\\azuremigrationclient_log.log
    log4j.appender.Rollfile.MaxFileSize=10MB
    log4j.appender.Rollfile.MaxBackupIndex=10
    log4j.appender.Rollfile.layout=org.apache.log4j.PatternLayout
    log4j.appender.Rollfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    

    旁注:log4j是一个严重过时的库,不再维护。这意味着没有修复错误或任何其他更新,并且它包含两个报告的安全漏洞。如果您控制了项目,并且能够选择日志库,那么您应该切换到更为最新的日志库,例如Log4j2。它有一些破坏API的不兼容更改,但或多或少它是Log4j 1的继承者。x

  2. # 2 楼答案

    我查看了一些文档,发现我没有指定log4j的路径。属性,这就是为什么console能够打印日志,但没有在日志文件中打印日志(也没有创建文件)

    static {
     PropertyConfigurator.configure("log4j.properties");
    }
    

    将上述代码添加到您的类中,并在configure内部为您的log4j提供路径。 例如-E:\JavaCodeDemo\testcases\classes\log4j。财产

    它会起作用的。您的代码无法找到log4j。属性文件

  3. # 3 楼答案

    我阅读了注释和答案,我认为如果您试图将Log4j2与Log4j配置文件(Log4j.properties)一起使用,可能会发生这种情况。您的配置实际上是针对Log4j的,只是缺少了log4j.appender.file = org.apache.log4j.RollingFileAppender,正如有人已经建议的那样。您应该坚持使用Log4j2,并根据其文档(https://logging.apache.org/log4j/2.x/manual/configuration.html)更改配置,例如

    appender.console.type = Console
    appender.console.name = STDOUT
    appender.console.layout.type = PatternLayout
    appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    appender.console.filter.threshold.type = ThresholdFilter
    appender.console.filter.threshold.level = info
    
    appender.rolling.type = RollingFile
    appender.rolling.name = RollingFile
    appender.rolling.fileName = E:\\TestLogger\\azuremigrationclient_log.log
    appender.rolling.filePattern = E:\\TestLogger\\azuremigrationclient-%d{MM-dd-yy-HH-mm-ss}-%i_log.log.gz
    appender.rolling.layout.type = PatternLayout
    appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    appender.rolling.policies.type = Policies
    appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
    appender.rolling.policies.size.size=10MB
    appender.rolling.strategy.type = DefaultRolloverStrategy
    
    rootLogger.level = info
    rootLogger.appenderRef.stdout.ref = STDOUT
    rootLogger.appenderRef.rolling.ref = RollingFile