log4j java版如何將日誌寫入資料庫

2021-05-23 01:00:29 字數 1619 閱讀 4403

其實如果是直接通過jdbc去連線資料庫,那麼下面的鏈結的

但是在現實情況,特別是大型應用,基本都是通過datasource來獲取

connection,而這個zip中已經明確說了不支援 datasource,那麼我們怎麼辦呢?

public class log4jtest

這個時候你發現 我們找了半天connection的地方原來在這裡

現在我們構建乙個handler,我這裡用的框架只是hibernate3

我需要從hibernate3中獲取connecion就可以了

public class cas2hibernateconnectionhandler implements jdbcconnectionhandler catch (exception e)

return con;

}public connection getconnection(string arg0, string arg1, string arg2)

throws exception

這就是我的handler,為了讓**更清楚,我把我的hibernateconnection也貼出來,這是常見的sington模式獲取hibernate的sessionfactory

public class hibernateconnection

//單元測試用的

synchronized public static sessionfactory getinstance() catch (hibernateexception e)

}return sessionfactoryinstance;}}

說到這裡我順便說一下,我這邊框架用的是hibernate3,還有很多資料庫相關的框架比如hibernate+spring或者c3p0等等,這些都無所謂

只要找到相應如果獲得jdbc connection的方法,同時構建自己的connectionhandler並實現裡面的getconnection()就可以了。

新構建的sqlhander需要實現介面

public inte***ce jdbcsqlhandler

他裡面只有乙個方法getstatement,裡面寫入具體insert 語句就可以了

public class casloginhandler implements jdbcsqlhandler catch (throwable th)

locationinfo locinfo = event.getlocationinformation();

throwableinformation throwableinfo = event.getthrowableinformation();

stringbuffer throwablestringbuffer = new stringbuffer();

string locinfostring = "'', '', '', ''";

return sb.tostring();

}這樣整個將log日誌寫入資料庫就完成了。當然下面還有問題可以供思考,一般log.debug("msg");這種寫法所有的日誌資訊都是以一條msg輸出

,比如"張三在127.0.0.1機器查詢表×××" ;如何簡單得將一句話insert到不同字段,這個有很多方式,可以思考一下哪種更方便,更簡潔。

log4j java日誌記錄

log4j.rootlogger debug,a1,a2,am 輸出到控制台 n 輸出到檔案當中 n 輸出到oracle當中 11.115 31 1521 orcl在oracle庫中建立表log 4j,兩個欄位都是varchar2型別 c是當前類名 m值是 於我們的log.debug this is...

如何將syslogng的日誌寫入MySQL資料庫

在 使用syslog ng搭建日誌伺服器 中已經講了如何將syslog ng配置為中心日誌伺服器,下面介紹如何將syslogng的日誌寫入mysql資料庫,系統 gentoo 2007 1 修改 etc syslog ng syslog ng.conf,新增 source s remote dest...

解析php如何將日誌寫進syslog

在做專案的時候們為了方便運維,我們經常需要將系統日誌寫入系統syslog,下邊我們就介紹一下,在linux下php對syslog的操作 在linux中配置syslog 在linux中,facility 裝置 有以下幾種 auth 普通安全 授權訊息 authpriv 私有安全 授權訊息 cron 定...