import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyClass { private static final Logger LOGGER = LoggerFactory.getLogger(MyClass.class); public void doSomething() { LOGGER.debug("This is a debug message"); LOGGER.info("This is an info message"); LOGGER.error("This is an error message"); } }
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class AnotherClass { private static final Logger LOGGER = LoggerFactory.getLogger(AnotherClass.class); public static void main(String[] args) { LOGGER.info("Starting the program"); // run the program logic here LOGGER.info("Finished the program"); } }In this example, we again import the Logger interface and LoggerFactory class. This time we create a LOGGER for the AnotherClass rather than MyClass. Inside the main method, we use the LOGGER to log informational messages when the program starts and finishes. Based on the package name org.slf4j, we can determine that the examples are using the SLF4J logging library.