views:

100

answers:

5

Hi,

that may be a dumb question... but I have a quite large java program that i'm debugging using the old System.err.println method, like I do using print* in any other language...

But after a while, I've so many of them, I don't know how to manage them (and sometimes I put twice the same message concatenated with other information making it even harder...)...(and suppressing/finding them in the end is sometimes not trivial

I don't know how to be more rigourous... Do you have like a kind of magic token ? do you always print out the location of the println...

If you've any java tool, I'd consider them but best-practices in general would be great...

+7  A: 

Perhaps give logging a try?

Rather to piping messages out to standard output, have the logger take care of the logging for you. Also, being able to set different logging levels will allow for varying granularity for the log messages which are recorded.

coobird
See Daniel Moura's answer for specific logging frameworks. Both log4j and commons are de-facto standards in java logging and work great.
Mike Reedell
A: 

Yes - I always print location of the print. It was possible even in C with LINE macro. Try log4j it also prints the location of everything.

agsamek
A: 

Eclipse Java Development Tool (JDT) is very efficient for Java debugging.

Personally I stopped debugging using println since I discovered this tool. If you don't know it, I think it worth a try.

Manu

Manuel Selva
+1  A: 

If you're using Eclipse you can use the "systrace" template (type systrace then ctrl+space). You edit the template to include file name, line number, etc (although the line number will be wrong if you move lines, there isn't a pretty macro like in C/C++)

On the other hand, you should be using a logging framework like java's build in logging, commons-logging, or log4j ... for LOGGING and you should be using a DEBUGGER like Eclipse for debugging.

basszero
+5  A: 

For java you should try log4j. With log4j you can set levels (TRACE, DEBUG, INFO, WARN, ERROR and FATAL) for the messages. You can choose (and change) the output of the log messages, like show the logs in the console, save in a file, ...

And you can configure the message formatting (show hour, show name of the class, ...).

You should try commons logging which works as an interface for the logging tools like log4j.

Daniel Moura
prefer logback / slf4j over log4j / commons logging
KitsuneYMG
why logback / slf4j are better ?
LB