java Log4j 日志会降低应用程序性能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11117916/
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-31 03:52:42  来源:igfitidea点击:

Log4j logs decreases application performance?

javalogginglog4j

提问by happy

Does logging decreases application performance? and how to restrict display-tags logs to be printed in log files?

日志记录会降低应用程序性能吗?以及如何限制在日志文件中打印的显示标签日志?

eg. my log file has below logs

例如。我的日志文件有以下日志

[2012-06-20 15:52:06,290] org.displaytag.tags.TableTag isFirstIteration 684 - [data] first iteration=true (row number=1)
[2012-06-20 15:52:06,290] org.displaytag.tags.TableTag isFirstIteration 684 - [data] first iteration=true (row number=1)
[2012-06-20 15:52:06,290] org.displaytag.tags.TableTag isFirstIteration 684 - [data] first iteration=true (row number=1)
[2012-06-20 15:52:06,290] org.displaytag.tags.TableTag isFirstIteration 684 - [data] first iteration=true (row number=1)

why the above is in log file?

为什么上面的内容在日志文件中?

log.properties file

log.properties 文件

# Log4j configuration file.
 log4j.rootCategory=DEBUG, A1
 # Available levels are DEBUG, INFO, WARN, ERROR, FATAL

 #
 # A1 is a ConsoleAppender 
 #

log4j.appender.A1 = org.apache.log4j.RollingFileAppender
log4j.appender.A1.File = C:/LogInfo/logfile.log
log4j.appender.A1.MaxFileSize = 100MB
log4j.appender.A1.MaxBackupIndex=50
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.append = true
log4j.appender.A1.layout.ConversionPattern = [%d] %C %M %L - %m%n
log4j.appender.A1.Threshold = DEBUG

how to stop (org.displaytag.tags.TableTag) these kind of logs to be printed in log files

如何停止(org.displaytag.tags.TableTag)这类日志打印在日志文件中

回答by Stephen C

Does logging decreases application performance?

日志记录会降低应用程序性能吗?

Yes. How much it does depends on a number of factors; see below.

是的。它的作用有多大取决于许多因素;见下文。

and how to restrict display-tags logs to be printed in log files?

以及如何限制在日志文件中打印的显示标签日志?

By changing the ConversionPattern in the logging properties

通过更改日志属性中的 ConversionPattern

why the above is in log file?

为什么上面的内容在日志文件中?

Because:

因为:

  1. somewhere in the code is a call to a Logger method (probably debug(String)) with that message, and
  2. your logging properties set the logging Threshold to DEBUG for the appender.
  1. 代码中的某处是debug(String)使用该消息调用 Logger 方法(可能),并且
  2. 您的日志记录属性将附加程序的日志记录阈值设置为 DEBUG。

To improve performance:

提高性能:

  1. change the ConversionPattern to use less expensive date/time formatting, and (more importantly) avoid 'C', 'F', 'L' and 'M' because they are particularly expensive.
  2. change the logging Threshold to INFO or WARNING or ERROR to reduce the amount of logging,
  3. put the Logger.debug(...)call inside an ifstatement that checks that debug logging is enabled. This saves the cost of assembling the log message in cases where it won't be needed; see In log4j, does checking isDebugEnabled before logging improve performance?.
  4. with log4j version 2 (log4j2), there are overloads that on the logging methods that take a format and parameters. These reduce the overhead when a logging at a level that is disabled.
  5. look also at logback.
  1. 更改 ConversionPattern 以使用更便宜的日期/时间格式,并且(更重要的是)避免使用“C”、“F”、“L”和“M”,因为它们特别昂贵。
  2. 将日志记录阈值更改为 INFO 或 WARNING 或 ERROR 以减少日志记录量,
  3. Logger.debug(...)调用放在if检查调试日志记录已启用的语句中。这节省了在不需要的情况下组装日志消息的成本;请参阅在 log4j 中,是否在记录之前检查 isDebugEnabled 会提高性能?.
  4. 使用 log4j 版本 2 (log4j2),在采用格式和参数的日志记录方法上存在重载。当日志处于禁用级别时,这些会减少开销。
  5. 也看看logback。

You can also throttle logging at the Loggerlevel ... as described in the log4j documentation. In fact, the documentation answers most of the questions that you asked, and has a lot of detail on the topics of logging performance and logging configuration.

您还可以在Logger级别限制日志记录...如log4j 文档中所述。事实上,该文档回答了您提出的大部分问题,并且在日志记录性能和日志记录配置主题上有很多详细信息。

回答by Guido

Short answer: yes, it decreases application performance as it uses some CPU cycles and other resources (memory, etc).

简短回答:是的,它会降低应用程序性能,因为它使用了一些 CPU 周期和其他资源(内存等)。

See also this question : log4j performance

另请参阅此问题:log4j 性能

回答by Peter Lawrey

Logging can be 30% of you cpu time or more. In terms of jitter, it as large (and more often) than your GC delays.

日志记录可能占用您 cpu 时间的 30% 或更多。就抖动而言,它与您的 GC 延迟一样大(而且更频繁)。

A simple way to reduce overhead is to use the Pattern to turn off where you are logging each message from. In your case this is %C %M and %L as it has to take a stack trace (of the entier stack) to get this information.

减少开销的一种简单方法是使用 Pattern 关闭记录每条消息的位置。在您的情况下,这是 %C %M 和 %L 因为它必须进行堆栈跟踪(整个堆栈的)才能获取此信息。

回答by Thihara

Yes they do. That's why you should only log an error or something that must absolutely be logged. You can also log information helpful for debugging in the debug channel so it won't affect production performance.

是的,他们这样做。这就是为什么您应该只记录错误或必须绝对记录的内容。您还可以在调试通道中记录有助于调试的信息,这样它就不会影响生产性能。

回答by Akhi

You can restrict junk logs like this. Set the root logger as INFO so that unnecessary debug logs won't come and fill up your log file.

您可以像这样限制垃圾日志。将根记录器设置为 INFO,这样不必要的调试日志就不会出现并填满您的日志文件。

log4j.rootCategory=INFO, A1

If you want specific class or package to give out DEBUG logs you can do it like this.

如果您希望特定的类或包提供调试日志,您可以这样做。

log4j.logger.org.hibernate.event.def.DefaultLoadEventListener=DEBUG,A1

The above will print DEBUG level logs from the class DefaultLoadEventListener in your log file along with other INFO level logs.

以上将打印日志文件中 DefaultLoadEventListener 类中的调试级别日志以及其他 INFO 级别日志。

回答by user1419950

how about?

怎么样?

log4j.category.org.displaytag.tags.TableTag=ERROR, A1