我想把console.log输出写到一个
层中。
例如:
document.write(console.log(5+1)); //Incorrect, random example
有人能给我提供解决我的问题的方案吗?
谢谢。
编辑:
我的意思是,例如:
console.log("hi");
它在屏幕上显示输出"hi"。
例如:
document.write(console.log(5+1)); //Incorrect, random example
有人能给我提供解决我的问题的方案吗?
谢谢。
编辑:
我的意思是,例如:
console.log("hi");
它在屏幕上显示输出"hi"。
您可以覆盖默认实现的console.log()
(function () {
var old = console.log;
var logger = document.getElementById('log');
console.log = function (message) {
if (typeof message == 'object') {
logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(message) : message) + '<br />';
} else {
logger.innerHTML += message + '<br />';
}
}
})();
演示: Fiddle
String(message)
?为什么不直接用 message
? - zerkms稍作改进 @arun-p-johny 的答案:
在 HTML 中,
<pre id="log"></pre>
在js中,(function () {
var old = console.log;
var logger = document.getElementById('log');
console.log = function () {
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] == 'object') {
logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(arguments[i], undefined, 2) : arguments[i]) + '<br />';
} else {
logger.innerHTML += arguments[i] + '<br />';
}
}
}
})();
开始使用:
console.log('How', true, new Date());
console.log
功能,需要在新的控制台日志函数末尾添加 old(...arguments);
。记得使用扩展运算符! - Timold.apply(console, arguments);
的作用是保留原有的参数,也可以起到相同的效果。 - Tim有点晚了,但我进一步完善了@Hristiyan Dodov的答案。
所有控制台方法现在都被重新连接,并且在文本溢出的情况下,包括一个可选的自动滚动到底部功能。颜色现在基于日志记录方法而不是参数。
rewireLoggingToElement(
() => document.getElementById("log"),
() => document.getElementById("log-container"), true);
function rewireLoggingToElement(eleLocator, eleOverflowLocator, autoScroll) {
fixLoggingFunc('log');
fixLoggingFunc('debug');
fixLoggingFunc('warn');
fixLoggingFunc('error');
fixLoggingFunc('info');
function fixLoggingFunc(name) {
console['old' + name] = console[name];
console[name] = function(...arguments) {
const output = produceOutput(name, arguments);
const eleLog = eleLocator();
if (autoScroll) {
const eleContainerLog = eleOverflowLocator();
const isScrolledToBottom = eleContainerLog.scrollHeight - eleContainerLog.clientHeight <= eleContainerLog.scrollTop + 1;
eleLog.innerHTML += output + "<br>";
if (isScrolledToBottom) {
eleContainerLog.scrollTop = eleContainerLog.scrollHeight - eleContainerLog.clientHeight;
}
} else {
eleLog.innerHTML += output + "<br>";
}
console['old' + name].apply(undefined, arguments);
};
}
function produceOutput(name, args) {
return args.reduce((output, arg) => {
return output +
"<span class=\"log-" + (typeof arg) + " log-" + name + "\">" +
(typeof arg === "object" && (JSON || {}).stringify ? JSON.stringify(arg) : arg) +
"</span> ";
}, '');
}
}
setInterval(() => {
const method = (['log', 'debug', 'warn', 'error', 'info'][Math.floor(Math.random() * 5)]);
console[method](method, 'logging something...');
}, 200);
#log-container { overflow: auto; height: 150px; }
.log-warn { color: orange }
.log-error { color: red }
.log-info { color: skyblue }
.log-log { color: silver }
.log-warn, .log-error { font-weight: bold; }
<div id="log-container">
<pre id="log"></pre>
</div>
async: false
的ajax
调用的嵌套循环,其中有许多console.log
调用,但输出只在我的循环完成处理后才会更新。(我的动画也遇到了同样的问题,它只在我的循环完成后才播放。)我尝试在ajax
的success: function( result ) {
中添加一个调用,但无法使其正常工作。感谢您的帮助。 - MeSo2log-container
会出现零散的写入。所以也许我只需要加入一些暂停来更新,以便有机会赶上循环。不确定如何实现... - MeSo2我稍晚地提供了 Arun P Johny 的答案 更高级的版本。他的解决方案无法处理多个 console.log()
参数,也无法访问原始函数。
这是我的版本:
(function (logger) {
console.old = console.log;
console.log = function () {
var output = "", arg, i;
for (i = 0; i < arguments.length; i++) {
arg = arguments[i];
output += "<span class=\"log-" + (typeof arg) + "\">";
if (
typeof arg === "object" &&
typeof JSON === "object" &&
typeof JSON.stringify === "function"
) {
output += JSON.stringify(arg);
} else {
output += arg;
}
output += "</span> ";
}
logger.innerHTML += output + "<br>";
console.old.apply(undefined, arguments);
};
})(document.getElementById("logger"));
// Testing
console.log("Hi!", {a:3, b:6}, 42, true);
console.log("Multiple", "arguments", "here");
console.log(null, undefined);
console.old("Eyy, that's the old and boring one.");
body {background: #333;}
.log-boolean,
.log-undefined {color: magenta;}
.log-object,
.log-string {color: orange;}
.log-number {color: cyan;}
<pre id="logger"></pre>
我进一步添加了一个类来为每个日志着色,这些日志与Chrome控制台中看到的所有参数一样。您还可以通过console.old()
访问旧日志。
以下是上面脚本的压缩版本,供您内嵌使用:
<script>
!function(o){console.old=console.log,console.log=function(){var n,e,t="";for(e=0;e<arguments.length;e++)t+='<span class="log-'+typeof(n=arguments[e])+'">',"object"==typeof n&&"object"==typeof JSON&&"function"==typeof JSON.stringify?t+=JSON.stringify(n):t+=n,t+="</span> ";o.innerHTML+=t+"<br>",console.old.apply(void 0,arguments)}}
(document.body);
</script>
document.body
替换为您想要登录的任何元素。<div id="output"></div>
var output = document.getElementById("output");
output.innerHTML = "hello world";
JSON.stringify
。var myObj = {foo: "bar"};
output.innerHTML = JSON.stringify(myObj);
这篇文章对我帮助很大,经过几次迭代,以下就是我们现在使用的方法。
这个想法是将日志消息和错误信息发布到HTML中,例如如果您需要调试JS并且无法访问控制台。
您需要将 'console.log' 更改为 'logThis',因为不建议更改本机功能。
您将获得:
function logThis(message) {
// if we pass an Error object, message.stack will have all the details, otherwise give us a string
if (typeof message === 'object') {
message = message.stack || objToString(message);
}
console.log(message);
// create the message line with current time
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ':' + today.getMinutes() + ':' + today.getSeconds();
var dateTime = date + ' ' + time + ' ';
//insert line
document.getElementById('logger').insertAdjacentHTML('afterbegin', dateTime + message + '<br>');
}
function objToString(obj) {
var str = 'Object: ';
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
str += p + '::' + obj[p] + ',\n';
}
}
return str;
}
const object1 = {
a: 'somestring',
b: 42,
c: false
};
logThis(object1)
logThis('And all the roads we have to walk are winding, And all the lights that lead us there are blinding')
#logWindow {
overflow: auto;
position: absolute;
width: 90%;
height: 90%;
top: 5%;
left: 5%;
right: 5%;
bottom: 5%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 20;
}
<div id="logWindow">
<pre id="logger"></pre>
</div>
感谢这个答案,JSON.stringify()对此无效。
请复制并粘贴以下代码
console.log = (...e) => {for(var i=0;i<e.length;i++){document.getElementById("log").innerHTML += (typeof(e[i]) == "object" ? JSON.stringify(e[i]):e[i]) + "<br />"}};
console.log = (...e) => {for(var i=0;i<e.length;i++){document.getElementById("log").innerHTML += (typeof(e[i]) == "object" ? JSON.stringify(e[i]):e[i]) + "<br />"}};
console.log("Hello world",{objectWorks:true},["Array works?","Yes"])
<div id="log"></div>
var CustomNameOfLog = (...e) => {for(var i=0;i<e.length;i++){document.getElementById("log").innerHTML += (typeof(e[i]) == "object" ? JSON.stringify(e[i]):e[i]) + "<br />"}};
var customNameOfLog = (...e) => {for(var i=0;i<e.length;i++){document.getElementById("log").innerHTML += (typeof(e[i]) == "object" ? JSON.stringify(e[i]):e[i]) + "<br />"}};
customNameOfLog("Hello world",{objectWorks:true},["Array works?","Yes"])
<div id="log"></div>
console.log
和document.write
都是副作用,它们不返回任何值。 - elclanrs6
,对吗?你可以使用document.write(5+1)
来评估这个表达式。 - elclanrs