AOP log4j MDC implements custom fields into the database __ database

Source: Internet
Author: User
Tags aop getmessage log log throwable log4j

@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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.