import org.apache.log4j.Logger; public class MyApp { private static final Logger logger = Logger.getLogger(MyApp.class); public void doSomething() { if (logger.isEnabledFor(Level.DEBUG)) { // log debug statement here } } }
import org.apache.log4j.Logger; public class MyApp { private static final Logger logger = Logger.getLogger(MyApp.class); public static void main(String[] args) { logger.debug("Debug message"); logger.info("Info message"); logger.warn("Warning message"); logger.error("Error message"); logger.fatal("Fatal message"); } }In the above example, we create an instance of the Logger class and add logging statements for different levels - DEBUG, INFO, WARN, ERROR, and FATAL. Here, we don't use the isEnabledFor method as we want to log all levels. The package/library used in these examples is org.apache.log4j.