PureJS (6.3):Rhino 中的日志与 proxy 对象
程序员文章站
2022-03-02 11:10:06
...
PureJS (6.3):Rhino 中的日志与 proxy 对象
这篇文章里我们将继续探讨日志处理(pure.log)和 proxy 对象(pure.proxy)。结合使用 pure.log 和 pure.proxy 可以实现灵活的日志处理,有效地分离业务逻辑和日志记录的代码。pure.log 是在 slf4j 和 log4j 的基础上完成的;pure.proxy 是利用 Rhinio JS 的 __noSuchMethod__ 实现的。
代码示例
下载附件中的 PureJS 工程,打开文件 scripts/app/proxy/log.js ,可以看到如下代码:
(function() { // 分别用不同的名称注册两个 log 对象 var timeLog = pure.log("pure.log.time"); var aroundLog = pure.log("pure.log.around"); // 表示日志输出格式的字符串 var timeString = "{}.{}() completed in {} seconds."; var inputString = "Excuting {}.{}(), with param: {}."; var outputString = "{}.{}() completed, with result: {}."; // proxy.log 对象,用于记录日志,优先级 100 proxy.log = { priority: 100 }; // proxy.log.time 对象,记录执行时间 proxy.log.time = { // 优先级 80,将在内层执行 priority: 80, // 截获以 "api."、"page."、"dbo." 开头的表达式 expr: /^(api|page|dbo)\./, // 代理函数,name: 对象名称,method:函数名称,args:函数调用参数 func: function(name, method, args) { // 在执行函数的前后,插入计执行时间的代码 var start = new Date().getTime(); var ret = this[method].apply(this, args); var seconds = (new Date().getTime() - start) / 1000; // 输出日志并返回 timeLog.info(timeString, [ name, method, seconds ]); return ret; } } // proxy.log.around 对象,记录函数的输入和输出 proxy.log.around = { // 优先级 100,将在外层执行 priority: 100, // 截获以 "api."、"page."、"dbo." 开头的表达式 expr: /^(api|page|dbo)\./, // 代理函数,name: 对象名称,method:函数名称,args:函数调用参数 func: function(name, method, args) { // 执行函数前,将第一个参数序列化后输出到日志 var toLog = JSON.stringify(args[0]); aroundLog.info(inputString, [name, method, toLog]); // 执行函数 var ret = this[method].apply(this, args); // 缩短函数执行结果并序列化,输出到日志 toLog = JSON.stringify(shorten()); aroundLog.info(outputString, [name, method, toLog]); // 返回结果 return ret; // 缩短 ret 以便显示,实现略。 function shorten() { ... } } } }());
要使得 proxy 生效,还需要通过 pure.proxy(name) 创建对象,比如在scripts/app/api/users.js 中创建 dbo.users 的 proxy:
api.users = function(){ return { save: function(params) { pure.proxy("dbo.users").save(params); }, remove: function(params) { pure.proxy("dbo.users").remove(params); } } }();
执行顺序
proxy.log 中的函数与被截获的核心代码的执行顺序如下图所示(priority 较大的在较外层):
实际运行结果
启动 mongod,并像 6.1 中介绍的那样用 Rhino 启动项目,我们可以看到如下日志:
引用
2011-07-30 07:51:01 INFO pure.log.around - Excuting page.index(), with param: undefined.
2011-07-30 07:51:01 INFO pure.log.around - Excuting dbo.users.list(), with param: undefined.
2011-07-30 07:51:01 INFO pure.log.time - dbo.users.list() completed in 0.004 seconds.
2011-07-30 07:51:01 INFO pure.log.around - dbo.users.list() completed, with result: [{"name":"user1","desc":"desc1"},{"name":"user2","desc":"desc2"},{"name":"user3","desc":"desc3"}].
2011-07-30 07:51:02 INFO pure.log.time - page.index() completed in 0.271 seconds.
2011-07-30 07:51:02 INFO pure.log.around - page.index() completed, with result: "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-str...".
2011-07-30 07:51:01 INFO pure.log.around - Excuting dbo.users.list(), with param: undefined.
2011-07-30 07:51:01 INFO pure.log.time - dbo.users.list() completed in 0.004 seconds.
2011-07-30 07:51:01 INFO pure.log.around - dbo.users.list() completed, with result: [{"name":"user1","desc":"desc1"},{"name":"user2","desc":"desc2"},{"name":"user3","desc":"desc3"}].
2011-07-30 07:51:02 INFO pure.log.time - page.index() completed in 0.271 seconds.
2011-07-30 07:51:02 INFO pure.log.around - page.index() completed, with result: "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-str...".
在界面的输入框中输入 user 的 name 和 desc,并点击save,可以看到如下日志:
引用
2011-07-30 07:52:08 INFO pure.log.around - Excuting api.users.save(), with param: {"name":"aaa","desc":"bbb"}.
2011-07-30 07:52:08 INFO pure.log.around - Excuting dbo.users.save(), with param: {"name":"aaa","desc":"bbb"}.
2011-07-30 07:52:08 INFO pure.log.time - dbo.users.save() completed in 0.001 seconds.
2011-07-30 07:52:08 INFO pure.log.around - dbo.users.save() completed, with result: undefined.
2011-07-30 07:52:08 INFO pure.log.time - api.users.save() completed in 0.003 seconds.
2011-07-30 07:52:08 INFO pure.log.around - api.users.save() completed, with result: undefined.
2011-07-30 07:52:08 INFO pure.log.around - Excuting dbo.users.save(), with param: {"name":"aaa","desc":"bbb"}.
2011-07-30 07:52:08 INFO pure.log.time - dbo.users.save() completed in 0.001 seconds.
2011-07-30 07:52:08 INFO pure.log.around - dbo.users.save() completed, with result: undefined.
2011-07-30 07:52:08 INFO pure.log.time - api.users.save() completed in 0.003 seconds.
2011-07-30 07:52:08 INFO pure.log.around - api.users.save() completed, with result: undefined.