简体   繁体   English

如何在 JavaScript 中正确覆盖 console.warn

[英]How to properly override console.warn in JavaScript

I am overriding console.warn to capture the warnings that are printed into the console of the web browser like this and the file in which I am doing this is track.js shown below.我正在覆盖console.warn以捕获像这样打印到 Web 浏览器控制台的警告,我在其中执行此操作的文件是track.js所示的track.js The code works absolutely fine.代码工作得很好。

File 1文件 1

// Filename - track.js

function getWarnStatements() {
  window.warnStatements = [];
  var oldWarn = console.warn;
  console.warn = function (message) {
  oldWarn.apply(console, arguments);
   window.warnStatements.push({
    type: 'console.warn',
    data: message,
   });  
 };
} 

File 2档案 2

// Filename - xyz.js

console.warn('Hello World');

Let's say I have used console.warn() in some other file which is xyz.js and both track.js and xyz.js runs at the same time.假设我在xyz.js其他文件中使用了console.warn()并且track.jsxyz.js运行。 When we normally check the browser console for the warning we will see the file name on the right side of the warning in the console and when we click on the filename it will take us to the javascript file in which the console.warn was written.当我们正常检查浏览器控制台是否有警告时,我们会在控制台中警告的右侧看到文件名,当我们单击文件名时,它会将我们带到写入console.warn的 javascript 文件。

ISSUE问题

Check the image below.检查下面的图像。 Instead of showing xyz.js it is showing track.js .不是显示xyz.js而是显示track.js How can i prevent this behavior.我怎样才能防止这种行为。 I want it to take the user to the correct file where console.warn was used instead of the file where I am over riding it because it will result in misleading the other developers to the wrong file.我希望它将用户带到使用console.warn的正确文件,而不是我正在使用它的文件,因为它会导致误导其他开发人员使用错误的文件。

在此处输入图片说明

Taking a general view the problem is to intercept and capture arguments supplied in calls to console.warn and complete the intention of showing warnings on the console with an indication of where the warning arose in code.从一般的角度来看,问题是拦截和捕获在对console.warn调用中提供的参数,并完成在控制台上显示警告的意图,并指示代码中警告出现的位置。

First up you can't hide from a real console.warn call where the call was made from, so simplify matters by dropping all subterfuge.首先,您无法躲避真正的console.warn调用,因此可以通过放弃所有诡计来简化问题。 The two options that remain are to produce a full or partial trace of where calls to the substituted warn function are made from.剩下的两个选项是生成完整或部分跟踪调用替代warn函数的位置。

  1. Full trace.全踪。

    In the intercepted warn function make a console.trace call.在拦截的警告函数中进行console.trace调用。

  2. Partial trace.部分痕迹。

    In the intercepted warn function create a new Error object, slice one or more lines from the object's stack property and log them to the console without using console.warn .在拦截的警告函数中创建一个新的 Error 对象,从对象的stack属性中切出一行或多行并将它们记录到控制台,而不使用console.warn

The biggest problem with the second option is that errorObject.stack is not standardized in web standards - while you may test it and achieve outstanding results (with minor differences) in browsers tried, there is no guarantee that it will work in all browsers.第二个选项的最大问题是errorObject.stack没有在 Web 标准中标准化 - 虽然您可以测试它并在尝试的浏览器中获得出色的结果(有细微差别),但不能保证它会在所有浏览器中工作。

In conclusion solutions that you implement depend on the use case - is it for in-house testing, or if on the web do you need to sniff the presence of Error Object stack properties before using them.总之,您实施的解决方案取决于用例 - 是用于内部测试,还是在网络上,您是否需要在使用它们之前嗅探错误对象stack属性的存在。

As a side note, console.xxx functions accept multiple parameters which the posted substitute does not record.作为旁注,console.xxx 函数接受多个参数,这些参数被发布的替代品没有记录。


Here's a quick example showing the console.warn intermediate function (anonymous in the post) being called directly.这是一个快速示例,显示了直接调用的console.warn中间函数(在帖子中匿名)。 Which lines to slice may need fine tuning, and if emoji are not acceptable then substitute as needed:切片哪些行可能需要微调,如果表情符号不可接受,则根据需要替换:

 function interWarn() { let args = arguments; // save arguments; let err = new Error( "trace"); var stack = err.stack; if( !stack) try { throw err; } catch( err) { stack = err.stack; } if( stack) { stack = stack.split('\\n').slice(1,3).join('\\n'); console.info('⚠️ Warning from\\n%s\\n%s', stack, '🟨'); } else console.trace(); console.log.apply(console, args); } function foo() { interWarn("foo warning, %s", "okay?"); } foo();

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

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