import org.apache.log4j.Logger; public class MyClass { private static final Logger logger = Logger.getLogger(MyClass.class); public static void main(String[] args) { logger.warn("This is a warning message."); } }
import org.apache.log4j.*; public class MyClass { private static final Logger logger = Logger.getLogger(MyClass.class); public void someMethod() { if (someCondition) { logger.warn("Some condition was met."); } } }In this example, the someMethod method checks a condition and logs a warning message if it is true. The logger variable is again initialized with the Logger.getLogger method, passing in the current class as an argument. The org.apache.log4j package is part of the Apache log4j library.