Commons-logging + log4j

Source: Internet
Author: User
Tags log log

What can commons-logging do for us?

1, provide a unified log interface, simple operation, while avoiding the project and a log implementation system tightly coupled

2, automatically select the appropriate log implementation system

Find Commons-logging.properties under A.classpath

Find log4j Packages under B.classpath

C. Using the JDK's own log implementation class (note: JDK1.4 only after the log implementation class)

D. Implementing class Simplelog using Commons-logging's own log

3, it does not even need to configure

Code for the Java class of commons-logging :

Package Com.nadim.framework.monitor;import Org.apache.commons.logging.log;import Org.apache.commons.logging.LogFactory; Public classRun {Private StaticFinal log log = Logfactory.getlog (Run.class);  Public Static voidMain (string[] args) {//output 4 Different levels of loggingLog.debug ("111"); Log.info ("222"); Log.warn ("333"); Log.error ("444"); }}

Jar Package Address: http://commons.apache.org/proper/commons-logging/download_logging.cgi

Code for the Java class that uses log4j alone:

Package Com.nadim.framework.monitor;import Org.apache.log4j.logger;import Org.apache.log4j.PropertyConfigurator; Public classRun {StaticLogger Logger = Logger.getlogger (testlog4j.class);//First Step     Public Static voidMain (string[] args) {propertyconfigurator.configure ("log4j.properties"); Logger.debug ("Here is some DEBUG"); Logger.info ("Here is some INFO"); Logger.warn ("Here is some WARN"); Logger.error ("Here is some ERROR"); Logger.fatal ("Here is some FATAL"); }}

Jar Package Address: http://logging.apache.org/log4j/1.2/download.html

commons-logging + log4j Java class Code:

Java code with commons-logging Java class code

Commons-logging.properties Add implementation

Org.apache.commons.logging.log=org.apache.commons.logging.impl.log4jlogger

Second, a little introduction of log4j.properties

Except for comments that begin with # and empty lines, the first line is useful: Log4j.rootlogger = DEBUG, console,a1

1, Log4j.rootlogger is the most important attribute, it defines the log information "output level" and "Output destination".

2, the first comma before the "output level", followed by "Output destination". Multiple "output destinations" can be specified at the same time, separated by commas.

Output levels: DEBUG, INFO, WARN, ERROR, FATAL, which are defined by the log4j system.

The output destination is defined by itself:

# Apply to Console

1 # # # set log Levels # # #2 Log4j.rootlogger = Debug,stdout,d,e3 4 # # # output to console # # #5 log4j.appender.stdout = Org.apache.log4j.ConsoleAppender6 log4j.appender.stdout.Target = System.out7 # # Output logs above the info level8 log4j.appender.stdout.Threshold = INFO9 log4j.appender.stdout.layout = Org.apache.log4j.PatternLayoutTen Log4j.appender.stdout.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m%n One  A # # # Output to log file # # # - LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender - log4j.appender.d.file = D:/logs/debug.log the Log4j.appender.d.append = True - # # Output debug levels above the log - log4j.appender.d.threshold = DEBUG - log4j.appender.d.layout = Org.apache.log4j.PatternLayout + Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n -  + # # # Save exception information to a separate file # # # A LOG4J.APPENDER.E = Org.apache.log4j.DailyRollingFileAppender at # # Exception Log file name - log4j.appender.e.file = D\:/logs/error.log - Log4j.appender.e.append = True - # # Only output logs above the error level!!! - log4j.appender.e.threshold = ERROR - log4j.appender.e.layout = Org.apache.log4j.PatternLayout inLog4j.appender.e.layout.conversionpattern=%d{yyyy-mm-dd hh\:mm\:ss,sss}%5p%c\:(%F\:%L)%n-%m%n
1 # # # Send error through email.2 #log4j的邮件发送appender, if necessary, you can write your own appender.3 Log4j.appender.mail=org.apache.log4j.net.smtpappender4 #发送邮件的门槛, messages are sent only if they are equal to or higher than error (such as fatal)5 Log4j.appender.mail.threshold=error6 #缓存文件大小, send email when log reaches 10k7 log4j.appender.mail.buffersize=108 #发送邮件的邮箱帐号9 [email protected]Ten #SMTP邮件发送服务器地址 One log4j.appender.mail.smtphost=smtp.163.com A #SMTP发送认证的帐号名 - Log4j[email protected] - #SMTP发送认证帐号的密码 the log4j.appender.mail.smtppassword=xxx - #是否打印调试信息, If True is selected, details such as the handshake between the output and SMTP - Log4j.appender.mail.smtpdebug=false - #邮件主题 + Log4j.appender.mail.subject=log4jerrormessage - #发送到什么邮箱, if you want to send to multiple mailboxes, separate them with commas; + #如果需要发副本给某人, add the following line A #[email protected] at [email protected] - Log4j.appender.mail.layout=org.apache.log4j.patternlayout -log4j.appender.mail.layout.conversionpattern=[framework]%d-%c-%-4r[%t]%-5p%c%x-%m%n
Send mail

Send a message Note: Add Mail.jar Activation.jar two jar mail send jar package

MyEclipse problem: Java.lang.noclassdeffounderror:com/sun/mail/util/lineinputstream

Solution: Remove MyEclipse 6.5/myeclipse/eclipse/plugins/com.genuitec.eclipse.j2eedt.core_6.5.0.zmyeclipse650200806/data/ Libraryset/ee_5/javaee.jar inside the Mail.jar and Activation.jar.

1 Log4j.appender.db=org.apache.log4j.jdbc.jdbcappender2 log4j.appender.db.buffersize=13 Log4j.appender.db.driver=com.ibm.db2.jcc.db2driver4 LOG4J.APPENDER.DB.URL=JDBC:DB2://10.190.7.183:50000/FJDC25 Log4j.appender.db.user=db2inst16 Log4j.appender.db.password=db2admin7 Log4j.appender.db.sql=insert into Zyms.test (OrderNo, name) values (0, '%d{yyyy-mm-dd hh\:mm\:ss,sss}%5p%c\:(%F\:% L)%n-%m%n ')8Log4j.appender.db.layout=org.apache.log4j.patternlayout
Output to Database

Output to Database Note: Add a database-related jar package

1. Output mode
Org.apache.log4j.RollingFileAppender (scroll file, automatically record the latest log)
Org.apache.log4j.ConsoleAppender (console)
Org.apache.log4j.FileAppender (file)
Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)
Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)

2. Priority of Journal Records
OFF, FATAL, ERROR, WARN, INFO, DEBUG, all.
LOG4J recommends using only the five levels of fatal, ERROR, WARN, INFO, Debug.

3. Format description The parameters in layout begin with%, and subsequent parameters represent different formatting information (the parameters are listed in alphabetical order):
The full name of the class to which the%c output belongs can be modified to%d{num}, the dimension that the Num class name outputs (such as: "Org.apache.elathen.ClassName",%c{2} will output elathen. ClassName)
%d output log time in the format%d{yyyy-mm-dd hh:mm:ss,sss}, you can specify a format such as%d{hh:mm:ss}
%l output Log event location, including the class name, the thread that occurred, the number of lines in the code
%n line break
%M Output code specifies information, such as info ("message"), output message
%p output priority, i.e. FATAL, ERROR, etc.
%r the number of milliseconds that the output from boot to display the log information
%t output The name of the thread that generated the log event

Commons-logging + log4j

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.