Reputation: 274
Is there way to handle console.log messages from renderer in main process? Similar to CefDisplayHandler::OnConsoleMessage handler in Cef.
Upvotes: 0
Views: 1860
Reputation: 9140
The ability to intercept console messages generated by the renderer process, in the main process, was implemented in Electron version v1.8.2-beta.3
(as far as I were able to determine).
To be able to handle these messages, one attaches a "console-message" event handler to the webContents
object property of a BrowserWindow
object.
A rather straightforward replication of messages by the main process, could be implemented as follows (win
refers to a BrowserWindow
you want to capture messages for):
const log_level_names = { "-1": "DEBUG", "0": "INFO", "1": "WARN", "2": "ERROR" };
win.webContents.on("console-message", (ev, level, message, line, file) => {
console.log(`${new Date().toUTCString()}\t${log_level_names[level]}\t${message} (${file}:${line})`);
});
Upvotes: 1
Reputation: 4481
You can do this in three ways,
ELECTRON_ENABLE_LOGGING=true
to parse every console.log to your CLIAdd a function to app from the main process
# MAIN
const {app} = require('electron')
app.MySuperCoolLoggingUtility = function (msg) {
console.log(msg)
}
# RENDERER
require('electron').remote.app.MySuperCoolLoggingUtility('hi')
There are also some ways to limit the log level for specific files via --vmodule=
but it is not close to the handler of normal Cef. So you will probably build your own utility function for it.
Upvotes: 1