megahertz/electron-log

Error: Attempted to register a second handler for '__ELECTRON_LOG__'

FlyBooks opened this issue · 3 comments

Hello~,
Could you please give me some hints ?
logging-util/index.ts:

import log from 'electron-log/main';
import "./electron-log.d.ts";

  log.initialize({ preload: true });
  log.addLevel("critical");
  log.addLevel("warning", 1);
  log.addLevel("information", 3);
  
console.log(log.levels,'level');
const modbusLog = log.scope("modbus");
  

export { modbusLog };
import { modbusLog } from "../logging-util/index";
ipcMain.handle("modbusLoggingRecord", (_, level, logging) => {
    modbusLog[level](logging);
});

Those code will be executed in file preload.js.

But it always runs failed with error: Error: Attempted to register a second handler for 'ELECTRON_LOG'
at IpcMainImpl.handle (node:electron/js2c/browser_init:2:105646)
at ElectronExternalApi.onIpcInvoke (C:\projects\Hummingbird\dist-electron\main\index.js:523:94)
at Object. (C:\projects\Hummingbird\dist-electron\main\index.js:2060:13)

Thanks a lot in advance!
Eva

code from electron-log/main shouldn't be used in the preload script.

Really appreciated for your hint!
So i just use the ELECTRON_LOG in the ipcRenderer

 electronLoggingRecord: (logContent: string) => { 
      ipcRenderer.send('__ELECTRON_LOG__', {
        data: [logContent],
        level: 'information',
        scope: "user"
      });
    }

main.ts:

app.whenReady().then(() => {
  createWindow();

  // electron-log
  log.initialize();
  log.addLevel("critical");
  log.addLevel("warning", 1);
  log.addLevel("information", 3);
});

It works and is that a recommended way in electron-log?

Yes, that's fine. The only hint is to call the code in main.ts without waiting for the ready event.