问题描述
我正在在Java应用程序中实施日志记录,以便我可以调试一旦应用程序进行生产,可能会发生的潜在问题.
考虑在这种情况下,人们不会使用IDE,开发工具(以调试模式运行事物或步骤彻底代码),将class名称,方法名称和行号与使用每个消息.
我正在搜索网络以获取登录的最佳实践,我遇到了本文说:
您绝不应该包括文件名,班级名称和行号, 虽然很诱人.我什至看过空日志语句 从代码发行:
log.info("");因为程序员认为该行号将是 记录模式,他知道"如果出现空记录消息 在文件的第67行(在Authenticate()方法中),这意味着 用户已验证".此外,记录班级名称,方法名称 和/或行号具有严重的性能影响.
我试图了解记录类名称,方法名称和行号降低性能的方式.
上述所有记录框架还是其中的一些是正确的吗? (作者在同一主题中提到Logback).我有兴趣了解Log4j中这样做的性能的影响.
.推荐答案
这里有不同的担忧. 首先,记录简单字符串的影响是什么?这在很大程度上取决于您使用的基础架构.最近,我确实运行了一些基准测试,并且仅使用标准Java记录API将字符串记录到文件非常昂贵.您将使用Log4J记录基础架构获得更好的结果,我的测试显示,该基础架构的顺序更快.
.现在让我们考虑文件名和行号问题.与C相反,Java没有__file__和__line__常数,而__line__ consance则由编译器(或c的预处理程序)解决.如果要记录文件名和行号,则有两个选项:
问题描述
I am implementing logging in my java application , so that I can debug potential issues that might occur once the application goes in production.
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("");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.
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.
推荐答案
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.
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:
- 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
- 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.