java日志管理 – JAVA編程語言程序開發技術文章

Java代碼    
1. package cn.hs.dpl.utils;  
2.  
3. import java.io.File;  
4. import java.io.FileInputStream;  
5. import java.io.FileOutputStream;  
6. import java.io.IOException;  
7. import java.io.InputStream;  
8. import java.util.Properties;  
9.  
10. import org.apache.log4j.Logger;  
11. import org.apache.log4j.SimpleLayout;  
12. import org.apache.log4j.WriterAppender;  
13. import org.apache.log4j.jdbc.JDBCAppender;  
14.  
15.  
16. /** 
17.  * @author wWX61846 
18.  * 日志工具類 
19.  * 
20.  */ 
21. public class LoggerUtil {  
22.       
23.     private static Logger logger = Logger.getLogger(LoggerUtil.class.getName());  
24.     private static Properties properties =  new  Properties();   
25.       
26. /*  static{ 
27.         SimpleLayout layout = new SimpleLayout (); 
28. 
29.         //向文件中輸出信息,OutputStream示例  
30.         WriterAppender appender1 = null;  
31.         try {  
32.             String temp = Thread.currentThread().getContextClassLoader().getResource("").getPath() ; 
33.             temp = temp.substring(0, temp.lastIndexOf("WEB-INF"));  //WEB-INF 上一級就是項目的根目錄  
34.             String s = temp + "\\log" + "\\appMgr_log.txt";          
35.             appender1 = new WriterAppender(layout, new FileOutputStream(s)); 
36.         } catch(Exception ex) {}  
37.          
38.         logger.addAppender(appender1);       
39.     }*/ 
40.       
41.     static{  
42.         JDBCAppender appender = new JDBCAppender();   
43.         String file = Thread.currentThread().getContextClassLoader().getResource("").getPath()+ "jdbc.properties";            
44.         File f = new File(file);  
45.         try {  
46.             InputStream in = new FileInputStream(f);  
47.             properties.load(in);  
48.         } catch (IOException e) {  
49.             e.printStackTrace();  
50.         }      
51.           
52.         String driverName = properties.getProperty("jdbc.driver");  
53.         String url = properties.getProperty("jdbc.url");  
54.         String username = properties.getProperty("jdbc.username");  
55.         String password = properties.getProperty("jdbc.password");  
56.           
57.         appender.setDriver(driverName);   
58.         appender.setURL(url);   
59.         appender.setUser(username);   
60.         appender.setPassword(password);   
61.         appender.setSql("insert into v5r1_log (operateContent,operateDate) values ('%m','%d{yyyy-MM-dd HH:mm:ss}')");   
62.         logger.addAppender(appender);     
63.           
64. //      log4j.rootLogger=info,db  
65. //  
66. //      log4j.appender.db=org.apache.log4j.jdbc.JDBCAppender  
67. //      log4j.appender.db.driver=com.mysql.jdbc.Driver  
68. //      log4j.appender.db.URL=jdbc:mysql://127.0.0.1/log  
69. //      log4j.appender.db.bufferSize=10  
70. //      log4j.appender.db.user=root  
71. //      log4j.appender.db.password=123  
72. //      log4j.appender.db.sql=insert into log (LogName,UserName,Class,Method,createTime,LogLevel,MSG) values ('%X{userId}','%X{userName}','%C','%M','%d{yyyy-MM-dd HH:mm:ss}','%p','%m')  
73. //      log4j.appender.db.layout=org.apache.log4j.PatternLayout       
74.           
75.           
76.           
77.           
78.     }  
79.       
80.       
81.       
82.     /** 
83.      * @param msg 
84.      * INFO級別的日志 
85.      */ 
86.     public static void tranceINFO(String msg) {  
87.         logger.info( msg);  
88.     }  
89.       
90.     /** 
91.      * @param username 
92.      * @param msg 
93.      * 記錄用戶的操作,  eg:張三  –>  刪除瞭XXX應用 
94.      */ 
95.     public static void tranceINFO(String username, String msg) {  
96.         logger.info(username + " –>  " + msg);  
97.     }  
98.       
99.     /** 
100.      * @param msg 
101.      * WARN級別的日志 
102.      */ 
103.     public static void tranceWARN(String msg) {  
104.         logger.warn(msg);  
105.     }  
106.       
107.     /** 
108.      * @param msg 
109.      * ERROR級別的日志 
110.      */ 
111.     public static void tranceERROR(String msg) {  
112.         logger.error(msg);  
113.     }  
114.       
115.     public static void main(String[] args) {  
116.  
117.     }  
118. } 

 

摘自  CodeToMyLaw
 

發佈留言