javascript console.log 的替代品?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26245417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:42:40  来源:igfitidea点击:

Alternatives to console.log?

javascriptconsole

提问by Doug

I was wondering if there are any alternatives to writing something to the console without using console messages. My project removes all console statements before finalizing the build, but in a unique case I need to find a way to display something to the user via the console. Is this even possible without console statements?

我想知道是否有任何替代方法可以在不使用控制台消息的情况下向控制台写入内容。我的项目在完成构建之前删除了所有控制台语句,但在一个独特的情况下,我需要找到一种方法通过控制台向用户显示某些内容。如果没有控制台语句,这甚至可能吗?

回答by rink.attendant.6

If your build process only touches the consolestatements in your project and not dependencies, you can try using one of many third-party logging frameworks.

如果您的构建过程只涉及console项目中的语句而不涉及依赖项,您可以尝试使用许多第三方日志记录框架之一。

It is important to note that internally they still call console, and if your build process strips out code of third-party dependencies then I must say that your process requires some changes.

重要的是要注意,它们在内部仍然调用console,如果您的构建过程剥离了第三方依赖项的代码,那么我必须说您的过程需要一些更改。

回答by Aleksandr Golovatyi

You can write to console only through Console object. The Console object provides access to the browser's debugging console.

您只能通过 Console 对象写入控制台。Console 对象提供对浏览器调试控制台的访问。

console.log("Failed to open the specified link")

You can use other methods for debugging: info()

您可以使用其他方法进行调试:info()

console.info('Debug message');

warn()

警告()

console.warn('Debug message');

error()

错误()

console.error('Debug error message')

time()

时间()

console.time(label);

table()

桌子()

console.table(["apples", "oranges", "bananas"]);

trace()

痕迹()

console.trace();

As for me, I like using console.table() and console.group()

至于我,我喜欢使用 console.table() 和 console.group()

Additional info on the MDN - https://developer.mozilla.org/en-US/docs/Web/API/Consoleand on the article https://medium.freecodecamp.org/how-to-get-the-most-out-of-the-javascript-console-b57ca9db3e6d

关于 MDN 的其他信息 - https://developer.mozilla.org/en-US/docs/Web/API/Console和文章https://medium.freecodecamp.org/how-to-get-the-most -out-of-the-javascript-console-b57ca9db3e6d