1

I am trying to learn the trick on how to run the code which prints a lot of stuff when the debug flag is on.

How is that done in java.

A very naive way I can think of is have the debug flag in all the methods i write

and write something like

 if (this.debug == true){
 System.out.println("blah blah");
  }

But there should be much more elegant way rather than having all these ifs in code?

Also, is there a way I can get the line number of certain execution in code:

For example if there is an exception

            try:

/* line number 22 */

 catch Exception e{
         //print that exception occured in above line number??
    }

Probably very lame questions. THanks

0

2 Answers 2

7

Use logging framework that will do it for you - for example : log4j, slf4j

For example:

log.debug("some text");

now in logging configuration properties file choose to enable disabled debug logging statements

See

Sign up to request clarification or add additional context in comments.

Comments

2

Simple example from 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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.