@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