我正在尝试将控制台调用重定向到log4javascript库。
因此,基本上,任何对console.log的调用都会调用log.info,log是一个Log4javascript实例。
但是,当它调用log.info时,我会得到一个"Function“错误(法语),它的基本意思是”函数预期“。
我还试图从log.info控制台调用IE8,同样的故事。
我不认为它与脚本有关,但如果是这样的话,这里是:
(function (fallback) {
fallback = fallback || function () { };
// function to trap most of the console functions from the FireBug Console API.
var trap = function () {
// create an Array from the arguments Object
var args = Array.prototype.slice.call(arguments);
// console.raw captures the raw args, without converting toString
console.raw.push(args);
var message = args.join(' ');
console.messages.push(message);
fallback(message);
};
// redefine console
if (typeof console === 'undefined') {
console = {
messages: [],
raw: [],
dump: function() { return console.messages.join('\n'); },
log: trap,
debug: trap,
info: trap,
warn: trap,
error: trap,
assert: trap,
clear: function() {
console.messages.length = 0;
console.raw.length = 0 ;
},
dir: trap,
dirxml: trap,
trace: trap,
group: trap,
groupCollapsed: trap,
groupEnd: trap,
time: trap,
timeEnd: trap,
timeStamp: trap,
profile: trap,
profileEnd: trap,
count: trap,
exception: trap,
table: trap
};
}
})(log.info);我以为Log4Javascript支持IE8,那么这里有什么问题呢?谢谢。
发布于 2015-05-31 11:26:36
log4javascript确实支持IE8。问题是在调用log.info时,this是不正确的,它希望作为一个方法被调用,从而将log引用为this的值。我建议通过将记录器对象传递给您的生命并调用它的info方法来修复它:
(function (log) {
var fallback = log ?
function() {
var args = Array.prototype.slice.call(arguments);
log.info.apply(log, args);
} :
function() { };
// function to trap most of the console functions from the FireBug Console API.
var trap = function () {
// create an Array from the arguments Object
var args = Array.prototype.slice.call(arguments);
// console.raw captures the raw args, without converting toString
console.raw.push(args);
var message = args.join(' ');
console.messages.push(message);
fallback(message);
};
// redefine console
if (typeof window.console === 'undefined') {
window.console = {
messages: [],
raw: [],
dump: function() { return console.messages.join('\n'); },
log: trap,
debug: trap,
info: trap,
warn: trap,
error: trap,
assert: trap,
clear: function() {
console.messages.length = 0;
console.raw.length = 0 ;
},
dir: trap,
dirxml: trap,
trace: trap,
group: trap,
groupCollapsed: trap,
groupEnd: trap,
time: trap,
timeEnd: trap,
timeStamp: trap,
profile: trap,
profileEnd: trap,
count: trap,
exception: trap,
table: trap
};
}
})(log);https://stackoverflow.com/questions/30530888
复制相似问题