megahertz / electron-log

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

Renderer logs not written to file after updating to v5.2.0 #441

Open tomdixon opened 3 weeks ago

tomdixon commented 3 weeks ago

Hi,

After updating to v5.2.0 from v4.2.2 I'm no longer seeing renderer process logs written to the renderer.log file as before. I've set up the main/remote logging as per the README, and I am getting console logs which indicates that the logs are being sent to the main process via IPC ok. Any ideas what I might be doing wrong?

My setup is as follows:

Main process

import log from 'electron-log/main';

log.initialize();
log.transports.file.level = IS_DEV ? 'debug' : 'info';
log.transports.console.level = IS_DEV ? 'debug' : false;

Renderer process

import log from 'electron-log/renderer';

log.info('Hello world')
megahertz commented 3 weeks ago

Your code looks fine. Can you try to call logging with a delay in a renderer process? E.g. setTimeout(() => log.info('Hello world'), 1000)

tomdixon commented 3 weeks ago

Thanks for the quick reply. Unfortunately that still doesn't lead to the log file being written. I'm just trying it now in a vanilla Electron project. If that also doesn't work I'll link to the code here.

tomdixon commented 3 weeks ago

OK I figured it out. Two things happening at once!

  1. All logs are now being written to main.log. Is this intentional? Is there anyway to configure the file logger to use a different file path for renderer logs?
  2. I tracked down a codepath from my main process that was using a shared util which imported from 'electron-log/renderer'. It seems that if this import exists in the main process at all (even if the log function is never called) then renderer logs stop logging to file. Is there a recommended setup here, as I can imagine this being an easy trap to fall into especially if you use shared utilities between main and renderer code?
megahertz commented 3 weeks ago
  1. You can configure a custom log path through the resolvePath callback. The second argument is a message object containing information about the process where the log was generated.
  2. I didn't think about such a case. You're right, it would be nice to warn about it.
TuKun33 commented 2 weeks ago

Your code looks fine. Can you try to call logging with a delay in a renderer process? E.g. setTimeout(() => log.info('Hello world'), 1000)

Hello, I encountered a similar problem. The logs printed in the main process are not printed in the console of the renderer process. Is it because I am not using bundler, I cannot import "electron-log/renderer" in HTML, or is it another configuration problem?

megahertz commented 2 weeks ago

@TuKun33 When contextIsolation/sandboxing is enabled (by default), and no bundling is used, it's only possible to use a limited set of logger features in a renderer process.