When you are developing programs, debugging and logging are very important work, but now there are too many Logging APIs, because they are all good, it is difficult to make a choice. Foreign Java Forum For These logging methods have also discussions.
Common logging is a small bridge in these different Logging API. At present, the most famous log mode in Java, the first is LOG4J, and the other JDK 1.4 logging API. In addition, there is Avalon The logkit used in the medium, and Commons-Logging also has some basic logging methods as NOOPLOG and SIMPLOG. For their comparison is not this discussion,
Interested people please refer to the reference file.
Quick use Logging actually logging very simple to use, put the commons-logging.jar under / web-inf / lib. Next, write the following code
Loggingtest.java
Package com.softleader.newspaper.java.opensource;
Import org.apache.commons.logging.log;
Import org.apache.commons.logging.logfactory;
Public class loggingtest {
Log log = logfactory.getlog (loggingtest.class);
Public void hello () {
Log.Error ("error");
Log.debug ("debug");
Log.warn ("WARN");
Log.info ("info");
Log.Trace ("trace");
System.out.Println ("Okok");
}
}
Test Test-Commons-Logging.jsp on / Place a JSP Test Test-Commons-Logging.jsp
<% @ page import = "com.softleader.newspaper.java.opensource.loggingtest"%>
<% Loggingtest test = new loggingte (); test.hello ();%>
You will see Tomcat Console's output below
Log4j: warn no appenders could be bound for logger (com.softleader.newspaper.java.opensource.loggingte).
Log4J: Warn please Initialize the log4j system prot otly.okok
Because you haven't configured commons-logging.properties, you will introduce you ~~~.
Set commons-logging.properties You can set your log factory? Which one I want to use is written in /Web-inf/classes/commons-logging.properties with log4j
Org.apache.commons.logging.log = org.apache.commons.logging.Impl.log4jcategoryLog
If you Server is a version using JDK1.4
You can use org.apache.commons.logging.Impl.jdk14logger
Then follow your logger to comply with his Properties to take the log4j as an example, you will be
/ WEB-INF / CLASSES / Lower Place one
// Log Output to File
Log4j.rootlogger = debug, a_defaultlog4j.Appender.a_default = org.apache.log4j.rollingFileAppender
Log4j.Appender.a_default.file = c: //log/test.log
Log4j.Appender.a_default.maxfilesize = 4000kb
Log4j.Appender.a_default.maxbackupindex = 10
Log4j.Appender.a_default.Layout = org.apache.log4j.patternlayout
Log4j.Appender.a_default.Layout.conversionPattern =% D {ISO8601} -% P -% M% N
// Log Outputs to the console
Log4j.rootlogger = info, A1
Log4j.Appender.a1 = org.apache.log4j.consoleAppender
Log4j.Appender.a1.Layout = Org.apache.log4j.patternlayout
Log4j.Appender.a1.Layout.conversionPattern =% - D {yyyy-mm-dd hh: mm: s, sss} [% C] - [% P]% M% N
// send the message # Configuration for receiving e-mails when ERROR messages occur. # Custom Appenderlog4j.appender.MAIL = org.apache.log4j.net.SMTPAppender # log error level log4j.appender.MAIL.Threshold = ERROR # Cache file size, send email4j.appender.mail.buff4j.appender.mail.buff4j.Appender.mail.buff4j.appender.mail.buff4j.appender.mail.from=webmaster@bcxy.com # Send mail server log4j.Appender.mail.smtphost = SMTP. 163.com # 邮 标 Log4j.Appender.mail.subject = log4j message # log mail recipient log4j.Appender.mail.to=Ezerg@126.com# log patternLayoutLog4j.Appender.mail.Layout = Org.apache. Log4j.patternLayout # log format log4j.appender.mail.Layout.conversionPattern = [framework]% D -% C -% - 4R [% T]% -5p% C% X -% M% N this time you go to execute The contents of Test-Commons-Logging.jsp Output will be recorded in Test.log in your C: / log directory PS: If there is no associated class, simpLog is used, it is setting Simplelog.properties. Conclusion With my own experience, log4j can meet all engineers, so I also use log4j directly without using commons-logging. However, in order to increase product versatility, avoid the trouble, new products and projects, I will Changed him to the Commons-logging api. If you don't know what's the working principle of Commons-logging, please refer to