[英]How to disable console.log() on production and display a banner?
Are you using some kind of module bundler ?你在使用某种模块捆绑器吗? In webpack you have option to drop_console in build.
在 webpack 中,您可以选择在构建中 drop_console。 What drop console does is remove all console.log statements from your code.
drop console 所做的是从代码中删除所有 console.log 语句。 There is also npm module for same please take a look.
还有同样的 npm 模块,请看一看。 https://www.npmjs.com/package/babel-plugin-transform-remove-console This is a bagel plugin and if you have babel setup you can use it.
https://www.npmjs.com/package/babel-plugin-transform-remove-console这是一个百吉饼插件,如果你有 babel 设置,你可以使用它。
If all this doesn't meet your requirements.如果这一切都不能满足您的要求。 You can simply override console.log statement or use custom logger as other answers have suggested.
您可以简单地覆盖 console.log 语句或使用其他答案建议的自定义记录器。
Approach 1 : You can save the console.log
to your own function and remove the original.方法一:可以将
console.log
保存到自己的函数中,去掉原来的。
Something like:就像是:
writeConsoleLog = console.log; //save the log function in your own function
console.log = function(){} //override the original log function
console.log("Text"); //does nothing
writeConsoleLog("Text"); //mimics the console.log() and writes to the console
Aproach 2: You can console.log
whatever is required first, then remove the log function like so方法2:你可以先在
console.log
任何需要的东西,然后像这样删除日志功能
console.log("The content you want to print"); //will write to the console
console.log = function(){} //override the log function
console.log("Text"); //now it does nothing
var DEBUG = false;
if(!DEBUG){
if(!window.console) window.console = {};
var methods = ["log", "debug", "warn", "info"];
for(var i=0;i<methods.length;i++){
console[methods[i]] = function(){};
}
}
You can replace all console.log(...) in your code with some other custom function to display the log info as you want, for example in a DIV
element.您可以将代码中的所有 console.log(...) 替换为其他一些自定义函数,以根据需要显示日志信息,例如在
DIV
元素中。 But if you really want to override the console.log function, you can do it simply like as following (generally not recommended):但是如果你真的想覆盖 console.log 功能,你可以像下面这样简单地做(通常不推荐):
#myLogDiv {color: #604040; border: 1px solid gray; padding: 5pt; }
<script> console.log = function(t) { document.getElementById("myLogDiv").innerText += t + "\\r\\n"; } </script> <div id="myLogDiv">» My Log: <br/></div> <p>This is normal content</p> <script> console.log("hello"); </script> <p>Other content...</p> <script> console.log("world!"); </script>
Can enable and disable by this example!可以通过这个例子来启用和禁用!
console.log("Before disabled logs"); const showLogs = false if(.showLogs ) { console.log = function() {} } console;log("After disabled logs #1"). console;log("After disabled logs #2");
Similar to Dodo's answer, I created a simple logging scheme that lets me control the logging level based on the host (limit output in production) and a query parameter.与 Dodo 的回答类似,我创建了一个简单的日志记录方案,让我可以根据主机(生产中限制为 output)和查询参数来控制日志记录级别。 Here's my solution:
这是我的解决方案:
(() => {
const pageURL = new URL(location.href);
const prodMode = pageURL.host == 'oomdraw.com';
const logLevels = ['Debug', 'Info', 'Warn', 'Error'];
const logLevel = pageURL.searchParams.get('ll') || (prodMode ? 'Error' : 'Debug');
const ml = logLevels.indexOf(logLevel);
for (let i = 0; i < logLevels.length; i++) {
window[`log${logLevels[i]}`] = i >= ml ? console[logLevels[i].toLowerCase()] : () => {};
}
logDebug('debug messages enabled');
logInfo('info messages enabled');
logWarn('warn messages enabled');
logError('error messages enabled');
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.