本篇文章来自一个需求,前端websocket会收到各类消息,可是调试的时候,我但愿把websoekt推送过来的消息都保存到一个文件里,若是出问题的时候,我能够把这些消息的日志文件提交给后端开发区分析错误。可是在浏览器里,js通常是不能写文件的。鼠标另存为的方法也是不太好,由于会保存全部的console.log的输出。因而,终于找到这个debugout.js。
debugout.js的原理是将全部日志序列化后,保存到一个变量里。固然这个变量不会无限大,由于默认的最大日志限制是2500行,这个是可配置的。另外,debugout.js也支持在localStorage里存储日志的。
前端
通常来讲,能够使用打开console面板,而后右键save,是能够将console.log输出的信息另存为log文件的。可是这就把全部的日志都包含进来了,如何只保存我想要的日志呢?
(调试输出)从您的日志中生成能够搜索,时间戳,下载等的文本文件。 参见下面的一些例子。git
Debugout的log()接受任何类型的对象,包括函数。 Debugout不是一个猴子补丁,而是一个单独的记录类,你使用而不是控制台。github
调试的一些亮点:web
下图是使用downloadLog方法下载的日志文件。后端
官方提供的demo示例,欢迎试玩。http://inorganik.github.io/de...浏览器
在脚本顶部的全局命名空间中建立一个新的调试对象,并使用debugout的日志方法替换全部控制台日志方法:websocket
var bugout = new debugout(); // instead of console.log('some object or string') bugout.log('some object or string');
···
// log in real time (forwards to console.log)
self.realTimeLoggingOn = true;
// insert a timestamp in front of each log
self.useTimestamps = false;
// store the output using window.localStorage() and continuously add to the same log each session
self.useLocalStorage = false;
// set to false after you're done debugging to avoid the log eating up memory
self.recordLogs = true;
// to avoid the log eating up potentially endless memory
self.autoTrim = true;
// if autoTrim is true, this many most recent lines are saved
self.maxLines = 2500;
// how many lines tail() will retrieve
self.tailNumLines = 100;
// filename of log downloaded with downloadLog()
self.logFilename = 'log.txt';
// max recursion depth for logged objects
self.maxDepth = 25;
···session
https://github.com/inorganik/...less
我本身也模仿debugout.js写了一个日志保存的项目,该项目能够在ie10及以上下载日志。
debugout.js在ie浏览器上下载日志的方式是有问题的。
项目地址:https://github.com/wangduandu...socket