简体   繁体   English

在生产代码中保留 console.log() 调用是否可以?

[英]Is it OK to leave console.log() calls in production code?

Regarding the "console.log" function, is it used only for testing/debugging purposes?关于“console.log”功能,它是否仅用于测试/调试目的? So would I have to remove them for the final coding stage where everything is completed?那么我是否必须在一切都完成的最后编码阶段删除它们? I'm confused.我糊涂了。 For instance, when I'm declaring a function, how would I change the following?例如,当我声明一个函数时,我将如何更改以下内容?

function greeting() { console.log('Hello World'); }

I apologize if this is a stupid question.如果这是一个愚蠢的问题,我深表歉意。

console.log is used for outputting/logging text to the console. console.log用于向控制台输出/记录文本。 It is not necessary to remove it when you release the web app/website.发布 Web 应用程序/网站时无需将其删除。

For more information on how to use console.log , click here .有关如何使用console.log更多信息,请单击此处

Yes, it should be used only for testing/debugging purposes.是的,它应该仅用于测试/调试目的。 I would suggest not to use it even for debugging/testing purposes.我建议即使出于调试/测试目的也不要使用它。 Better, start a debugger, attach a breakpoint on a statement that you want to analyze and you are sorted.更好的是,启动调试器,在要分析的语句上附加断点并进行排序。

Example in https://developers.google.com/web/tools/chrome-devtools/javascript https://developers.google.com/web/tools/chrome-devtools/javascript 中的示例

It is mainly for printing relevant info to the browsers console window, although there is a better alternative for debugging.它主要用于将相关信息打印到浏览器控制台窗口,尽管有更好的调试替代方法。 See debugger statement .请参阅调试器语句 I would not leave console.logs lying around all over the place whereas it would seem a bit sloppy.我不会让 console.logs 到处都是,但它看起来有点马虎。 Besides, it could be harder to follow depending on how many there are.此外,根据数量的不同,可能更难遵循。

There are however times when you would want to leave the console logs and those times are merely for conditional local or server environment execution.然而,有时您会想要离开控制台日志,而这些时间仅用于有条件的本地或服务器环境执行。

console.log or any console methods are for showing any information on browser developer console console.log或任何控制台方法用于在浏览器开发者控制台上显示任何信息

It varies, Production env means it's solely for end user and everything is correct to make your website useful.它因人而异, Production意味着它仅供最终user ,一切都是正确的,以使您的网站有用。 That is why websites have dev / QA / Stage env where you want to have some information for developers .这就是为什么网站有dev / QA / Stage env 的原因,您希望在其中为developers提供一些信息。

console.log or any methods associated to console uses developer console of the browser, with that said it's clear that it totally meant for someone who is using your website more then just getting information [hacker, developer, auditor, ] etc. console.log或与console相关的任何方法都使用浏览器的开发人员console ,很明显,它完全适用于使用您网站的人,而不仅仅是获取信息[hacker, developer, auditor, ]等。

For End user which we expect to be just the normal people who are on your website for their need and don't have any technical requirement, then loggin an error to console is useless !对于End user ,我们希望他们只是在您的网站上满足他们需要并且没有任何技术要求的普通人,那么将error到控制台是没有用的! Instead you need some kind of pop up or notification to inform end user that something went wrong or any useful information.相反,您需要某种弹出或通知来通知最终用户出现问题或任何有用的信息。

But there is kind of useful scenarios which you want to inform user to avoid any bad activity suggested by any hacker.但是有一些有用的场景,您希望通知用户避免任何黑客建议的任何不良活动。 Like facebook does, as many of the hacker uses JS snippets to do some automatic changes to website ask normal users to use console and post their snippets.就像facebook一样,许多黑客使用JS段对网站进行一些自动更改,要求普通用户使用控制台并发布他们的代码段。 In this case console methods are useful在这种情况下, console方法很有用

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM