import org.apache.log4j.Logger; public class MyClass { final static Logger logger = Logger.getLogger(MyClass.class); public static void main(String[] args) { try { // some code } catch (Exception e) { logger.error("Error occurred: " + e.getMessage()); } } }
import org.apache.log4j.Logger; public class MyOtherClass { final static Logger logger = Logger.getLogger(MyOtherClass.class); public void doSomething() { logger.warn("This method is deprecated!"); // some code } }In this example, we create a Logger object for MyOtherClass and use it to log a warning message when a deprecated method is called. In both examples, we use the Logger error to log different types of messages during runtime, including errors, warnings, and information. The Apache log4j package library provides additional features and configurations for logging, including logging levels, appenders, and layouts.