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”将变为空。那里没有任何记录
# 1 楼答案
rootLogger配置中的引用称为
Rollfile
,而Appender的标识符称为file
。这不匹配。与配置文件中stdout
的用法进行比较1:缺少您的类型配置,例如:
2:您应该尝试将引用命名为从
Rollfile
到file
或者将配置的所有
file
标识符重命名为Rollfile
:旁注:log4j是一个严重过时的库,不再维护。这意味着没有修复错误或任何其他更新,并且它包含两个报告的安全漏洞。如果您控制了项目,并且能够选择日志库,那么您应该切换到更为最新的日志库,例如
Log4j2
。它有一些破坏API的不兼容更改,但或多或少它是Log4j 1的继承者。x# 2 楼答案
我查看了一些文档,发现我没有指定log4j的路径。属性,这就是为什么console能够打印日志,但没有在日志文件中打印日志(也没有创建文件)
将上述代码添加到您的类中,并在configure内部为您的log4j提供路径。 例如-E:\JavaCodeDemo\testcases\classes\log4j。财产
它会起作用的。您的代码无法找到log4j。属性文件
# 3 楼答案
我阅读了注释和答案,我认为如果您试图将Log4j2与Log4j配置文件(Log4j.properties)一起使用,可能会发生这种情况。您的配置实际上是针对Log4j的,只是缺少了
log4j.appender.file = org.apache.log4j.RollingFileAppender
,正如有人已经建议的那样。您应该坚持使用Log4j2,并根据其文档(https://logging.apache.org/log4j/2.x/manual/configuration.html)更改配置,例如