调试语句中有字符串操作的最佳做​​法是什么? [英] What is the best practice for debug statements which have string operations in them?

查看:181
本文介绍了调试语句中有字符串操作的最佳做​​法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现自己在log4net和log4j的调试语句中添加了concatonized字符串或使用字符串格式化程序,我应该使用if debug块来围绕这些调试语句,以阻止自己通过处理这些参数浪费资源,即使调试语句不会被打印出来?



我会假设检查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屋!

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