日期:2014-05-16  浏览次数:20398 次

log4j MDC将日志数据插入数据库
static Logger logger = Logger.getLogger(this.class);


MDC.put("id", id);
MDC.put("returncode", recode);
MDC.put("type", "3");
MDC.put("msg", mes);
MDC.put("unit", unit);

logger.info("用来驱动,不然MDC无法保存");


以上是在方法体内的将需要插入数据库内的数据用MDC(类似map(key/value))保存

log4j.properties   中配置存入数据库


log4j.appender.Database=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.Database.URL=jdbc:oracle:thin:@192.168.2.51:1521:orcl
log4j.appender.Database.BufferSize=1
log4j.appender.Database.Driver=oracle.jdbc.driver.OracleDriver
log4j.appender.Database.User= admin
log4j.appender.Database.Password= admin
log4j.appender.Database.sql=INSERT INTO testlog(id,returncode,dysj,type,msg,unit) values('%X{id}','%X{returncode}',sysdate,'%X{type}','%X{msg}','%X{unit}')
log4j.appender.Database.layout=org.apache.log4j.PatternLayout
log4j.appender.Database.layout.ConversionPattern=-%c-%-4r[%t]%-5p%c%x-%m%n