Home  >  Article  >  Web Front-end  >  Method to capture console.log() output in JS

Method to capture console.log() output in JS

高洛峰
高洛峰Original
2017-02-03 14:46:063680browse

The example in this article describes the method of capturing console.log() output in JS. Share it with everyone for your reference. The specific analysis is as follows:

We know that console.log() can output information to the debugger for developers to view. But what if we want to get the output of console.log() in JS? In fact, it is not difficult. Just save the original console.log first and then replace it with another implementation. The code is as follows:

var lastLog;
console.oldLog = console.log;
console.log = function(str) {
  console.oldLog(str);
  lastLog = str;
}
console.log("Hello, Neo");
document.write(lastLog);

At this time, "Hello, Neo" is saved in lastLog.

I hope this article will be helpful to everyone’s JavaScript programming design.

For more articles related to methods of capturing console.log() output in JS, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn