The Java Logging API uses

Source: Internet
Author: User
Tags log log log log4j
debugging (Debugging) and logging (logging) are very important when you are developing a program, but there are too many logging APIs available now because they're all good and it's hard to make a choice. The Foreign Java Forum also has some discussion about these logging ways.

And common logging is a small bridge in these different logging APIs. The most famous Log in Java is the log4j and the JDK 1.4 logging API. In addition, there are Avalon used in Logkit and so on. And Commons-logging also has to achieve some basic logging way for Nooplog and Simplelog. For their comparison is not in the scope of this discussion,

Those who are interested please refer to the reference document.

Fast use Logging actually Logging very simple to use, will Commons-logging.jar put under/web-inf/lib. Then 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");
}

}

In/Place a JSP test test-commons-logging.jsp

<%@ page import= "Com.softleader.newspaper.java.opensource.LoggingTest"%>
<% loggingtest test = new Loggingtest (); Test.hello ();%>

You'll see that the Tomcat console will have the following output

Log4j:warn No Appenders could is found for logger (com.softleader.newspaper.java.opensource.LoggingTest).
Log4j:warn Please initialize the log4j system properly. Okok

is because you have not configured Commons-logging.properties, will soon introduce you ~ ~ ~ ~.

Set Commons-logging.properties you can set your log factory to use which one I write in/web-inf/classes/commons-logging.properties as an example of log4j
Org.apache.commons.logging.log=org.apache.commons.logging.impl.log4jcategorylog
If you server is using more than jdk1.4 version
You can use the Org.apache.commons.logging.impl.Jdk14Logger
And then, according to your Logger, you write the properties that match his log4j.
/web-inf/classes/Place a log4j.properties

Log output to File
Log4j.rootlogger=debug, A_default
Log4j.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

# more complete output
# log4j.appender.a_default.layout.conversionpattern=%d%-5p [%t]%-17c{2} (%13f:%l)%3x-%m%n
Log output to 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:ss,sss} [%c]-[%p]%m%n


At this point you go to perform the content of test-commons-logging.jsp output, it will be recorded in your C:\log directory Test.log PS: If no relevant class will be used to simplog, this time to set the
Simplelog.properties conclusion in my own use of experience, log4j can meet all the engineers, so I am also directly using log4j without using commons-logging.

However, in order to increase the versatility of the product, avoid the trouble of porting, new products and projects, I will change him to commons-logging API to call.

If you are not familiar with the working principle of commons-logging, please refer to the method of <commons-logging




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.