import org.apache.log4j.Logger; public class MyClass { private static final Logger logger = Logger.getLogger(MyClass.class); public void myMethod() { logger.info("This is an informative message."); logger.debug("This is a debug message with extra details."); logger.error("This is an error message with exception.", new Exception()); } }
import org.apache.log4j.Logger; import org.apache.log4j.BasicConfigurator; public class MyClass { private static final Logger logger = Logger.getLogger(MyClass.class); public static void main(String[] args) { BasicConfigurator.configure(); // configure basic logging output logger.info("Hello, world!"); // log message to console } }
import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; import org.apache.log4j.ConsoleAppender; public class MyClass { private static final Logger logger = Logger.getLogger(MyClass.class); public static void main(String[] args) { PatternLayout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %p %c - %m%n"); ConsoleAppender appender = new ConsoleAppender(layout); logger.addAppender(appender); // add custom logging output logger.warn("This is a warning message with custom format."); // log message with custom format to console } }In this example, we create a custom logging format using PatternLayout class from org.apache.log4j package. We can also create a custom logging output using ConsoleAppender class and add it to the logger instance. We can then log messages with the custom format to the console or other targets. Overall, org.apache.log4j is a widely used package library for logging messages in Java applications. Its Logger class provides powerful features for logging messages at different levels to different outputs with flexible configuration options.