Two-step implementation of LOG4J records Java console output system all exception information and custom log log----> very practical, strongly recommend reading

Source: Internet
Author: User
Tags log log log4j

1, about logging is the development of essential knowledge, now give you a detailed talk about how to record log information; the principle is not much said to see the actual operation

2, configure the log4j file: The specific configuration of what means to do not say a look at the understand Hey

Log4j.rootcategory=debug,stdlog,errorlog


Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.threshold=error
Log4j.appender.stdlog.file=d:/error.log
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
Log4j.appender.stdout.layout.conversionpattern=%d{yyyy/mm/dd HH:mm:ss} [%c{1}]-%m%n


Log4j.appender.stdlog=org.apache.log4j.rollingfileappender
Log4j.appender.stdlog.threshold=debug,console
Log4j.appender.stdlog.file=d:/debug.log
log4j.appender.stdlog.maxfilesize=1024kb
Log4j.appender.stdlog.maxbackupindex=3
Log4j.appender.stdlog.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.STDLOG.LAYOUT.CONVERSIONPATTERN=%D{YYYY/MM/DD HH:mm:ss}%-5p [%c{1}]-%m%n


Log4j.appender.errorlog=org.apache.log4j.rollingfileappender
Log4j.appender.errorlog.threshold=warn
Log4j.appender.errorlog.file=d:/warn.log
log4j.appender.errorlog.maxfilesize=1024kb
Log4j.appender.errorlog.maxbackupindex=3
Log4j.appender.errorlog.layout=org.apache.log4j.patternlayout
log4j.appender.errorlog.layout.conversionpattern=%d [%c{1}]-%m%n


3, Specific code:

Log4jlogger L = new Log4jlogger ("Log4j.properties"); Initialize Log driver
L.logdebug ("--->dug record");
L.logerror ("Log is recording ...");
L.loginfo ("message record ...");
try {
Log4jlogger LL = null;
Ll.logdebug ("---");
catch (Exception e) {
L.logdebug (E, E.fillinstacktrace ());
L.logerror (E,e.fillinstacktrace ());
}

4, saved log information:

2013/01/24 16:51:03 ERROR [Properties]-Log is recording ...
2013/01/24 16:51:03 INFO [Properties]-Message Records ...
2013/01/24 16:51:03 DEBUG [Properties]-java.lang.NullPointerException
Java.lang.NullPointerException
At DDM. Datadrivermanager.showsysinfo (datadrivermanager.java:47)
At DDM. Datadrivermanager.main (DATADRIVERMANAGER.JAVA:20)
2013/01/24 16:51:03 ERROR [Properties]-java.lang.NullPointerException
Java.lang.NullPointerException
At DDM. Datadrivermanager.showsysinfo (datadrivermanager.java:48)
At DDM. Datadrivermanager.main (DATADRIVERMANAGER.JAVA:20)

5, the Gaocheng. Think you can of the brothers hope to give a good opinion.

6,QQ Group: 180258862

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.