2

I want to access console.log data programmatically and so I need to implement a console.log which stores its data in a global variable or file.

Can anyone tell me how to do this?

1

1 Answer 1

5

EDIT 2

Do use a library like loglevel

See the answers below to understand how you can implement something like this by yourself.

original answer:

Do something like:

var myLogs = [];
(function () {
  var log = console.log;
  console.log = function () {
    var args = Array.prototype.slice.call(arguments)
    log.apply(this, args );
    myLogs.push(args);
  };
}());

The gloabl variable myLogs will contain your log data.

You may want to adopt the code to your needs. Maybe you want to make one string out of the args array.

EDIT

If you want to have console.log, console.error, console.warn, console.info and store the log level:

var logData = [];
(function () {
    var log = console.log,
        error = console.error,
        warn = console.warn,
        info = console.info;

    console.log = function () {
        var args = Array.prototype.slice.call(arguments)
        log.apply(this, args );
        logData.push({level: "log", arguments: args});
    };
    console.error = function () {
        var args = Array.prototype.slice.call(arguments)
        error.apply(this, args );
        logData.push({level: "error", arguments: args});
    };
    console.warn = function () {
        var args = Array.prototype.slice.call(arguments)
        warn.apply(this, args );
        logData.push({level: "warn", arguments: args});
    };
    console.info = function () {
        var args = Array.prototype.slice.call(arguments)
        info.apply(this, args );
        logData.push({level: "info", arguments: args});
    };
}());
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks this works. But how do i get error type like info,warning etc. In the arguments object im getting only error description.
I edited the answer and added an example of how also storing the log level. Be aware that now for every log you have an object with a property called level.
This does not catch all the javascript warnings and error messages which i see in the console but not getting saved in the global variable.I want to catch the Error and warning messages which get generated itself without me calling console.log or the other functions.
It should overwritte the console.log function and the others. So who ever calls this functions, the data should end up in the global array. Make sure that you run this piece of code first! And if somebody calls throw new Error("error") then this does not end up in this log. In your question you did only ask about how to collect data from calling console.log.
Question. If I overrite my console.<log level> in this way, would I be able to capture also node modules, libraries logs? Sometime errors happens on the side of the library. It would be important to store them as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.