(OAF)jdeveloper集成log4j并将日志输出到指定文件并写入数据库
阅读原文时间:2020年09月20日阅读:5

参考:

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 Model\src.

For ViewController project this file has to be present as 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{} – %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.\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");
}

}