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

Logging from renderer to file does not work with custom resolvePathFn #404

Closed TimoKunze closed 4 months ago

TimoKunze commented 4 months ago

I'm using electron-log 5.1.1 with electron 29.0.1 and trying to log to a custom file. It works for the main process, but not for the renderer. This is my code:

// main.ts
import log from "electron-log/main";
log.initialize();
log.transports.file.level = "info";
log.transports.file.resolvePathFn = (variables, message) => {
  // simplified
  return "mylog.log";
}
log.warn("Hello from main");
// create BrowserWindow with contextIsolation=false and nodeIntegration=true

// renderer.ts
import log from "electron-log/renderer";
log.warn("Hello from renderer");

In the log file I see only "Hello from main". If I don't override resolvePathFn, I see both log entries in the standard log file.

What am I doing wrong? Timo

TimoKunze commented 4 months ago

Ok, forget about this: "If I don't override resolvePathFn, I see both log entries in the standard log file." I have seen it in an older logfile, but cannot reproduce it anymore. So actually I never get the entries from the renderer process in any log file.

Meanwhile I've analyzed the code of electron-log and I think the reason for this issue is that log entries that come from the renderer, get a logger instance for which file transport is disabled. This would explain why I see the log entries from renderer in the system's console window.

megahertz commented 4 months ago

Do you use multiple logger instances? If so, you should specify logId both in the main and renderer processes when creating an instance.

TimoKunze commented 4 months ago

Well, I don't explicitly create instances. Instead I use the default instances that I get with import log from "electron-log/main"; and import log from "electron-log/renderer";

Is this not possible if logging to a file?

megahertz commented 4 months ago

If you use only default instances it should work fine. I tried to reproduce the issue using your code, but it works fine for me. Could you make a demo project where this issue is reproduced?

TimoKunze commented 4 months ago

I solved it. I did configure both loggers in a function in one module. This module therefore did import "electron-log/main" and "electron-log/renderer". Although there was no mix of which process type did access which logger, importing both loggers in the same module already was enough to cause problems.

Thank you for your support and also for this great project!