没有日志被打印在日志文件中,但在控制台上打印出来了 [log4j]。

huangapple go评论83阅读模式
英文:

No log is being printed in log file, but it is getting printed on cosole [log4j]

问题

我正在尝试在我的Java项目中实现log4j,我在目录target/classes/log4j.properties下创建了log4j.properties文件。

以下是我的属性文件内容:

#根记录器选项
log4j.rootLogger=INFO, stdout, Rollfile

#将日志消息直接记录到文件
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

#将日志消息直接记录到标准输出
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"却为空。没有任何日志被记录在那里。

英文:

I am trying to implement log4j in my java project, I created log4j.properties under directory target/classes/log4j.properties

Below is my properties file content

#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

My logs are getting printed in my console out, but the logging file "azuremigrationclient_log.log" is coming empty. Nothing is getting logged there.

答案1

得分: 1

我查阅了一些文档,发现我没有指定 log4j.properties 的路径,这就是为什么控制台能够打印日志,但日志没有被打印到日志文件中(也没有创建任何文件)

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

将上述代码添加到你的类中,并在 configure 内部指定路径到你的 log4j。
例如 - E:\JavaCodeDemo\testcases\classes\log4j.properties。

这样就可以正常工作了。你的代码无法找到 log4j.properties 文件。

英文:

I went through some docs and found that I did not specify the path to log4j.properties, which is why console was able to print the log but no logs were getting printed in the log file (no file getting created either)

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

Add the above code to your class and inside configure, give path to your log4j.
eg - E:\JavaCodeDemo\testcases\classes\log4j.properties.

It will work. Your code was not able to find the log4j.properties file.

答案2

得分: 1

我阅读了评论和答案,我认为如果你尝试使用 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
英文:

I read the comments and answers and I think this could happen if you're trying to use Log4j2 with a Log4j configuration file (log4j.properties). Your configuration is really for Log4j, only missing log4j.appender.file = org.apache.log4j.RollingFileAppender, as someone already suggested. You should stick with Log4j2 and change your configuration as per its documentation (https://logging.apache.org/log4j/2.x/manual/configuration.html), e.g.

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

答案3

得分: 0

你在 rootLogger 配置中引用的名称为 Rollfile 的设置与 Appender 的标识符名 file 不匹配。这两者不一致。请参照配置文件中对于 stdout 的用法进行比较。

1:你的类型配置缺失,例如:

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

2:你应该将引用从 Rollfile 改为 file

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 的继任者。

英文:

you reference in the rootLogger configuration is called Rollfile while your identifier for the Appender is called file. This does not match. Compare to the usage of stdout in your config file.

1: your type configuration is missing e.g.:

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

2: You should try to name the reference from Rollfile to file

log4j.rootLogger=INFO, stdout, file

OR rename all your file identifiers for the configurations to 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

side note: log4j is a heavily outdated library which is no longer maintained. This means no bug-fixed or any other updates and it contains a couple of reported security vulnerabilities. If you are in control of the project and are able to choose your logging library, you should switch to something more up to date e.g. Log4j2. It has some API breaking incompatible changes, but more or less it's the successor for Log4j 1.x

huangapple
  • 本文由 发表于 2020年9月7日 22:21:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63779440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定