我不调试时如何禁用console.log?

时间:2022-03-05 14:30:43

I have many console.log (or any other console calls) in my code and I would like to use them only when my app is in some kind of "debug mode".

我的代码中有很多console.log(或任何其他控制台调用),我想只在我的应用程序处于某种“调试模式”时才使用它们。

I can't seem to use some kind of logger function and internally use console.log because then I wouldn't know what line fired it. Maybe only with a try/catch, but my logs are very general and I don't want try/catch in my code.

我似乎无法使用某种记录器功能并在内部使用console.log,因为那时我不知道是什么行触发它。也许只有try / catch,但我的日志非常通用,我不想在我的代码中使用try / catch。

What would you recommend?

你会推荐什么?

10 个解决方案

#1


46  

I would probably abuse the short-circuiting nature of JavaScript's logical AND operator and replace instances of:

我可能会滥用JavaScript逻辑AND运算符的短路特性并替换以下实例:

console.log("Foo.");

With:

附:

DEBUG && console.log("Foo.");

Assuming DEBUG is a global variable that evaluates to true if debugging is enabled.

假设DEBUG是一个全局变量,如果启用了调试,则计算结果为true。

This strategy avoids neutering console.log(), so you can still call it in release mode if you really have to (e.g. to trace an issue that doesn't occur in debug mode).

此策略避免中断console.log(),因此如果您确实需要(例如,跟踪调试模式中未发生的问题),您仍然可以在发布模式下调用它。

#2


38  

Just replace the console.log with an empty function for production.

只需将console.log替换为空函数即可生成。

if (!DEBUG_MODE_ON) {
    console = console || {};
    console.log = function(){};
}

#3


25  

Clobbering global functions is generally a bad idea.

Clobbering全球职能通常是一个坏主意。

Instead, you could replace all instances of console.log in your code with LOG, and at the beginning of your code:

相反,您可以使用LOG替换代码中的所有console.log实例,并在代码的开头替换:

var LOG = debug ? console.log.bind(console) : function () {};

This will still show correct line numbers and also preserve the expected console.log function for third party stuff if needed.

这仍然会显示正确的行号,并且如果需要,还可以保留第三方内容的预期console.log函数。

#4


13  

Nowdays, in 2014, I simply use GULP (and recommend everyone to, it's an amazing tool), and I have a package installed which is called stripDebug which does that for you.

现在,在2014年,我只是使用GULP(并推荐大家,它是一个了不起的工具),我有一个名为stripDebug的软件包,它可以为你做到这一点。

(I also use uglify and closureCompiler in production)

(我也在生产中使用uglify和closureCompiler)

#5


4  

One more way to disable console.log in production and keep it in development.

另一种在生产中禁用console.log并使其保持开发的方法。

// overriding console.log in production
if(window.location.host.indexOf('localhost:9000') < 0) {
    console.log = function(){};
}

You can change your development settings like localhost and port.

您可以更改您的开发设置,如localhost和port。

#6


1  

Simple.

简单。

Add a little bash script that finds all references to console.log and deletes them.

添加一个小bash脚本,找到对console.log的所有引用并删除它们。

Make sure that this batch script runs as part of your deployment to production.

确保此批处理脚本作为部署到生产的一部分运行。

Don't shim out console.log as an empty function, that's a waste of computation and space.

不要将console.log称为空函数,这会浪费计算和空间。

#7


0  

This code works for me:

这段代码适合我:

if(console=='undefined' || !console || console==null) {
  var console = {
    log : function (string) {
        // nothing to do here!!
    }
  }
}

#8


0  

我不调试时如何禁用console.log?

The newest versions of chrome show which line of code in which file fired console.log. If you are looking for a log management system, you can try out logeek it allows you to control which groups of logs you want to see.

最新版本的chrome显示哪个文件行触发了console.log。如果您正在寻找日志管理系统,可以尝试使用logeek,它允许您控制要查看的日志组。

#9


0  

// In Development:
var debugMode = true

// In Prod:
var debugMode = false

// This function logs console messages when debugMode is true .
function debugLog(logMessage) {
    if (debugMode) {
        console.log(logMessage);
    }
}

// Use the function instead of console.log
debugLog("This is a debug message");

#10


0  

This Tiny wrapper override will wrap the original console.log method with a function that has a check inside it, which you can control from the outside, deepening if you want to see console logs and not.

这个微小的包装器覆盖将包含原始console.log方法,其中包含一个内部检查的功能,您可以从外部进行控制,如果您想查看控制台日志,则加深。

I chose window.allowConsole just as an example flag but in real-life use it would probably be something else. depending on your framework.

我选择window.allowConsole作为示例标志,但在现实生活中使用它可能是其他的东西。取决于您的框架。

(function(cl){
  console.log = function(){
    if( window.allowConsole )
      cl(...arguments); 
  }
})(console.log)

Usage:

// in development (allow logging)
window.allowConsole = true;
console.log(1,[1,2,3],{a:1});

// in production (disallow logging)
window.allowConsole = false;
console.log(1,[1,2,3],{a:1});

This override should be implement as "high" as possible in the code hierarchy so it would "catch" all logs before then happen. This could be expanded to all the other console methods such as warn, time, dir and so on.

此覆盖应在代码层次结构中尽可能“高”实现,以便在发生之前“捕获”所有日志。这可以扩展到所有其他控制台方法,如warn,time,dir等。

#1


46  

I would probably abuse the short-circuiting nature of JavaScript's logical AND operator and replace instances of:

我可能会滥用JavaScript逻辑AND运算符的短路特性并替换以下实例:

console.log("Foo.");

With:

附:

DEBUG && console.log("Foo.");

Assuming DEBUG is a global variable that evaluates to true if debugging is enabled.

假设DEBUG是一个全局变量,如果启用了调试,则计算结果为true。

This strategy avoids neutering console.log(), so you can still call it in release mode if you really have to (e.g. to trace an issue that doesn't occur in debug mode).

此策略避免中断console.log(),因此如果您确实需要(例如,跟踪调试模式中未发生的问题),您仍然可以在发布模式下调用它。

#2


38  

Just replace the console.log with an empty function for production.

只需将console.log替换为空函数即可生成。

if (!DEBUG_MODE_ON) {
    console = console || {};
    console.log = function(){};
}

#3


25  

Clobbering global functions is generally a bad idea.

Clobbering全球职能通常是一个坏主意。

Instead, you could replace all instances of console.log in your code with LOG, and at the beginning of your code:

相反,您可以使用LOG替换代码中的所有console.log实例,并在代码的开头替换:

var LOG = debug ? console.log.bind(console) : function () {};

This will still show correct line numbers and also preserve the expected console.log function for third party stuff if needed.

这仍然会显示正确的行号,并且如果需要,还可以保留第三方内容的预期console.log函数。

#4


13  

Nowdays, in 2014, I simply use GULP (and recommend everyone to, it's an amazing tool), and I have a package installed which is called stripDebug which does that for you.

现在,在2014年,我只是使用GULP(并推荐大家,它是一个了不起的工具),我有一个名为stripDebug的软件包,它可以为你做到这一点。

(I also use uglify and closureCompiler in production)

(我也在生产中使用uglify和closureCompiler)

#5


4  

One more way to disable console.log in production and keep it in development.

另一种在生产中禁用console.log并使其保持开发的方法。

// overriding console.log in production
if(window.location.host.indexOf('localhost:9000') < 0) {
    console.log = function(){};
}

You can change your development settings like localhost and port.

您可以更改您的开发设置,如localhost和port。

#6


1  

Simple.

简单。

Add a little bash script that finds all references to console.log and deletes them.

添加一个小bash脚本,找到对console.log的所有引用并删除它们。

Make sure that this batch script runs as part of your deployment to production.

确保此批处理脚本作为部署到生产的一部分运行。

Don't shim out console.log as an empty function, that's a waste of computation and space.

不要将console.log称为空函数,这会浪费计算和空间。

#7


0  

This code works for me:

这段代码适合我:

if(console=='undefined' || !console || console==null) {
  var console = {
    log : function (string) {
        // nothing to do here!!
    }
  }
}

#8


0  

我不调试时如何禁用console.log?

The newest versions of chrome show which line of code in which file fired console.log. If you are looking for a log management system, you can try out logeek it allows you to control which groups of logs you want to see.

最新版本的chrome显示哪个文件行触发了console.log。如果您正在寻找日志管理系统,可以尝试使用logeek,它允许您控制要查看的日志组。

#9


0  

// In Development:
var debugMode = true

// In Prod:
var debugMode = false

// This function logs console messages when debugMode is true .
function debugLog(logMessage) {
    if (debugMode) {
        console.log(logMessage);
    }
}

// Use the function instead of console.log
debugLog("This is a debug message");

#10


0  

This Tiny wrapper override will wrap the original console.log method with a function that has a check inside it, which you can control from the outside, deepening if you want to see console logs and not.

这个微小的包装器覆盖将包含原始console.log方法,其中包含一个内部检查的功能,您可以从外部进行控制,如果您想查看控制台日志,则加深。

I chose window.allowConsole just as an example flag but in real-life use it would probably be something else. depending on your framework.

我选择window.allowConsole作为示例标志,但在现实生活中使用它可能是其他的东西。取决于您的框架。

(function(cl){
  console.log = function(){
    if( window.allowConsole )
      cl(...arguments); 
  }
})(console.log)

Usage:

// in development (allow logging)
window.allowConsole = true;
console.log(1,[1,2,3],{a:1});

// in production (disallow logging)
window.allowConsole = false;
console.log(1,[1,2,3],{a:1});

This override should be implement as "high" as possible in the code hierarchy so it would "catch" all logs before then happen. This could be expanded to all the other console methods such as warn, time, dir and so on.

此覆盖应在代码层次结构中尽可能“高”实现,以便在发生之前“捕获”所有日志。这可以扩展到所有其他控制台方法,如warn,time,dir等。