日志记录类名称,方法名称和行号的性能影响 [英] Performance Impact of logging class name , method name and line number

查看:239
本文介绍了日志记录类名称,方法名称和行号的性能影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Java应用程序中实现日志记录,以便调试应用程序投入生产后可能发生的潜在问题.

I am implementing logging in my java application , so that I can debug potential issues that might occur once the application goes in production.

考虑到在这种情况下,人们可能不会奢侈地使用IDE,开发工具(以调试模式运行代码或通过一步一步的代码运行代码),使用以下命令记录类名,方法名和行号将非常有用.每条消息.

Considering in such cases one wouldn't have the luxury of using an IDE , development tools (to run things in debug mode or step thorough code) , it would be really useful to log class name , method name and line number with each message.

我正在网上搜索日志记录的最佳做法,但遇到了

I was searching the web for best practices for logging and I came across this article which says:

您永远不应包含文件名,类名和行号, 虽然很诱人.我什至看到空的日志语句 从代码中发出:

You should never include file name, class name and line number, although it’s very tempting. I have even seen empty log statements issued from the code:

log.info("");

因为程序员认为行号将是其中的一部分 日志模式,他知道如果出现空日志消息 在文件的第67行中(在authenticate()方法中),这意味着 用户已通过身份验证".另外,记录类名,方法名 和/或行号会严重影响性能.

because the programmer assumed that the line number will be a part of the logging pattern and he knew that "If empty logging message appears in 67th line of the file (in authenticate() method), it means that the user is authenticated". Besides, logging class name, method name and/or line number has a serious performance impact.

我试图了解记录类名称,方法名称和行号如何降低性能.

I am trying to understand how logging class name , method name and line number degrade performance.

以上情况是否适用于所有日志记录框架或仅适用于其中某些框架? (作者在同一主题中引用了Logback).我有兴趣了解在Log4j中执行这样的操作对性能的影响.

Is the above true for all logging frameworks or only some of them? (The author makes a reference to Logback in the same topic) . I am interested in knowing about performance impacts of doing something like this in Log4j.

推荐答案

这里有不同的关注点. 首先,记录一个简单的字符串有什么影响.这在很大程度上取决于您使用的基础架构.最近,我确实运行了一些基准测试,仅使用标准的Java日志记录API将字符串记录到文件中就非常昂贵.使用log4j日志记录基础结构,您将获得更好的结果,我的测试表明,该基础结构的速度要快15或20倍.

There are different concerns at play here. First of all, what is the impact of logging a simple string. This largely depends on the infrastructure you use. Recently I did run some benchmarks, and just logging a string to a file using the standard java logging API is extremely expensive. You're going to get better results using the log4j logging infrastructure, which my tests show are in the order of 15 or 20 times faster.

现在让我们考虑文件名和行号问题.与C相对,java没有__FILE__和__LINE__常量,这些常量由编译器(或在C的情况下为预处理器)解析.如果要记录文件名和行号,则有两个选择:

Now let's consider the file name and line number problem. As opposed to C, java doesn't have a __FILE__ and __LINE__ constant which are resolved by the compiler (or the preprocessor in case of C). If you want to log file name and line number, you have two options:

  1. 您实际上自己将这些文件名和行号写为常量.这对于文件名可能是可以接受的,但是如果您在要记录的行上方引入任何行,则行号将会更改,因此您必须去那里更改所有行号.不太合理
  2. 您使用Java API来获取堆栈跟踪,如所述此处.但是,此操作在运行时非常昂贵,因此会降低程序速度.
  1. You actually write such file name and line number yourself as constants. This may be acceptable for the filename, but the line number will change if you introduce any line above the one where you're logging, so you will have to go and change all the line numbers there. Not really reasonable
  2. You use java APIs to get a stack trace as mentioned here. This operation though is very expensive at runtime, and hence will slow down your program.

这篇关于日志记录类名称,方法名称和行号的性能影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆