admin管理员组文章数量:1414628
The Mozilla Developer Network page on the browser-provided Javascript console
object says: "Note: At least in Firefox, if a page defines a console object, that object overrides the one built into Firefox.
". Is there any way to overwrite this object, but still interact with the browser's Web Console?
A use case is to intercept console.log()
calls and do something extra or take different parameters, such as a log classification, while retaining the line number/file information provided when logging to console by tools like Firebug or Google Chrome Inspect Element. The closest matching answer I could find is: Intercepting web browser console messages, but it doesn't dive into interacting with the web console through a custom console object, and using a custom defined debug service like
debug.log = function(string, logLevel) {
checkLogLevel(logLevel); // return false if environment log setting is below logLevel
var changedString = manipulate(string);
console.log(changedString);
}
doesn't retain the line number/file source of the function calling debug.log()
. An option is to do something with console.trace()
and crawl one level up the trace stack, but I was curious about extending console.log()
first. I'd also like to find a solution that works with existing Web Consoles/tools like Firebug rather than creating a custom browser extension or Firebug plugin, but if anybody knows of existing solutions for this I'd be interested in them.
Obviously something like:
console = {
log: function (string) {
console.log('hey!');
}
}
console.log('hey!');
won't work and results in infinite recursion.
The Mozilla Developer Network page on the browser-provided Javascript console
object says: "Note: At least in Firefox, if a page defines a console object, that object overrides the one built into Firefox.
". Is there any way to overwrite this object, but still interact with the browser's Web Console?
A use case is to intercept console.log()
calls and do something extra or take different parameters, such as a log classification, while retaining the line number/file information provided when logging to console by tools like Firebug or Google Chrome Inspect Element. The closest matching answer I could find is: Intercepting web browser console messages, but it doesn't dive into interacting with the web console through a custom console object, and using a custom defined debug service like
debug.log = function(string, logLevel) {
checkLogLevel(logLevel); // return false if environment log setting is below logLevel
var changedString = manipulate(string);
console.log(changedString);
}
doesn't retain the line number/file source of the function calling debug.log()
. An option is to do something with console.trace()
and crawl one level up the trace stack, but I was curious about extending console.log()
first. I'd also like to find a solution that works with existing Web Consoles/tools like Firebug rather than creating a custom browser extension or Firebug plugin, but if anybody knows of existing solutions for this I'd be interested in them.
Obviously something like:
console = {
log: function (string) {
console.log('hey!');
}
}
console.log('hey!');
won't work and results in infinite recursion.
Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Dec 1, 2012 at 0:02 Alex RossAlex Ross 3,8393 gold badges27 silver badges29 bronze badges 1- tobyho./2012/07/27/taking-over-console-log – Alfonso Rubalcava Commented Dec 4, 2014 at 19:12
2 Answers
Reset to default 3It's easy, just save a reference to the (original) console before overwriting it:
var originalConsole = window.console;
console = {
log: function(message) {
originalConsole.log(message);
// do whatever custom thing you want
}
}
You can do:
var colors = {
DEFAULT: '\033[m',
RED: '\033[31m',
GREEN: '\033[32m',
BLUE: '\033[34m'
};
var print = {
out: function(message) {
console.log(message);
},
read: function(message) {
prompt(message + ' ');
},
color: function(message, color) {
if (color == 'RED') {
console.log(`${colors.RED}${message}${colors.DEFAULT}`);
}
else if (color == 'GREEN') {
console.log(`${colors.GREEN}${message}${colors.DEFAULT}`);
}
else if (color == 'BLUE') {
console.log(`${colors.BLUE}${message}${colors.DEFAULT}`);
}
else {
console.log(`${colors.RED}ValueError: \"${color}\" is not in the colors set.`);
}
}
};
You can test it using:
print.out('Hello World!');
print.read('Hello World!');
print.color('Hello World!', 'RED');
print.color('Hello World!', 'GREEN');
print.color('Hello World!', 'BLUE');
本文标签:
版权声明:本文标题:javascript - Creating a custom "console" object to interact with Web Console in browser for custom debugger - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745164218a2645595.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论