Vishal
Vishal

Reputation: 3296

Hook to console.log in electron

I'm looking to hook to console.log in electron.
I tried following the steps as mentioned here: https://stackoverflow.com/a/9624028/2091948
While the above works well in node.js, this same approach doesn't work in electron (which in-turn is node.js based)

How can I achieve this in electron?
I need to capture all calls to console.log, and console.error, so that I can use the module electron-log(https://www.npmjs.com/package/electron-log) to hook to all console.log, and console.error calls.

Upvotes: 0

Views: 4058

Answers (2)

yoel halb
yoel halb

Reputation: 12711

You can hook up to the console-message event of the WebContent object (available from the webContent property on BrowserView and BrowserWindow).

Note that it only provides it as a string so if you are loggign an object it will be received as [object Object]

For more info see the docs

Upvotes: 0

Alexander Leithner
Alexander Leithner

Reputation: 3432

I think this could be done just via overriding console.log or console.error like this:

var log = require("electron-log");

console.log = function (message) {
  log.info(message);
}
console.error = function (message) {
  log.error(message);
}

Alternatively, if you like to preserve the old functions and/or store all messages in an array for later use, you could use the following functions:

var log = require("electron-log"), msgInfo = [], msgErr = [];

// Preserve the old, built-in functions
console.log_old = console.log;
console.error_old = console.error;

console.log = function (message) {
  msgInfo.push(message);
  log.info(message);
}

console.error = function (message) {
  msgErr.push(message);
  log.error(message);
}

You can use these functions in a browser window, for the main process, the code you already mentioned should work with some adaptions:

// https://stackoverflow.com/a/9624028, written by "kevin"
var logs = [],

hook_stream = function(_stream, fn) {
  // Reference default write method
  var old_write = _stream.write;
  // _stream now write with our shiny function
  _stream.write = fn;

  return function() {
    // reset to the default write method
    _stream.write = old_write;
  };
},

// hook up standard output
unhook_stdout = hook_stream(process.stdout, function(string, encoding, fd) {
  logs.push(string);
}),

// require electron-log
log = require("electron-log");

// goes to our custom write method
console.log('foo');
console.log('bar');

unhook_stdout();

console.log('Not hooked anymore.');

// Now do what you want with logs stored by the hook
logs.forEach(function(_log) {
  // Either log the contents of the message array...
  //console.log('logged: ' + _log);

  // ...or use electron-log to actually log them.
  log.info(_log);
});

This code will work in the main process, because there NodeJS creates the stream to stdout. In the renderer process you will have to use the functions I mentioned above, because Electron doesn't create any stream but rather lets Chromium log all messages to it's Developer Console.

Upvotes: 3

Related Questions