Using Log4J
9
Log4J is a really nice Library that aims to facilitate outputting debugging information, but also relevant data, in Java applications. It is highly configurable, using both Categories and Class-Based rules, so that developers can narrow down the problems very well when debugging applications.
import org.apache.log4j.*;
// How to use log4j
public class TestLogging {
// Initialize a logging category. Here, we get THE ROOT CATEGORY
//static Category cat = Category.getRoot();
// Or, get a custom category
static Category cat = Category.getInstance(TestLogging.class.getName());
// From here on, log away! Methods are: cat.debug(your_message_string),
// cat.info(...), cat.warn(...), cat.error(...), cat.fatal(...)
public static void main(String args[]) {
// Try a few logging methods
cat.debug("Start of main()");
cat.info("Just testing a log message with priority set to INFO");
cat.warn("Just testing a log message with priority set to WARN");
cat.error("Just testing a log message with priority set to ERROR");
cat.fatal("Just testing a log message with priority set to FATAL");
// Alternate but INCONVENIENT form
cat.log(Priority.DEBUG, "Calling init()");
new TestLogging().init();
}
public void init() {
java.util.Properties prop = System.getProperties();
java.util.Enumeration enum = prop.propertyNames();
cat.info("***System Environment As Seen By Java***");
cat.debug("***Format: PROPERTY = VALUE***");
while (enum.hasMoreElements()) {
String key = (String) enum.nextElement();
cat.info(key + " = " + System.getProperty(key));
}
}
}
// How to use log4j
public class TestLogging {
// Initialize a logging category. Here, we get THE ROOT CATEGORY
//static Category cat = Category.getRoot();
// Or, get a custom category
static Category cat = Category.getInstance(TestLogging.class.getName());
// From here on, log away! Methods are: cat.debug(your_message_string),
// cat.info(...), cat.warn(...), cat.error(...), cat.fatal(...)
public static void main(String args[]) {
// Try a few logging methods
cat.debug("Start of main()");
cat.info("Just testing a log message with priority set to INFO");
cat.warn("Just testing a log message with priority set to WARN");
cat.error("Just testing a log message with priority set to ERROR");
cat.fatal("Just testing a log message with priority set to FATAL");
// Alternate but INCONVENIENT form
cat.log(Priority.DEBUG, "Calling init()");
new TestLogging().init();
}
public void init() {
java.util.Properties prop = System.getProperties();
java.util.Enumeration enum = prop.propertyNames();
cat.info("***System Environment As Seen By Java***");
cat.debug("***Format: PROPERTY = VALUE***");
while (enum.hasMoreElements()) {
String key = (String) enum.nextElement();
cat.info(key + " = " + System.getProperty(key));
}
}
}
The above code will output some information to Standard output according to the Categories, Rules and Appenders as defined in the configuration file log4j.properties:
log4j.rootCategory=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.ConsoleAppender
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
This defines a root Category which will accept all messages above debug level and send it to the appender dest1. dest1 is defined in the next two lines to be of type ConsoleAppender and it will use the PatternLayout which can be easily configured.






There are currently no comments for this snippet.