How to Use log4j and commons. Logging

Source: Internet
Author: User
When you are developing a program, debugging (debugging) and logging are very important tasks. However, there are too many logging APIs available because they are both good, it is difficult to make a choice. the foreign Java Forum also has some discussions on these logging methods.

While common logging is a small bridge between these different logging APIs. currently, the most famous LOG method in Java is log4j and JDK 1.4 logging API. in addition, there are logkit and so on used in aveon. commons-logging also implements some basic logging methods: nooplog and simplelog. their comparison is not in the scope of this discussion,

If you are interested, please refer to the reference file.

The quick use of logging is actually very simple to use, put the commons-logging.jar 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 = 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 ");
}

}

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

<% @ Page import = "com. softleader. Newspaper. java. opensource. loggingtest" %>
<% Loggingtest test = new loggingtest (); test. Hello (); %>

You will see the following output in the Tomcat Console

Log4j: warn no appenders cocould be found for logger (COM. softleader. Newspaper. java. opensource. loggingtest ).
Log4j: Warn please initialize the log4j system properly. okok

It is because you have not configured the commons-logging.properties, will introduce you soon ~~~.

Set the commons-logging.properties you can set your log factory to use which one I write in/WEB-INF/classes/commons-logging.properties for example with log4j
Org. Apache. commons. Logging. log = org. Apache. commons. Logging. impl. log4jcategorylog
If your server is a jdk1.4 or later version
You can use org. Apache. commons. Logging. impl. jdk14logger
Then, based on your logger, write the properties that match him. Take log4j as an example.
/WEB-INF/classes/place one

// 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

// Log output to the console
Log4j. rootlogger = info, A1
Log4j. appender. A1 = org. Apache. log4j. leleappender
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

// Send an email
# Configuration for processing ing e-mails when error messages occur.

# Custom appender
Log4j. appender. Mail = org.apache.log4j.net. smtpappender
# Log error levels
Log4j. appender. Mail. Threshold = Error
# The size of the cached file. An email is sent when the log reaches kb.
Log4j. appender. Mail. buffersize = 512
# Sender
Log4j. appender. Mail. From = webmaster@bcxy.com
# Email sending server
Log4j. appender. Mail. smtphost = smtp.163.com
# Mail title
Log4j. appender. Mail. Subject = log4j message
# Receiver of log mail
Log4j. appender. Mail. To = ezerg@126.com
# Log patternlayout
Log4j. appender. Mail. layout = org. Apache. log4j. patternlayout
# Log format
Log4j. appender. Mail. layout. conversionpattern = [framework] % d-% C-%-4r [% T] %-5 p % C % x-% m % N

At this point you will execute the test-commons-logging.jsp output content, it will be recorded in your C:/log directory test. PS in log: if no related class is used, the simplog is used.
Simplelog. properties draws conclusions based on my own experience. log4j can satisfy all engineers, so I also directly use log4j without using commons-logging.

However, in order to increase the versatility of the product and avoid the trouble during porting, I will change it to the commons-logging API to call for the new product and project.

If you are not familiar with the working principle of commons-logging, see <commons-logging usage>

Ezerg Programming Language

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.