关于log4j知识
今天下午接触到log4j知识,花了几个小时,百度了一圈,总算是懂了一些。
log4j的作用:log4j是一个日志输出的插件专门用来进行日志管理的,根据我的理解就是用来执行我们用来检测程序bug的System.out.println()语句的,不过更为简洁,格式也更好判断,利用相关的符号,可以精确到某一行的问题点。
在log4j中,我们刚开始是要利用到log4j的jar包,可以去Apache官网下载,http://www.apache.org/ ,下载后进入到到java中执行build path 加入jar包即可。
log4j有两种使用的形式,一种是利用我们建立的log4j.properties文件进行相关的配置,另外一种是利用log4j2.xml文件进行配置。配置好之后我们就可以写程序了。
1 package com.sxt.Log4j; 2 3 import org.apache.logging.log4j.LogManager; 4 import org.apache.logging.log4j.Logger; 5 6 public class TestLog4j { 7 private static Logger logger = LogManager.getLogger(TestLog4j.class.getName()); 8 9 public void getTestLog4j() { 10 11 logger.info("我是info信息"); 12 logger.error("我是error"); 13 logger.fatal("我是fatal"); 14 } 15 16 public static void main(String[] args) { 17 new TestLog4j().getTestLog4j(); 18 } 19 }
log4j,可以利用properties的配置语句实现大的功能。
1 #OFF,systemOut,logFile,logDailyFile,logRollingFile,logMail,logDB,ALL,socket,im 2 log4j.rootLogger =ALL,systemOut,logFile,logDailyFile,logRollingFile,logMail,logDB,socket,im 3 4 #输出到控制台 5 log4j.appender.systemOut = org.apache.log4j.ConsoleAppender 6 log4j.appender.systemOut.layout = org.apache.log4j.PatternLayout 7 log4j.appender.systemOut.layout.ConversionPattern = [%-5p][%-22d{yyyy-MM-dd HH\:mm\:ss S}][%l]%n%m%n 8 log4j.appender.systemOut.Threshold = DEBUG 9 log4j.appender.systemOut.ImmediateFlush = TRUE 10 log4j.appender.systemOut.Target = System.out 11 12 #输出到文件 13 log4j.appender.logFile = org.apache.log4j.FileAppender 14 log4j.appender.logFile.layout = org.apache.log4j.PatternLayout 15 log4j.appender.logFile.layout.ConversionPattern = [%-5p][%-22d{yyyy-MM-dd HH\:mm\:ss S}][%l]%n%m%n 16 log4j.appender.logFile.Threshold = DEBUG 17 log4j.appender.logFile.ImmediateFlush = TRUE 18 log4j.appender.logFile.Append = TRUE 19 log4j.appender.logFile.File = D://opt/ftp/vtm-om/logs/FileAppender.log 20 log4j.appender.logFile.Encoding = UTF-8 21 22 #定期回滚日志文件(logDailyFile) 23 log4j.appender.logDailyFile = org.apache.log4j.DailyRollingFileAppender 24 log4j.appender.logDailyFile.layout = org.apache.log4j.PatternLayout 25 log4j.appender.logDailyFile.layout.ConversionPattern = [%-5p][%-22d{yyyy-MM-dd HH\:mm\:ss S}][%l]%n%m%n 26 log4j.appender.logDailyFile.Threshold = DEBUG 27 log4j.appender.logDailyFile.ImmediateFlush = TRUE 28 log4j.appender.logDailyFile.Append = TRUE 29 log4j.appender.logDailyFile.File = D://opt/ftp/vtm-om/logs/DailyRollingFileAppender 30 log4j.appender.logDailyFile.DatePattern = '.'yyyyMMddHHmmss'.log' 31 log4j.appender.logDailyFile.Encoding = UTF-8 32 33 #回滚文件(logRollingFile) 34 log4j.appender.logRollingFile = org.apache.log4j.RollingFileAppender 35 log4j.appender.logRollingFile.layout = org.apache.log4j.PatternLayout 36 log4j.appender.logRollingFile.layout.ConversionPattern = [%-5p][%-22d{yyyy-MM-dd HH\:mm\:ss S}][%l]%n%m%n 37 log4j.appender.logRollingFile.Threshold = DEBUG 38 log4j.appender.logRollingFile.ImmediateFlush = TRUE 39 log4j.appender.logRollingFile.Append = TRUE 40 log4j.appender.logRollingFile.File = D://opt/ftp/vtm-om/logs/RollingFileAppender.log 41 log4j.appender.logRollingFile.MaxFileSize = 1MB 42 log4j.appender.logRollingFile.MaxBackupIndex = 10 43 log4j.appender.logRollingFile.Encoding = UTF-8 44 45 #发送日志到指定邮件 46 log4j.appender.logMail = org.apache.log4j.net.SMTPAppender 47 log4j.appender.logMail.layout = org.apache.log4j.HTMLLayout 48 log4j.appender.logMail.layout.LocationInfo = TRUE 49 log4j.appender.logMail.layout.Title = Struts2 Mail LogFile 50 log4j.appender.logMail.Threshold = DEBUG 51 log4j.appender.logMail.SMTPDebug = FALSE 52 log4j.appender.logMail.SMTPHost = smtp.163.com 53 log4j.appender.logMail.From = ****@163.com 54 log4j.appender.logMail.To = ****@qq.com 55 #log4j.appender.logMail.Cc = ****@qq.com 56 log4j.appender.logMail.Bcc = ****@qq.com 57 log4j.appender.logMail.SMTPUsername = ****@163.com 58 log4j.appender.logMail.SMTPPassword = **** 59 log4j.appender.logMail.Subject = Log4j Log Messages 60 log4j.appender.logMail.BufferSize = 1024 61 log4j.appender.logMail.SMTPAuth = TRUE 62 63 #将日志登录到MySQL数据库 64 log4j.appender.logDB = org.apache.log4j.jdbc.JDBCAppender 65 log4j.appender.logDB.layout = org.apache.log4j.PatternLayout 66 log4j.appender.logDB.Driver = com.mysql.jdbc.Driver 67 log4j.appender.logDB.URL = jdbc:mysql://192.168.1.71:3306/test 68 log4j.appender.logDB.User = root 69 log4j.appender.logDB.Password = 123456 70 log4j.appender.logDB.Sql = INSERT log_info(project_name,create_date,level,category,file_name,thread_name,line,all_category,message)values('souvc','%d{yyyy-MM-dd HH:mm:ss}','%p','%c','%F','%t','%L','%l','%m') 71 72 # 应用于socket 73 log4j.appender.socket=org.apache.log4j.RollingFileAppender 74 log4j.appender.socket.RemoteHost=localhost 75 log4j.appender.socket.Port=5001 76 log4j.appender.socket.LocationInfo=true 77 # Set up for Log Factor 5 78 log4j.appender.socket.layout=org.apache.log4j.PatternLayout 79 log4j.appender.socket.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n 80 # Log Factor 5 Appender 81 log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender 82 log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000 83 84 85 # 自定义Appender 86 log4j.appender.im = com.souvc.logger.appender.IMAppender 87 log4j.appender.im.host = localhost 88 log4j.appender.im.username = username 89 log4j.appender.im.password = password 90 log4j.appender.im.recipient = souvc@souvc.com 91 log4j.appender.im.layout=org.apache.log4j.PatternLayout 92 log4j.appender.im.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n