有什么方法可以关闭我的 JavaScript 代码中的所有 console.log 语句以进行测试?
Is there any way to turn off all console.log statements in my JavaScript code, for testing purposes?
在脚本中重新定义 console.log 函数.
Redefine the console.log function in your script.
console.log = function() {}
就是这样,没有更多消息要控制台.
That's it, no more messages to console.
扩展 Cide 的想法.一个自定义记录器,您可以使用它来从您的代码中切换登录/关闭.
Expanding on Cide's idea. A custom logger which you can use to toggle logging on/off from your code.
从我的 Firefox 控制台:
From my Firefox console:
var logger = function()
{
var oldConsoleLog = null;
var pub = {};
pub.enableLogger = function enableLogger()
{
if(oldConsoleLog == null)
return;
window['console']['log'] = oldConsoleLog;
};
pub.disableLogger = function disableLogger()
{
oldConsoleLog = console.log;
window['console']['log'] = function() {};
};
return pub;
}();
$(document).ready(
function()
{
console.log('hello');
logger.disableLogger();
console.log('hi', 'hiya');
console.log('this wont show up in console');
logger.enableLogger();
console.log('This will show up!');
}
);
如何使用上面的记录器"?在您的就绪事件中,调用 logger.disableLogger 以便不记录控制台消息.在要将消息记录到控制台的方法中添加对 logger.enableLogger 和 logger.disableLogger 的调用.
How to use the above 'logger'? In your ready event, call logger.disableLogger so that console messages are not logged. Add calls to logger.enableLogger and logger.disableLogger inside the method for which you want to log messages to the console.
这篇关于如何快速方便地禁用我的代码中的所有 console.log 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 Electron 渲染器进程中创建子窗口时如何修复How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器进程中创建子窗口
mainWindow.loadURL("https://localhost:3000/") 在 EmainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 应用程
Electron webContents executeJavaScript:无法在第二个 loaElectron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:无法在第二个 loadURL 上
如何在angular-cli的组件内使用电子浏览器窗口?how to use electron browser window inside components in angular-cli?(如何在angular-cli的组件内使用电子浏览器窗口?)
ElectronJS - 在 Windows 之间共享 redux 存储?ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之间共享 redux 存储?)
如何在电子应用程序中访问相机/网络摄像头?How to access camera/webcamera inside electron app?(如何在电子应用程序中访问相机/网络摄像头?)