reference:http://www.vipan.com/htdocs/log4jhelp.html
package
log4j;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
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());
public void init() ...{
Properties prop = System.getProperties();
Enumeration enum1 = prop.propertyNames();
cat.info("***System Environment As Seen By Java***");
cat.debug("***Format: PROPERTY = VALUE***");
while (enum1.hasMoreElements()) ...{
String key = (String) enum1.nextElement();
cat.info(key + " = " + System.getProperty(key));
}
}
public static void main(String args[]) ...{
// Try a few logging methods
// Use debug to write debugging messages which should not be printed
// when the application is in production
cat.debug("Start of main()");
// Use info for messages similar to the "verbose" mode of many
// applications.
cat.info("Just testing a log message with priority set to INFO");
// Use warn for warning messages which are logged to some log but the
// application is able to carry on without a problem.
cat.warn("Just testing a log message with priority set to WARN");
// Use error for application error messages which are also logged to
// some log but, still, the application can hobble along. Such as when
// some administrator-supplied configuration parameter is incorrect and
// you fall back to using some hard-coded default value.
cat.error("Just testing a log message with priority set to ERROR");
// Use fatal for critical messages, after logging of which the
// application quits abnormally.
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();
}
}
import java.util.Enumeration;
import java.util.Properties;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
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());
public void init() ...{
Properties prop = System.getProperties();
Enumeration enum1 = prop.propertyNames();
cat.info("***System Environment As Seen By Java***");
cat.debug("***Format: PROPERTY = VALUE***");
while (enum1.hasMoreElements()) ...{
String key = (String) enum1.nextElement();
cat.info(key + " = " + System.getProperty(key));
}
}
public static void main(String args[]) ...{
// Try a few logging methods
// Use debug to write debugging messages which should not be printed
// when the application is in production
cat.debug("Start of main()");
// Use info for messages similar to the "verbose" mode of many
// applications.
cat.info("Just testing a log message with priority set to INFO");
// Use warn for warning messages which are logged to some log but the
// application is able to carry on without a problem.
cat.warn("Just testing a log message with priority set to WARN");
// Use error for application error messages which are also logged to
// some log but, still, the application can hobble along. Such as when
// some administrator-supplied configuration parameter is incorrect and
// you fall back to using some hard-coded default value.
cat.error("Just testing a log message with priority set to ERROR");
// Use fatal for critical messages, after logging of which the
// application quits abnormally.
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();
}
}
log4j.properties is below:
log4j.rootCategory=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.ConsoleAppender
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
Note:
log4j.properties must be under the src dir
src
-log4j
-TestLogging.java
-log4j.properties