实例:
package com.yss.util;
import org.apache.log4j.Category;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.apache.log4j.jdbc.JDBCAppender;
import org.apache.log4j.spi.LoggingEvent;
public class ReJDBCAppender extends JDBCAppender {
protected String getLogStatement(LoggingEvent event) {
String fqnOfCategoryClass = event.fqnOfCategoryClass;
Category logger = Logger.getRootLogger();
Priority level = event.getLevel();
Object message = event.getMessage();
Throwable throwable = null;
ReLoggingEvent bEvent = new ReLoggingEvent(fqnOfCategoryClass, logger,
level, message, throwable);
return super.getLogStatement(bEvent);
}
}
package com.yss.util;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
import org.apache.log4j.spi.LoggingEvent;
public class ReLoggingEvent extends LoggingEvent {
private static final long serialVersionUID = 1L;
public ReLoggingEvent(String fqnOfCategoryClass, Category logger,
Priority level, Object message, Throwable throwable) {
super(fqnOfCategoryClass, logger, level, message, throwable);
}
/**
* 线程名单引号做处理
*/
public String getThreadName() {
String thrdName=super.getThreadName();
if(thrdName.indexOf("'")!=-1){
thrdName=thrdName.replaceAll("'", "''");
}
return thrdName;
}
/**
* 对插入的message中包含的单引号(')做处理
* @see org.apache.log4j.spi.LoggingEvent#getRenderedMessage()
*/
public String getRenderedMessage() {
String renderedMessage = super.getRenderedMessage();
//插入的message中包含(')单引号需要处理
if(renderedMessage.indexOf("'")!=-1)
renderedMessage=renderedMessage.replaceAll("'", "''");
return renderedMessage;
}
}
配置:
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} %5p [%c]- %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=reportLog.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} %5p [%c]- %m%n
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=2
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info,stdout,db
#log4j.logger.business=INFO,db
#log4j.appender.db=com.neam.commons.MyJDBCAppender
log4j.appender.db=com.yss.util.ReJDBCAppender
log4j.appender.db.BufferSize=1
log4j.appender.db.driver=${com.yss.db.driverClassName}
log4j.appender.db.URL=${com.yss.zt.url}
log4j.appender.db.user=${com.yss.zt.username}
log4j.appender.db.password=${com.yss.zt.password}
log4j.appender.db.sql=insert into RP_REPORTLOG(FLOW,USERNAME,REPORTCODE,REPORTDATE,LOGLEVEL,LOGMSG ,JCLASS ,JMETHOD ,ERRORTIME ) values (REPORTLOG.nextval,'%X{userName}','%X{reportCode}','%X{reportDate}','%p','%m','%M','%C','%d{yyyy-MM-dd HH:mm:ss}')
log4j.appender.db.layout=org.apache.log4j.PatternLayout
### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug
### log just the SQL
#log4j.logger.org.hibernate.SQL=debug
### log cache activity ###
#log4j.logger.org.hibernate.cache=debug
### log transaction activity
#log4j.logger.org.hibernate.transaction=debug
### log JDBC resource
#log4j.logger.org.hibernate.jdbc=debug