1 According to reason the Commons-logging component has a method of printing logs, or a logging with Java, such as the following:
Importing logging jar packages is a must, and here are two ways to print the log
Package com.commons.logging;
Import Java.util.logging.Logger;
Import Org.apache.commons.logging.Log;
Import org.apache.commons.logging.LogFactory;
The public class Test {//Two print method results are similar to
private static log log = Logfactory.getlog (Test.class); This is the log
static Logger Logger = Logger.getlogger (Test.class.getName ()) in the Commons-logging component, or the Java self-contained log
public void log ()
{
log.debug ("Debug Info.");
Log.info ("info info");
Log.warn ("Warn info");
Log.error ("error info");
Log.fatal ("Fatal info");
System.out.println ();
Logger.info ("Hello");
}
public static void Main (string[] args) {
Test t = new Test ();
T.log ();
}
2 But the effect is not good, the General people do not use, the following print log
July 24, 2013 10:24:42 pm com.commons.logging.Test Log
info: Info info
July 24, 2013 10:24:42 pm com.commons.logging.Tes T log
Warning: Warn info
July 24, 2013 10:24:42 pm com.commons.logging.Test Log
serious: Error info
July 24, 2013 10:24: 42 pm Com.commons.logging.Test Log
serious: Fatal info
July 24, 2013 10:24:42 pm com.commons.logging.Test log
information: Hel Lo
3 these days are popular log4j, below look at the use of the log4j process:
1) Import jar Package log4j
2) Add log4j.properties under SRC
Log4j.rootlogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
Log4j.appender.stdout.layout.ConversionPattern =%5p [%t] (%f:%l)-%m%n
log4j.appender.r=org.apache.log4j.rollingfileappender
log4j.appender.r.file= GridQualitSupervision.log
log4j.appender.r.maxfilesize= 300KB
log4j.appender.r.maxbackupindex=1
Log4j.appender.r.layout=org.apache.log4j.patternlayout
log4j.appender.r.layout.conversionpattern=%p%t%c-%m %n
3) Simple test class
Package Com.wanju.common;
Import Org.apache.log4j.Logger;
public class Loggerutil {public
static Logger Logger = Logger.getlogger (LoggerUtil.class.getName ());
public static void Main (string[] args) {
logger.info ("OK");
}
4) View Test results
INFO [main] (loggerutil.java:11)-OK
It's so simple, it's so cool