js 日志类

可以方便记录js 运行时产生中间变量,可以方便跟踪你的代码运行情况

代码
1 var Log =function(path)
2 {
3 this.filehand =null;
4 this.namepath = path;
5 this.begin =function()
6 {
7 var fso =new ActiveXObject("Scripting.FileSystemObject");//firefox 需要下插件
8  if(this.namepath==null)
9 var filename = getFileName();
10 else
11 var filename =this.namepath;
12 this.filehand = fso.CreateTextFile(filename+getFileName()+".txt",true)
13 }
14 this.end =function()
15 {
16 this.filehand.Close();
17 }
18 Log.prototype.write =function()
19 {
20 var args = arguments, argsCount = args.length;
21 if(argsCount ==0) {filehand.WriteLine("参数为空"); filehand.Close(); }
22 for(var i =0;i<argsCount;i++)
23 { try
24 {
25 this.filehand.WriteLine((i+1)+"."+args[i]);
26 }
27 catch(e)
28 {
29 this.filehand.Close();
30 }
31 }
32 }
33 function getFileName()
34 {
35 var d =new Date();
36 var name = d.getFullYear().toString()+"-"+(d.getMonth()+1).toString()+"-"+d.getDate().toString()+"-"+d.getHours().toString()+"-"+d.getMinutes().toString()+"-"+d.getSeconds().toString()+"-"+Math.round(Math.random()*10000);
37 return name //年-月-日-时-分-秒-随机数
38   }
39 }

写的一般,大家有好的想法,欢迎交流!

原文地址:https://www.cnblogs.com/shouhongxiao/p/1677230.html