aop log4j mdc实现自定义字段存入数据库

来源:互联网 发布:数据清新图 编辑:程序博客网 时间:2024/06/07 04:53

@Aspect
public class ExceptionLog {
    Log log = LogFactory.getLog(ExceptionLog.class);
    
//    @AfterThrowing(pointcut ="execution(* com.sinosoft.csrkt.*.service.*.*(..))" +
//            "||execution(* com.sinosoft.query.service.*.*(..))" +
//            "||execution(* com.sinosoft.sysmanage.service.*.*(..))",throwing="e")
//    public void adfterThrowException(JoinPoint jp,Throwable e) throws Throwable{
//        PrintWriter pw = null;
//        Object target = jp.getTarget();
//        try{
//            pw = new PrintWriter(new FileOutputStream(ConstantUtil.EXCEPTION_LOG_PATH, true));
//            pw.println(new Date());
//            pw.println("Error happened in class: " + target.getClass().getName());
//            pw.println("Error happened in method: " + jp.getSignature().getName());
//            pw.println("Exception class: " + e.getClass().getName());
//            pw.println("Exception message:" + e.getMessage());
//            pw.println("*************************************");
//            pw.flush();
//        }finally{
//            if(pw!=null)pw.close();
//        }
//        
//    }
    
    @AfterThrowing(pointcut ="execution(* com.sinosoft.csrkt.*.service.*.*(..))" +
            "||execution(* com.sinosoft.query.service.*.*(..))" +
            "||execution(* com.sinosoft.sysmanage.service.*.*(..))",throwing="e")
    public void adfterThrowException(JoinPoint jp,Throwable e) throws Throwable{
        Object target = jp.getTarget();
        MDC.put("class", target.getClass().getName());
        MDC.put("method",jp.getSignature().getName());
        log.error(new Date()+"/n"
                +"/n"+"Error happened in class: " + target.getClass().getName()
                +"/n"+"Error happened in method: " + jp.getSignature().getName()
                +"/n"+"Exception class: " + e.getClass().getName()
                +"/n"+"Exception message:" + e.getMessage());
        
    }
}

public class ****JDBCAppender extends JDBCAppender{
    @Override
    protected Connection getConnection() throws SQLException {
        return ConnectPool.getInstance().getConnection();
    }
   
    @Override
    protected void closeConnection(Connection con) {
        ConnectPool.closeConn(con);
    }
   
}

appicationContext.xml
<aop:aspectj-autoproxy />
<bean class="com.sinosoft.log.ExceptionLog" />

properties:
### direct log message to db ###
log4j.rootLogger=error,db
###JDBCAppender
log4j.appender.db=com.sinosoft.log.CSRKTJDBCAppender
###buffer
log4j.appender.db.BufferSize=1
log4j.appender.db.driver=oracle.jdbc.driver.OracleDriver
log4j.appender.db.URL=jdbc:oracle:thin:@192.168.1.250:1521:ORCL
log4j.appender.db.user=wgc
log4j.appender.db.password=wgc
log4j.appender.db.sql=insert into log_error(id,errortime,errorclass,errormethod,errormsg) values (SEQUENCE_LOG.nextval,to_date('%d{yyyy-MM-dd HH:mm:ss}','yyyy-MM-dd HH24:mi:ss'),'%X{class}','%X{method}','%m')
log4j.appender.db.layout=org.apache.log4j.PatternLayout

原创粉丝点击