简体   繁体   English

如何覆盖Chrome中的console.log函数?

[英]How to overwrite the console.log function in Chrome?

I'm working on a web application that uses many different frameworks and custom scripts (jQuery, Backbone, Bootstrap, Less just to name a few) and any of them might print console output at any time. 我正在使用一个使用许多不同框架和自定义脚本(jQuery,Backbone,Bootstrap,Less仅举几例)的Web应用程序,它们中的任何一个都可以随时打印控制台输出。 Some output is from the frameworks I just mentioned, other output is from left over console.log calls left in the code from previous engineers working on the product. 一些输出来自我刚才提到的框架,其他输出来自剩余的console.log调用,这些调用是产品工作的先前工程师在代码中留下的。

It's a problem because all the dozens of lines of extra output mixes with valid output and it's hard to parse all those lines to check that my valid output is working correctly. 这是一个问题,因为所有数十行的额外输出都与有效输出混合在一起,并且很难解析所有这些行以检查我的有效输出是否正常工作。 I want a temporary solution to overwrite the console.log function in Chrome so that I can filter out certain messages and focus on the output I care about. 我想要一个临时解决方案来覆盖Chrome中的console.log函数,以便我可以过滤掉某些消息并集中精力在我关心的输出上。

How do I overwrite the console.log function in Chrome with my own function? 如何使用自己的功能覆盖Chrome中的console.log功能?

I tried creating a Chrome extension already and it kind of worked - I was able to overwrite console.log as far as the extension was concerned, but all references to console.log in the original source were unchanged. 我已经尝试创建一个Chrome扩展程序,并且可以正常工作-就扩展程序而言,我可以覆盖console.log ,但是原始源中对console.log所有引用均未更改。

EDIT: For clarity, I'm not looking for yet another script to add to the site. 编辑:为清楚起见,我没有在寻找另一个脚本添加到站点。 I'm looking for a solution that is entirely implemented either in the web browser, or some other technique that does not involve changing the source code being downloaded in the web app. 我正在寻找一种完全在Web浏览器中实现的解决方案,或者在不涉及更改在Web应用程序中下载源代码的其他某种技术的情况下实现的解决方案。

Not exactly overwriting console.log , but if you right click on any of the lines in the console output you can select Filter > Hide messages from .js. 不完全覆盖console.log ,但是如果右键单击控制台输出中的任何行,您可以选择“过滤器”>“隐藏来自.js的消息”。 It also lets you filter out error messages for 404s etc which you probably wouldn't be able to do even if you could overwrite console.log . 它还可以让您过滤掉404等错误消息,即使您可以覆盖console.log ,您也可能无法执行。

Firefox has a way to change the console logging level for specific extensions from about:config . Firefox可以通过about:config 更改特定扩展名的控制台日志记录级别

Chrome doesn't have this capability. Chrome没有此功能。 Your options: 您的选择:

  1. Filter out all messages from a particular source file by right-clicking the white (blank) part of console message line and choosing Filter > Hide Messages from foo.js . 右键单击控制台消息行的白色(空白)部分,然后选择“ 过滤器”>“从foo.js隐藏消息”,以过滤掉特定源文件中的所有消息。
  2. Override one or more console methods (as you've already done). 覆盖一个或多个控制台方法(如已完成)。 This approach only affects the current scripting context. 此方法仅影响当前的脚本上下文。 Chrome extensions run in their own isolated worlds with their own window objects, global scopes, and console objects. Chrome扩展程序在自己的隔离环境中运行,它们具有自己的窗口对象,全局作用域和控制台对象。 You'll need to paste and run your console-altering scripts in each extension's execution context, and your modifications will only be in effect until the page is unloaded. 您需要在每个扩展的执行上下文中粘贴并运行控制台更改脚本,并且所做的修改仅在卸载页面之前生效。 You can change the console's effective execution context using the dropdown at the bottom of the console that says <page context> . 您可以使用控制台底部显示<page context>的下拉菜单来更改控制台的有效执行上下文。 Note that any frames or iframes in the page also have their own execution contexts, accessible using the frame dropdown (initially set to <top frame> ). 请注意,页面中的任何框架或iframe都有其自己的执行上下文,可使用框架下拉列表(最初设置为<top frame> )进行访问。

log is a function of the console object, you can overwrite it like this: logconsole对象的功能,您可以像这样覆盖它:

var logFn = console.log;  // save the original log function for later use
console.log = function(msg) {
    ...
    logFn.call(console, msg); // call original function if you want to
};

or, use closure to save the old log function: 或者,使用闭包保存旧的日志功能:

console.log = (function(logFn) {
    return function(msg) {
        ...
        logFn.call(console, msg); // call original function if you want to
    };
})(console.log);

You need to do this as early as possible, since you want other frameworks/libraries to use the customized log function. 您需要尽早执行此操作,因为您希望其他框架/库使用自定义的日志功能。

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

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