&log

Super simple, but solves these pain points:

  • Lets you leave log statements in production, client-side code.
  • It won't log anything unless localStorage.debug is set.
  • Uses native console rather than trying to wrap it in something (which makes the output ugly).
  • Works with CommonJS.
  • It's just a selective alias for the window.console so the normal API applies.

How to use it:

Step 1. include it:

<script src="andlog.js"></script>

Step 2. Use the console in your code as usual:

console.log("hello");

Step 3. If you want to see log output set a value called debug in localStorage by doing typing this in console:

localStorage.debug = true

If you'd like to use a custom debug key then set localStorage.andlogKey = 'something-else' and then set localStorage['something-else'] = true to enable logging.

Step 4. Refresh the page, you should now see logs.

Step 5. To turn off console, just delete the localStorage flag:

delete localStorage.debug

Step 6. Feel free to deploy to production with console stuff in there.

CommonJS Version

If you're using this on the client but your project is in node.js you can install this with: browserify and npm.

var logger = require('andlog');

logger.log('hello');

This is identical to:

console.log('hello');

You could even get fancy and call it console. However by doing this you take the risk that you'll forget to require it and it'll still work and you'll ship it to production. However, obviously this would work as well:

var console = require('andlog');

console.log('hello');

License

MIT

If you like this, follow @HenrikJoreteg on the twitterwebz.



&log

超级简单,但解决了这些痛点:

  • 让您在生产中留下日志语句,客户端代码。
  • 除非设置了 localStorage.debug ,否则不会记录任何内容。
  • 使用native console ,而不是尝试将其包装在某物中(这使得输出丑陋)。
  • 适用于CommonJS。
  • 这只是一个代码 window.console 的别名,所以普通API适用。

如何使用它:

步骤1.包括它:

<script src="andlog.js"></script>

步骤2.像往常一样在代码中使用 console

console.log("hello");

步骤3.如果要在 localStorage 中查看日志输出设置一个名为 debug 的值,可在控制台中输入:

localStorage.debug = true

如果要使用自定义调试键,请设置 localStorage.andlogKey =‘something-else’,然后设置 localStorage [‘something-else’] = true <代码>启用日志记录。

步骤4.刷新页面,现在应该看到日志。

步骤5.要关闭控制台,只需删除localStorage标志:

delete localStorage.debug

步骤6.随时可以使用控制台内容部署到生产中。

CommonJS版本

如果您在客户端上使用此项目,但您的项目位于node.js中,则可以使用以下命令进行安装: browserify 和npm。

var logger = require('andlog');

logger.log('hello');

这与以下相同:

console.log('hello');

可以甚至得到好奇,称之为 console 。但是,通过这样做,您将承担您将忘记要求的风险,它仍然可以运行,并将其运送到生产中。然而,显然这也将是有效的:

var console = require('andlog');

console.log('hello');

许可证

麻省理工学院

如果您喜欢,请按照twitterwebz上的 @HenrikJoreteg




相关问题推荐