欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PandaJS 使用说明(1.6):日志与 proxy 对象

程序员文章站 2022-03-02 11:11:06
...
PandaJS 使用说明(1.6):日志与 proxy 对象

  这篇文章里我们将继续探讨日志处理(panda.log)和 proxy 对象(panda.proxy)。结合使用 panda.log 和 panda.proxy 可以实现灵活的日志处理,有效地分离业务逻辑和日志记录的代码。panda.log 是在 slf4j 和 log4j 的基础上完成的;panda.proxy 是利用 Rhinio JS 的 __noSuchMethod__ 实现的。

添加日志 proxy

  在 HelloPanda 项目的 scripts/app/proxy 文件夹中创建文件 log.js,内容如下:
(function() {
	// 分别用不同的名称注册两个 log 对象
	var timeLog = panda.log("panda.log.time");
	var aroundLog = panda.log("panda.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 生效,还需要通过 panda.proxy(name) 创建对象,比如在scripts/app/api/users.js 中创建 dbo.users 的 proxy:
api.users = {  
    add: function(params, req, res) {  
        return panda.proxy("dbo.users").add(params);
    },
    update: function(params, req, res) {  
        return panda.proxy("dbo.users").update(params);  
    },  
    remove: function(params, req, res) {  
        return panda.proxy("dbo.users").remove(params);  
    }
};

  以及 scripts/app/page/index.js:
page.index = function(params, req, res) {
	return panda.render("index", function($){
		views.index($, panda.proxy("dbo.users").list());
	});
};

执行顺序

  proxy.log 中的函数与被截获的核心代码的执行顺序如下图所示(priority 较大的在较外层):

PandaJS 使用说明(1.6):日志与 proxy 对象
            
    
    博客分类: Panda JS 使用说明 javascriptlog4jjavaproxyRhino 


实际运行结果

  启动应用并通过 http://localhost访问,可以看到如下日志:
引用
2011-08-27 12:54:16  INFO server - Loading Scripts ...
2011-08-27 12:54:18  INFO server - Starting Monit ...
2011-08-27 12:54:18  INFO server - Starting Server ...
2011-08-27 12:54:19  INFO org.eclipse.jetty.util.log - jetty-7.4.1.v20110513
2011-08-27 12:54:19  INFO org.eclipse.jetty.util.log - started o.e.j.s.ServletContextHandler{/,file:/C:/Users/dev/Downloads/HelloPanda/HelloPanda/webapp/}
2011-08-27 12:54:19  INFO org.eclipse.jetty.util.log - Started SelectChannelConnector@0.0.0.0:80 STARTING
2011-08-27 12:54:25  INFO proxy.log.around - Excuting page.index(), with param: undefined.
2011-08-27 12:54:26  INFO proxy.log.around - Excuting dbo.users.list(), with param: undefined.
2011-08-27 12:54:26  INFO proxy.log.time - dbo.users.list() completed in 0.272 seconds.
2011-08-27 12:54:26  INFO proxy.log.around - dbo.users.list() completed, with result: [{"name":"aaa","desc":"bbb"},{"name":"aaa","desc":"bbb"},{"name":"user1","desc":"desc1"},"..."].
2011-08-27 12:54:26  INFO proxy.log.time - page.index() completed in 1.722 seconds.
2011-08-27 12:54:26  INFO proxy.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-08-27 12:55:01  INFO proxy.log.around - Excuting api.users.add(), with param: {"name":"aaa","desc":"bbb"}.
2011-08-27 12:55:01  INFO proxy.log.around - Excuting dbo.users.add(), with param: {"name":"aaa","desc":"bbb"}.
2011-08-27 12:55:01  INFO proxy.log.around - Excuting dbo.users.list(), with param: undefined.
2011-08-27 12:55:01  INFO proxy.log.time - dbo.users.list() completed in 0.002 seconds.
2011-08-27 12:55:01  INFO proxy.log.around - dbo.users.list() completed, with result: [{"name":"aaa","desc":"bbb"},{"name":"user1","desc":"desc1"},{"name":"user2","desc":"desc2"},"..."].
2011-08-27 12:55:01  INFO proxy.log.time - dbo.users.add() completed in 0.007 seconds.
2011-08-27 12:55:01  INFO proxy.log.around - dbo.users.add() completed, with result: [{"name":"aaa","desc":"bbb"},{"name":"user1","desc":"desc1"},{"name":"user2","desc":"desc2"},"..."].
2011-08-27 12:55:01  INFO proxy.log.time - api.users.add() completed in 0.009 seconds.
2011-08-27 12:55:01  INFO proxy.log.around - api.users.add() completed, with result: [{"name":"aaa","desc":"bbb"},{"name":"user1","desc":"desc1"},{"name":"user2","desc":"desc2"},"..."].