megahertz / electron-log

Simple logging module Electron/Node.js/NW.js application. No dependencies. No complicated configuration.
MIT License
1.27k stars 124 forks source link

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

Closed FlyBooks closed 3 months ago

FlyBooks commented 3 months ago

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

megahertz commented 3 months ago

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

FlyBooks commented 3 months ago

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?

megahertz commented 3 months ago

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