本文介绍了console.log包装器保存行号并支持大多数方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何编写一个控制台日志包装器:

How can i write a console log wrapper that:


  • 保持记录的行号和日志语句的文件名保持不变

  • 提供对所有日志严重性方法(错误,日志,调试......)的访问,并在控制台中显示它们所记录的位置

  • 确实提供一些后备(例如,当浏览器不支持错误时调用log方法)

  • 可以在中心位置关闭,所以我可以关闭生产日志

  • 确实处理没有控制台存在的情况,并且不会抛出错误

  • Keeping the recorded line number and file name of the log statement intact
  • Provides access to all log severity methods (error, log, debug, ...) and shows them in the console as they where logged
  • does provide some fallback (for example calls the log method when the browser does not support error)
  • can be switched off in a central location, so I can switch off logging for production
  • does handle the case that no console exists, and does not throw errors

因为登录Java Script是如此不一致,必须有一些解决方案。自己实现它有点乏味,但似乎没有好的库。

Since logging in Java Script is so inconsistent, there must be some solution. Implementing it myself is a little bit tedious, but there seems to be no good library.

我目前发现这个记录器提供了所有功能,但它确实搞乱了行号。

I currently found this logger that provides all the features, but it does mess up the line numbers. http://benalman.com/projects/javascript-debug-console-log/

推荐答案

有我自己的。它符合您的所有条件,除了保持行号完好无损,如果您在另一个函数中包含对 console.log()等的调用,则无法实现。

There is my own log4javascript, which has its own logging console but also provides a wrapper around console.log. It fulfils all your criteria except keeping line numbers intact, which is impossible to achieve if you're wrapping calls to console.log() etc. in another function.

var log = log4javascript.getLogger("main");
var appender = new log4javascript.BrowserConsoleAppender();
log.addAppender(appender);
log.debug("Hello world");

这篇关于console.log包装器保存行号并支持大多数方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 14:00