Implementation and logging of AOP in JavaScript __java

Source: Internet
Author: User
Tags aop stack trace

Usually, in the FAT client B/s from the program, front-end JavaScript script is the most difficult to maintain, because of the diversity of the client's operating environment and the script to run the owner of the dependency, developers often can not reproduce the user's errors on the computer. So, when a lot of JavaScript exists, Monitoring the running state of the system, including logs and performance, is particularly important. When a client fails or is down, you can ask the user to send the system running history and current information to the developer for analysis.

However, too much intrusion into business logic code can negatively affect the stability and readability of your code. The following program can extend the functionality of a program without modifying the program code to easily monitor the stack and procedure of the call.

/**
*
*aopsupport 1.0
*written by Jarezwu
*last Updated on 2007-3-9
*
*/
aopsupport={
/**
* JavaScript method Agent, wrapping a method
* fn: Method name
* Obj: Method owner
* Roundfunc: Packing method
*/
Round:function (fn/*string*/,obj/*object*/,roundfunc/*function*/) {
var preserved=["alert"];
for (Var i=0,len=preserved.length;i<len;i++) {
if (PRESERVED[I]==FN) {
throw new Error ("Unable to monitor" +FN);
}
}
if (!obj) {//The default context is global
obj=self;
}
var of=obj[fn];
if (!of) {
Return
}
var nf=function () {
var args=arguments;
var invocation=new function () {
This.args=args;
This.method=of;
This.context=obj;
var self=this;
This.proceed=function () {
Return self.method.apply (Self.context,self.args);
}
}
Return Roundfunc.call (obj,invocation);
}
OBJ[FN]=NF;
}
}

An example is provided:

1. Add logic

function foo () {
Alert ("core business");
}
Foo ();
function Bar () {
Alert ("Value-added services");
}
Aopsupport.round ("foo", Null,function (invocation) {
Invocation.proceed ();
Bar ();
});
Foo ();

2. Record Log

logger={//a log recorder
Trace:function (info) {
Log stack Trace
},
Error:function (info) {
Log error
},
Debug:function (info) {
Log output
},
Dump:function () {
Output logged Info
}
};
Var obj=new object ();//Sample Object
Obj.doit=function () {//Sample method
Do something
}
Obj.doit ();
The process of wrapping the procedure below
Aopsupport.round ("DoIt", Obj,function (invocation) {
Logger.trace ("Start:" +invocation.method);/track Start
try{
Logger.debug (Invocation.args);//Record parameters
var result=invocation.proceed ()//Call
Logger.debug (result);//Record results
}catch (e) {
Logger.error (e);//Log exception
}
Logger.trace ("End:" +invocation.method);/close Trace
});
Obj.doit ()//Run
Logger.dump ()//print trace information


Related Article

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.