1.引入的包
2.配置文件
该文件放到src路径下,
1 log4j.rootLogger=DEBUG,CONSOLE,A1,im 2 #DEBUG,CONSOLE,FILE,ROLLING_FILE,MAIL,DATABASE 3 log4j.addivity.org.apache=true 4 ################### 5 # Console Appender 6 ################### 7 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 8 log4j.appender.Threshold=DEBUG 9 log4j.appender.CONSOLE.Target=System.out 10 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 11 log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 12 #log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n 13 ##################### 14 # File Appender 15 ##################### 16 log4j.appender.FILE=org.apache.log4j.FileAppender 17 log4j.appender.FILE.File=file.log 18 log4j.appender.FILE.Append=false 19 log4j.appender.FILE.layout=org.apache.log4j.PatternLayout 20 log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 21 # Use this layout for LogFactor 5 analysis 22 ######################## 23 # Rolling File 24 ######################## 25 log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender 26 log4j.appender.ROLLING_FILE.Threshold=ERROR 27 log4j.appender.ROLLING_FILE.File=rolling.log 28 log4j.appender.ROLLING_FILE.Append=true 29 log4j.appender.ROLLING_FILE.MaxFileSize=10KB 30 log4j.appender.ROLLING_FILE.MaxBackupIndex=1 31 log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout 32 log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 33 #################### 34 # Socket Appender 35 #################### 36 log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender 37 log4j.appender.SOCKET.RemoteHost=localhost 38 log4j.appender.SOCKET.Port=5001 39 log4j.appender.SOCKET.LocationInfo=true 40 # Set up for Log Facter 5 41 log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout 42 log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n 43 ######################## 44 # Log Factor 5 Appender 45 ######################## 46 log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender 47 log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000 48 ######################## 49 # SMTP Appender 50 ####################### 51 log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender 52 log4j.appender.MAIL.Threshold=FATAL 53 log4j.appender.MAIL.BufferSize=10 54 log4j.appender.MAIL.From=chenyl@yeqiangwei.com 55 log4j.appender.MAIL.SMTPHost=mail.hollycrm.com 56 log4j.appender.MAIL.Subject=Log4J Message 57 log4j.appender.MAIL.To=chenyl@yeqiangwei.com 58 log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout 59 log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 60 ######################## 61 # JDBC Appender 62 ####################### 63 log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender 64 log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test 65 log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver 66 log4j.appender.DATABASE.user=root 67 log4j.appender.DATABASE.password= 68 log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n') 69 log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout 70 log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 71 log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender 72 log4j.appender.A1.File=SampleMessages.log4j 73 log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j' 74 log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout 75 ################### 76 #自定义Appender 77 ################### 78 log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender 79 log4j.appender.im.host = mail.cybercorlin.net 80 log4j.appender.im.username = username 81 log4j.appender.im.password = password 82 log4j.appender.im.recipient = corlin@yeqiangwei.com 83 log4j.appender.im.layout=org.apache.log4j.PatternLayout 84 log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
3.打印参数
1 %m 输出代码中指定的消息 2 %p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL 3 %r 输出自应用启动到输出该log信息耗费的毫秒数 4 %c 输出所属的类目,通常就是所在类的全名 5 %t 输出产生该日志事件的线程名 6 %n 输出一个回车换行符,Windows平台为“\r\n”,Unix平台为“\n” 7 %d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss , SSS},输出类似:2002年10月18日 22 : 10 : 28 , 921 8 %l 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。举例:Testlog4.main(TestLog4.java: 10 )