import java.util.logging.Logger; public class MyClass { private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName()); public void myMethod() { LOGGER.trace("Entering myMethod..."); // code for myMethod LOGGER.trace("Exiting myMethod..."); } }
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyClass { private static final Logger LOGGER = LoggerFactory.getLogger(MyClass.class); public void myMethod() { LOGGER.trace("Entering myMethod..."); // code for myMethod LOGGER.trace("Exiting myMethod..."); } }This code uses the SLF4J library, which provides a simple facade over various logging frameworks. The Logger instance is obtained from the LoggerFactory and the trace method is used to log messages. Package library: - For Example 1, the Logger class is part of the java.util.logging package. - For Example 2, the LoggerFactory and Logger classes are part of the org.slf4j package, and a specific logging framework must be included as a dependency.