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 Events (eventLogger) Handler Arguments are undefined. #432

Closed melihcoban closed 2 months ago

melihcoban commented 2 months ago

I'm having issues with eventLogger.format functions.

How to access event itself in eventLogger.format function? For an example, in the event of 'render-process-gone', handlerArgs are undefined. I can only access event source or event name but not the reason.

log.eventLogger.format = ({ eventName, eventSource, handlerArgs }) => {
  const [event, ...eventArgs] = handlerArgs;
  return [`${eventSource}#${eventName}:`, JSON.stringify(eventArgs)];
};

This function gives error because handlerArgs is not iterable, and application crashes.

I tried string templates, but eventArgs is not filled here as well.

log.eventLogger.format = '{"eventSource": "{eventSource}", "eventName": "{eventName}", "eventArgs": }';

This is output of the above format. Properties and values is not valid format, does not have double quote around them.

{"timestamp": "2024-07-26T17:13:44.848+03:00", "level": "error", "process": "main", "log":  {"eventSource": "App", "eventName": "render-process-gone", "eventArgs":  {
  reason: 'crashed',
  exitCode: 2,
  webContents: { id: 1, url: 'http://localhost:5173/' }
}  }

This is what I want to achieve

{
    "timestamp": "2024-07-26T17:13:44.848+03:00",
    "level": "error",
    "process": "main",
    "log": {
        "eventSource": "App",
        "eventName": "render-process-gone",
        "eventArgs": {
            "reason": "crashed",
            "exitCode": 2,
            "webContents": {
                "id": 1,
                "url": "http://localhost:5173/"
            }
        }
    }
}

Is it achievable? Thanks for help.

megahertz commented 2 months ago

The documentation is incorrect, but I'm going to update it soon. Use args instead of handlerArgs

melihcoban commented 2 months ago

@megahertz Hi, using ({ args, event, eventName, eventSource }) gives the correct information but there is Type errors while using the function like this.

Property 'args' does not exist on type '{ eventName: string; eventSource: EventSource; handlerArgs: unknown[]; }'.ts(2339), Property 'event' does not exist on type '{ eventName: string; eventSource: EventSource; handlerArgs: unknown[]; }'

megahertz commented 2 months ago

the types will be updated in a few days