[electron] Electron 앱에서 console.log () 사용

Electron 앱의 콘솔에 데이터 또는 메시지를 어떻게 기록 할 수 있습니까?

이 정말 기본적인 hello world는 기본적으로 개발 도구를 엽니 다 console.log('hi'). Electron에 대한 대안이 있습니까?

main.js

var app = require('app');
var BrowserWindow = require('browser-window');

require('crash-reporter').start();

var mainWindow = null;

app.on('window-all-closed', function() {
  // Mac OS X - close is done explicitly with Cmd + Q, not just closing windows
  if (process.platform != 'darwin') {
    app.quit();
  }
});

app.on('ready', function(){
  mainWindow = new BrowserWindow({ width: 800, height: 600});

  mainWindow.loadUrl('file://' + __dirname + '/index.html');

  mainWindow.openDevTools();

  mainWindow.on('closed', function(){
    mainWindow = null;
  });
});



답변

console.log 작동하지만 로그 위치는 기본 프로세스에서 호출하는지 렌더러 프로세스에서 호출하는지에 따라 다릅니다.

렌더러 프로세스 (예 : index.html파일에 포함 된 JavaScript)에서 호출 하면 개발 도구 창에 기록됩니다.

메인 프로세스 (예 :)에서 호출하면 main.jsNode에서와 같은 방식으로 작동합니다. 터미널 창에 기록됩니다. 터미널에서 Electron 프로세스를 시작 하는 경우 메인 프로세스 electron .console.log호출을 볼 수 있습니다.


답변

Windows에서 환경 변수를 추가 할 수도 있습니다.

ELECTRON_ENABLE_LOGGING=1

그러면 터미널에 콘솔 메시지가 출력됩니다.


답변

렌더러 프로세스 내부에서 콘솔에 로깅하는 또 다른 방법이 있습니다. 이것이 Electron이라면 Node의 네이티브 모듈에 액세스 할 수 있습니다. 여기에는 console모듈 이 포함됩니다 .

var nodeConsole = require('console');
var myConsole = new nodeConsole.Console(process.stdout, process.stderr);
myConsole.log('Hello World!');

이 코드가 렌더러 프로세스 내부에서 실행되면 Hello World!Electron을 실행 한 터미널로 이동하게됩니다.

모듈 에 대한 추가 문서는 https://nodejs.org/api/console.html 을 참조 하십시오console .


답변

또 다른 가능성은 remote.getGlobal(name)다음을 사용하여 기본 프로세스 콘솔에 액세스하는 것입니다 .

const con = require('electron').remote.getGlobal('console')
con.log('This will be output to the main process console.')


답변

M. Damian의 답변에 추가하여 모든 렌더러에서 기본 프로세스의 콘솔에 액세스 할 수 있도록 설정하는 방법은 다음과 같습니다.

기본 앱에서 다음을 추가합니다.

const electron = require('electron');
const app = electron.app;
const console = require('console');
...
app.console = new console.Console(process.stdout, process.stderr);

모든 렌더러에서 다음을 추가 할 수 있습니다.

const remote = require('electron').remote;
const app = remote.app;
...
app.console.log('This will output to the main process console.');


답변

process.stdout.write('your output to command prompt console or node js ')


답변

npm 패키지 electron-log https://www.npmjs.com/package/electron-log를 사용할 수 있습니다.

네이티브 OS 로그에 오류, 경고, 정보, 상세 정보, 디버그, 어리석은 출력을 기록합니다.

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

log.info('Hello, log');
log.error('Damn it, an error');