import org.apache.log4j.Logger; public class MyClass { private static final Logger LOGGER = Logger.getLogger(MyClass.class); public static void main(String[] args) { try { // some code that may throw an exception } catch (Exception e) { LOGGER.fatal("An unexpected error occurred", e); // print stack trace e.printStackTrace(); } } }
import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Logger; public class MyClass { static { BasicConfigurator.configure(); } private static final Logger LOGGER = Logger.getLogger(MyClass.class); public static void main(String[] args) { LOGGER.fatal("System is shutting down"); } }In this example, we configure the basic logging properties using the BasicConfigurator class and define a Logger instance for our class. We use its fatal method to log a message that indicates the system is shutting down due to a critical error. The org.apache.log4j.Logger fatal method belongs to the Apache Log4j library, which is a package library for Java that provides a flexible logging framework for different types of applications. It allows developers to customize various logging properties, such as logging levels, appenders, and formatters, to meet their specific logging requirements.