我正在尝试学习如何在调试标志打开时运行打印大量内容的代码。
如何在java中完成。
我能想到的一种非常天真的方式是在我编写的所有方法中都有调试标志
并写下类似
的内容 if (this.debug == true){
System.out.println("blah blah");
}
但是应该有更优雅的方式,而不是在代码中包含所有这些ifs?
另外,有没有办法可以在代码中获得某些执行的行号:
例如,如果有异常
try:
/ *行号22 * /
catch Exception e{
//print that exception occured in above line number??
}
可能是非常蹩脚的问题。 感谢
答案 0 :(得分:7)
答案 1 :(得分:2)
log4j API的简单示例
private Logger _debugLogger = Logger.getLogger(yourClassName.class);
//For info mode
_debugLogger.info("Some Messages");
//or for debug mode
_debugLogger.debug(MessageFormat.format("Some message {0},{1},{2}",variable0, variable1,variable2));