megahertz / electron-log

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

"electron-log: logger isn't initialized in the main process" error in browser console #428

Closed vibhorgupta-gh closed 2 months ago

vibhorgupta-gh commented 2 months ago

I'm running my electron app (v31.1.0) and electron-log (v5.1.2) in it, with the following config:

main.ts

log.initialize()

log.transports.file.format = '[{h}:{i}:{s}] - [{d}-{m}-{y}] - [{processType}] -> {text}'
log.transports.console.format = '[{h}:{i}:{s}] - [{d}-{m}-{y}] - [{processType}] -> {text}'

const mainLogger = log.scope('main')
console.log = mainLogger.log
console.debug = mainLogger.debug
console.info = mainLogger.info
console.warn = mainLogger.warn
console.error = mainLogger.error

log.transports.file.resolvePathFn = (vars, message) => {
    if (message.scope === 'main') return path.resolve(vars.libraryDefaultDir, 'main.log')
    else return path.resolve(vars.libraryDefaultDir, 'render.log')
}

render.ts

import log from 'electron-log/renderer';

const renderLogger = log.scope('render')

console.log = renderLogger.log
console.debug = renderLogger.debug
console.warn = renderLogger.warn
console.error = renderLogger.error

this writes my main thread logs to my terminal, also writes my render thread logs to browser console, but it doesn't:

  1. Write render logs to terminal
  2. Write render logs to render.log file
Screenshot 2024-07-11 at 4 52 19 PM

What am i doing wrong?

megahertz commented 2 months ago

These fragments look fine. Could you share a whole project that reproduces the issue? It may be related to a bundler.

vibhorgupta-gh commented 2 months ago

@megahertz update: I was able to run this by upgrading my node-polyfill-webpack-plugin to v4.0.0, but a separate issue has popped up now -> In order for the above error to not come up, i always need to do a yarn && yarn dev to run my app instead of a simple yarn dev, otherwise the above mentioned error shows up - this is reproducable 100% of the times in my project. Any ideas what may be causing this? This may be unrelated, but on every yarn dev, the undefinedelectron-log-preload.js file is created in my project root

megahertz commented 2 months ago

This library tries to support different bundler environments, but some environments still make it impossible to initialize the logger correctly in a renderer process. If you use any preload script, you may try to import 'electron-log/preload' instead of running log.initialize(). Or you can use another initialisation strategy https://github.com/megahertz/electron-log/blob/master/docs/initialize.md

vibhorgupta-gh commented 2 months ago
  1. A simple import 'electron-log/preload' will suffice?
  2. This import is to placed in main.ts or preload.ts?
  3. How is it different from log.initialize({ preload: true })?
megahertz commented 2 months ago

Yes, you can add this import statement to preload.js. It's an alternative way to do the same as the log.initialize() statement does.

vibhorgupta-gh commented 2 months ago

This seemed to work, thanks a lot for the help! Will reach out to you if I face any further issues.