log4j log File configuration

Source: Internet
Author: User
Tags log4j

log4j log File configuration

------------------------------------------------------------------------

first, the log device configuration

# root Logger (other logs are inherited from this logger)

# Default Level=debug
Log4j.rootlogger=info, A1, A2 (a1,a2 for Appender)

# Level of the log

Log4j.logger.a=info,a1

Log4j.logger.a.b=debug,a1,a2

The log's inheritance relationship is: A.b inherits from the A,a inherits from the root logger, namely sends to the A.B the info log, also will be A record, also can by the root logger record

Note: A.B is not necessarily a package name plus class name, you can name any

Code:

Logger Logger = Logger.getlogger ("a.b");

logger.debug ("Test Log");

Then the A.B log inherits the Appender of a and root logger, so "test log" outputs three times, where the level info of the A log does not affect and still outputs "test log"


log4j Configuration for HttpClient
# commons.httpclient
Log4j.logger.httpclient.wire.header=info
Log4j.logger.httpclient.wire=info
Log4j.logger.org.apache.commons.httpclient=info

Second, appender configuration
# Console Appender=a1,file appender=a2,db appender=a3,mail appender=a4

# A1
Log4j.appender.a1=org.apache.log4j.consoleappender
Log4j.appender.a1.threshold=debug
Log4j.appender.a1.layout=org.apache.log4j.patternlayout
log4j.appender.a1.layout.conversionpattern=%-5p logger:%c{1} date:%d{iso8601}-%m%n

# A2
Log4j.appender.a2=org.apache.log4j.rollingfileappender
Log4j.appender.a2.threshold=debug

# ${loghome} and ${logname} read from the system environment variable [System.setproperty (prop,value)]
Log4j.appender.a2.file=${loghome}/${logname}
Log4j.appender.a2.append=true
Log4j.appender.a2.layout=org.apache.log4j.patternlayout
log4j.appender.a2.layout.conversionpattern=%-5p logger:%c{1} date:%d{iso8601}-%m%n
log4j.appender.a2.maxbackupindex=10
Log4j.appender.a2.maxfilesize=2mb

# A3
Log4j.appender.a3=org.apache.log4j.jdbc.jdbcappender
Log4j.appender.a3.threshold=warn
Log4j.appender.a3.url=jdbc:mysql://localhost:3306/test
Log4j.appender.a3.driver=com.mysql.jdbc.driver
Log4j.appender.a3.user=root
log4j.appender.a3.password=
Log4j.appender.a3.sql=insert into log4j (message) VALUES ('%-5p logger:%c{1} date:%d{iso8601}-%m%n ')
Log4j.appender.a3.layout=org.apache.log4j.patternlayout
log4j.appender.a3.layout.conversionpattern=%-5p logger:%c{1} date:%d{iso8601}-%m%n

# A4
Log4j.appender.a4=org.apache.log4j.net.smtpappender
Log4j.appender.a4.threshold=error
log4j.appender.a4.buffersize=10
Log4j.appender.a4.smtpusername=qydd_program
log4j.appender.a4.smtppassword=
log4j.appender.a4.smtphost=smtp.mail.yahoo.com.cn
Log4j.appender.a4.smtpdebug=true
log4j.appender.a4.from=qydd_program@yahoo.com.cn
Log4j.appender.a4.subject=baseframe Error Message
log4j.appender.a4.to=qydd_program@yahoo.com.cn
Log4j.appender.a4.layout=org.apache.log4j.patternlayout
log4j.appender.a4.layout.conversionpattern=%-5p logger:%c{1} date:%d{iso8601}-%m%n

Third, code example

Log4j.properties---------------------------------------# Logger Log4j.rootlogger=warn, A1 Log4j.logger.a=warn, A1 Log4j.logger.a.b=info, A1 # A1 Appender log4j.appender.a1=org.apache.log4j.consoleappender Log4j.appender.a1.threshold=debug Log4j.appender.a1.layout=org.apache.log4j.patternlayout log4j.appender.a1.layout.conversionpattern=%-5p logger:%c{1} date:%d{iso8601}-%m%n TestLog4J.java---------------- ------------------------Package Test; Import java.io.IOException; Import Java.io.InputStream; Import java.util.Properties; Import Org.apache.log4j.Logger; public class Testlog4j {public static void main (string[] args) throws IOException {InputStream in = TestLog4J.class.getR Esourceasstream ("Log4j.properties"); Properties Props = new properties (); Props.load (in); Org.apache.log4j.PropertyConfigurator.configure (props); Logger Rootlogger = Logger.getrootlogger (); Rootlogger.debug ("Test1"); Logger Alogger = Logger.getlogger ("A"); Alogger.warn ("Test2"); Logger Ablogger = Logger.getlOgger ("a.b"); Ablogger.info ("Test3"); Ablogger.debug ("Test4"); } }

Output:

WARN logger:a date:2009-10-02 01:13:52,734-test2
WARN logger:a date:2009-10-02 01:13:52,734-test2
INFO logger:b date:2009-10-02 01:13:52,734-test3
INFO logger:b date:2009-10-02 01:13:52,734-test3
INFO logger:b date:2009-10-02 01:13:52,734-test3

Analysis:

(1) Because the root logger is warn, its output is debug, so "Test1" does not print

(2) A logger inherits from Root logger, its level is warn, output is warn, so output two "Test2"

(3) B Logger inherits from a Logger,a Logger inherits from the root Logger, its level is info, so output three "Test3", and "Test4" does not output

(4) B logger prints the info ("Test3") satisfies the a.b level, but does not meet the level warn of a and root logger, but log4j appends the logger of a and root appender only to a.b, so levels to B The logger print does not affect

(5) Adding a log4j.additivity.a.b=false line in the log4j.properties so that the output of the A.B logger is output only in itself, not in a and root logger output

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.