Log information efficiently (with Log4J)Tag(s): Environment
In production code, a good practice is to only log what is necessary in the context.
With Log4J, you can have different levels of message written into your log.
public class MyClass { /** * Logger log4j */ static Logger logger = Logger.getLogger(MyClass.class.getName()); ... logger.debug("I'm here"); logger.info(e.getMessage()); logger.warning("something wrong " + e.getMessage()); logger.error("omg " + e.getMessage()); ...
While you can set the level in your code with something like
logger.setLevel(Level.WARN);
; can be OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL log4j.threshold=WARN
URL url = ClassLoader.getSystemResource("log4j.props"); PropertyConfigurator.configure(url);
Logging activities have a cost. Even if you set the level at DEBUG level, the logger.debug(...) is interpreted. Consider this line :
logger.setLevel(Level.DEBUG); logger.debug("something wrong with the value of " + myValue.toString());
The best practice is to check if the level is enabled or not.
logger.setLevel(Level.DEBUG); if (logger.isDebugEnabled()) { logger.debug("something wrong with the value of " + myValue.toString()); }
You can even remove the unwanted logging operation from the bytecode! See this HowTo.
Finally it's a good idea to design a robust toString() method for your class like this one :
public String toString(){ StringBuffer sb = new StringBuffer(); sb.append(getClass().getName()) sb.append(" myValue=[").append(this.myValue).append("]"); sb.append(" anotherValue=[").append(this.anotherProperty).append("]"); return sb.toString(); }
See also this HowTo and this one
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com