Skip to content

Commit

Permalink
fix(docs): Incorrect documentation for EventLogger, fixes #432
Browse files Browse the repository at this point in the history
  • Loading branch information
megahertz committed Jul 28, 2024
1 parent e4dd9bf commit 12a9f75
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
30 changes: 14 additions & 16 deletions docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,43 @@ Sometimes it's helpful to save critical electron events to the log file.

`log.eventLogger.startLogging(options?)`;

By default, it save the following events:
By default, it saves the following events:
- `certificate-error`, `child-process-gone`, `render-process-gone` of `app`
- `crashed`, `gpu-process-crashed` of `webContents`
- `did-fail-load`, `did-fail-provisional-load`, `plugin-crashed`,
`preload-error` of every WebContents. You can switch any event on/off.

#### `log.eventLogger.startLogging(options?)`
## Methods

Start saving logs. See options argument description below.
#### `log.eventLogger.startLogging(options?: EventLoggerOptions)`

Start saving event logs.

#### `log.eventLogger.stopLogging()`

Stop saving logs.

#### `log.eventLogger.setOptions()`
#### `log.eventLogger.setOptions(options: EventLoggerOptions)`

Set logging options.

#### `log.eventLogger.format` {string | Function}
## Options

#### `log.eventLogger.format` {string | (input: EventFormatterInput) => any[]}

Default: `'{eventSource}#{eventName}:'`

Custom format example:
Custom format function example:

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

#### `log.eventLogger.formatters` {object}

A set of function which formats a specific event.
A set of functions which formats a specific event.

```js
log.eventLogger.formatters.webContents['console-message'] = ({
Expand All @@ -58,7 +61,7 @@ log.eventLogger.formatters.webContents['console-message'] = ({
#### `log.eventLogger.events` {object}
Allow to switch specific events on/off easily
Allow switching specific events on/off easily
Default:
Expand Down Expand Up @@ -90,8 +93,3 @@ Default: `'warn'`
Which log scope is used for logging
Default: `''`
### Options
Options is just an object which may contain some eventLogger properties
(`events`, `level`, `logger`, `format`, `formatters`, `scope`)
31 changes: 10 additions & 21 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,40 +509,29 @@ declare namespace Logger {

type EventSource = 'app' | 'webContents';

interface EventFormatterInput {
args: unknown[];
event: object;
eventName: string;
eventSource: string;
}

interface EventLoggerOptions {
/**
* String template or function which prepares event data for logging
*/
format?:
| string
| ((
args: {
eventName: string;
eventSource: EventSource;
handlerArgs: unknown[];
}
) => unknown[]);
format?: string | ((input: EventFormatterInput) => unknown[]);

/**
* Formatter callbacks for a specific event
*/
formatters?: Record<
EventSource,
Record<
string,
(
args: {
args: unknown[];
event: object;
eventName: string;
eventSource: string;
}
) => unknown
>
Record<string, (input: EventFormatterInput) => object | unknown[]>
>;

/**
* Allow to switch specific events on/off easily
* Allow switching specific events on/off easily
*/
events?: Record<EventSource, Record<string, boolean>>;

Expand Down

0 comments on commit 12a9f75

Please sign in to comment.