(OAF)jdeveloper整合log4j並將日誌輸出到指定檔案並寫入資料庫

weixin_34126215發表於2016-10-27

參考:

How to configure Log4j in JDev 11g

Ever wanted to use log4j in your adf project ? Well though Oracle doesn’t recommends the use of log4j, i say its purely your choice to use it or not.

So how do we configure log4j in adf  ? Pretty simple..just follow these steps

1) Add log4j jars in the project library

2) Create a file log4j.properties in your Application Sources Directory (this folder is seen in jdev – in both ViewController and Model Folder).

For Model project this file has to be present at <path of model project>Model\src.

For ViewController project this file has to be present as <path of model project>ViewController\src

3) Paste the following contents

****************************

# Set root logger level to INFO and its only appender to ConsoleOut.
log4j.rootLogger=INFO,ConsoleOut,F
# ConsoleOut is set to be a ConsoleAppender.
log4j.appender.ConsoleOut=org.apache.log4j.ConsoleAppender
# ConsoleOut uses PatternLayout.
log4j.appender.ConsoleOut.layout=org.apache.log4j.PatternLayout
log4j.appender.ConsoleOut.layout.ConversionPattern=%-5p: [%d] %c{1} – %m%n
log4j.logger.org.apache.jsp=DEBUG
log4j.appender.F=org.apache.log4j.FileAppender
log4j.appender.F.File=C:/Logs/Logger.log
log4j.appender.F.layout=org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern=%d{MM-dd@HH:mm:ss,SSS} %-5p (%13F:%L) %3x – %m%n

#Addon for
com.sun.faces.level=FINE

****************************


 

整合開發測試

1.將log4j jars新增至專案中。

2.新建log4j.properties並將其放入myclasses目錄(oacore中將該檔案放入$JAVA_TOP目錄中),並在其中新增以下內容

3.log4j.properties中的內容(此檔案只是一個簡略的日誌使用,更詳細的日誌生成設定,請參考官方文件及他人學習筆記)

# Set root logger level to INFO and its only appender to ConsoleOut.
log4j.rootLogger=INFO,ConsoleOut,F,DATABASE
# ConsoleOut is set to be a ConsoleAppender.
log4j.appender.ConsoleOut=org.apache.log4j.ConsoleAppender
# ConsoleOut uses PatternLayout.
log4j.appender.ConsoleOut.layout=org.apache.log4j.PatternLayout
log4j.appender.ConsoleOut.layout.ConversionPattern=%-5p: [%d] %c{1} – %m%n
log4j.logger.org.apache.jsp=DEBUG
log4j.appender.F=org.apache.log4j.FileAppender
#由於本地windows系統預設支援中文,所以本地即使不配置編碼方式也不會亂碼,若在伺服器亂碼,請調整編碼方式 log4j.appender.F.Encoding
=GBK #會將日誌檔案生成到指定的目錄,例如工程在D盤,則生成至D盤的Logs目錄 #log4j.appender.F.File=/Logs/Logger.txt #本地測試時,會將日誌檔案生成至\jdevhome\jdev\system\oracle.j2ee.10.1.3.41.57\embedded-oc4j\config\logs目錄
#伺服器上會將日誌生成至apps/tech_st/10.1.3/j2ee/home/logs目錄
#伺服器若有多個oacore節點,生成的日誌結果暫時還不清楚 log4j.appender.F.File
=./logs/Logger.txt log4j.appender.F.DatePattern='.'yyyy-MM-dd log4j.appender.F.layout=org.apache.log4j.PatternLayout #log4j.appender.F.layout.ConversionPattern=%d{yyyy-MM-dd@HH:mm:ss,SSS} %-5p (%13F:%L) %3x – %m%n log4j.appender.F.layout.ConversionPattern=%d{yyyy-MM-dd@HH:mm:ss,SSS} %-5p %c.%M:%L - %m%n

#DATABASE

log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL=jdbc:oracle:thin:@host:port:SID
log4j.appender.DATABASE.driver=oracle.jdbc.driver.OracleDriver
log4j.appender.DATABASE.user=USERNAME
log4j.appender.DATABASE.password=PASSWORD
log4j.appender.CONSOLE.Threshold=ERROR
log4j.appender.DATABASE.sql=INSERT INTO LOG4J_TEST(stamp,thread, infolevel,class,messages) VALUES ('%d{yyyy-MM-dd HH:mm:ss}', '%t', '%p', '%l', '%m')

# INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n')

# 寫入資料庫中的表LOG4J的Message欄位中,內容%d(日期)%c: 日誌資訊所在地(類名)%p: 日誌資訊級別%m: 產生的日誌具體資訊 %n: 輸出日誌資訊換行
log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
log4j.appender.DATABASE.layout.ConversionPattern=%d %-5p [%c] %m%n



#Addon for
com.sun.faces.level=FINE

4.程式碼

import org.apache.log4j.Logger;

public class XXXCO
  extends OAControllerImpl
{

private static Logger logger = Logger.getLogger(XXXCO.class.getName());

  public void processRequest(OAPageContext pageContext,
                             OAWebBean webBean)
  {
logger.info("測試 123");
   }

}

 

相關文章