java log4j 的生产设置文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3537870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:17:47  来源:igfitidea点击:

Production settings file for log4j?

javalog4j

提问by James

Here is my current log4j settings file. Are these settings ideal for production use or is there something I should remove/tweak or change? I ask because I was getting all my threads being hung due to log4j blocking. I checked my open file descriptors I was only using 113.

这是我当前的 log4j 设置文件。这些设置是否适合生产使用,还是我应该删除/调整或更改某些内容?我问是因为由于 log4j 阻塞,我的所有线程都被挂起。我检查了我只使用 113 的打开文件描述符。

# ***** Set root logger level to WARN and its two appenders to stdout and R.
log4j.rootLogger=warn, stdout, R

# ***** stdout is set to be a ConsoleAppender.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# ***** stdout uses PatternLayout.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# ***** Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

# ***** R is set to be a RollingFileAppender.
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=logs/myapp.log
# ***** Max file size is set to 100KB
log4j.appender.R.MaxFileSize=102400KB
# ***** Keep one backup file
log4j.appender.R.MaxBackupIndex=5
# ***** R uses PatternLayout.
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %d %c - %m%n


#set httpclient debug levels
log4j.logger.org.apache.component=ERROR,stdout 
log4j.logger.httpclient.wire=ERROR,stdout 
log4j.logger.org.apache.commons.httpclient=ERROR,stdout 
log4j.logger.org.apache.http.client.protocol=ERROR,stdout

UPDATE*** Adding thread dump sample from all my threads (100)

更新*** 从我的所有线程中添加线程转储示例 (100)

"pool-1-thread-5" - Thread t@25
   java.lang.Thread.State: BLOCKED on org.apache.log4j.spi.RootLogger@1d45a585 owned by: pool-1-thread-35
    at org.apache.log4j.Category.callAppenders(Category.java:201)
    at org.apache.log4j.Category.forcedLog(Category.java:388)
    at org.apache.log4j.Category.error(Category.java:302)

回答by Vadzim

Log4j 1.2 is vulnerable to deadlockswhen toString()produces nested logging.

Log4j 1.2在生成嵌套日志记录时容易出现死锁toString()

See old aged still unresolved issues like Log4J can create deadlock conditions (concurrent package donation)and Deadlock with RollingFileAppender.

请参阅旧的仍未解决的问题,例如Log4J 可以创建死锁条件(并发包捐赠)使用 RollingFileAppender 的死锁

It also has performance-killing lock synchronization issues under heavy concurrent load. Like Category callAppenders synchronization causes java.lang.Thread.State: BLOCKEDand Move org.apache.log4j.Category to reentrant read/write locks.

在高并发负载下,它还存在性能致命的锁同步问题。像Category callAppenders 同步导致 java.lang.Thread.State: BLOCKEDMove org.apache.log4j.Category to reentrant read/write locks

Even AsyncAppenderis not free of excessive locks: AsyncAppender.doAppend() does not need to be synchronizedand Deadlock in 1.2.15 caused by AsyncAppender and ThrowableInformation classes. Also beware of AsyncAppender overflow.

即使是AsyncAppender 也不是没有过多的锁: AsyncAppender.doAppend() 不需要同步1.2.15 中由 AsyncAppender 和 ThrowableInformation 类引起的死锁。还要注意AsyncAppender 溢出

One caveat is to always constrain root category level to at least INFO or higher. This would prevent logging calls from acquiring unnecessary locks mentioned in the above issues. Just limiting appender threshold is not sufficient as it is taken into account later. See explanation with publish/subscribe analogy:

一个警告是始终将根类别级别限制为至少 INFO 或更高。这将防止日志记录调用获取上述问题中提到的不必要的锁。仅限制 appender 阈值是不够的,因为稍后会考虑。请参阅发布/订阅类比的解释

To answer your question about how threshold interacts with category, basically think of it is as a publish/subscribe. The category sets what is published by the logger, the threshold sets the subscription level of the appender.

要回答有关阈值如何与类别交互的问题,基本上可以将其视为发布/订阅。类别设置记录器发布的内容,阈值设置附加程序的订阅级别。

Nested categories of interest can be individually given lower priorities if needed.

如果需要,可以单独为感兴趣的嵌套类别赋予较低的优先级。

回答by AngerClown

Are you creating a Logger for each class using the standard private static final Logger logger = Logger.getLogger(Foo.class);where Foo is the class in which the logger is declared? If you only have 1 Logger instance in your entire application, there could be some contention if there is a lot of logging.

您是否使用标准为每个类创建记录器,private static final Logger logger = Logger.getLogger(Foo.class);其中 Foo 是声明记录器的类?如果整个应用程序中只有 1 个 Logger 实例,那么如果有大量日志记录,则可能会出现一些争用。

回答by Yoni

%F:%L has serious performance impact. Although I don't see how they'd cause locking, I'd consider omitting them for production.

%F:%L 有严重的性能影响。虽然我不知道它们是如何导致锁定的,但我会考虑在生产中省略它们。

回答by Gary

This looks normal. I don't see how this alone could cause log4j blocking. Maybe you could post a thread dump of your issue?

这看起来很正常。我不明白仅此一项如何导致 log4j 阻塞。也许您可以发布您的问题的线程转储?