调试语句中有字符串操作的最佳做法是什么? [英] What is the best practice for debug statements which have string operations in them?
问题描述
我会假设检查if(isDebug)是否比使用字符串操作更快更有效率,但是会导致当调试级别设置为高于调试时,程序的运行方式不同(更快),这可能意味着在写入日志时不会发生生产中发生的同步问题。
对于Java,您可以尝试 log5j 。
log4j:
log.debug( + foo +由于bar:+ bar +这个东西:+ car);
log5j:
code> log.debug(这个东西破了:%s由于bar:%s在这个东西:%s,foo,bar,car);
log.debug(Exception#%d,aThrowable,exceptionsCount ++);
I often find myself adding either concatonated strings or using a string formatter in my debug statements in log4net and log4j should I surround these debug statements with an "if debug" block to stop myself from wasting resources by processing these parameters even though the debug statement will not be printed out?
I would assume that checking if (isDebug) would be quicker and more efficient than having the string operations occuring, however it would lead to the program operating differently (faster) when the debug level is set higher than debug, which could mean that synchronisation problems that happen in production don't happen when I'm writing to the log.
for Java you can try log5j.
log4j:
log.debug("This thing broke: " + foo + " due to bar: " + bar + " on this thing: " + car);
log5j:
log.debug("This thing broke: %s due to bar: %s on this thing: %s", foo, bar, car);
log.debug("Exception #%d", aThrowable, exceptionsCount++);
这篇关于调试语句中有字符串操作的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!