Skip to content

Commit

Permalink
fix: Add workaround when appName can't be read, fixes #418
Browse files Browse the repository at this point in the history
  • Loading branch information
megahertz committed May 25, 2024
1 parent 957406d commit 500ccab
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/transports/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,8 @@ Reads content of all log files.
Be careful, if you use multiple log directories through overriding resolvePath,
it won't return all the files.

#### `setAppName(appName: string)`

Overrides appName used for resolving the log path

<!-- spech-dictionary whether -->
6 changes: 6 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ declare namespace Logger {
*/
resolvePathFn: (variables: PathVariables, message?: LogMessage) => string;

/**
* Override appName used for resolving log path
* @param appName
*/
setAppName(appName: string): void;

/**
* Whether to write a log file synchronously. Default to true
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/ElectronExternalApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class ElectronExternalApi extends NodeExternalApi {
getAppName() {
let appName;
try {
appName = this.electron.app?.name || this.electron.app?.getName();
appName = this.appName
|| this.electron.app?.name
|| this.electron.app?.getName();
} catch {
// fallback to default value below
}
Expand Down
16 changes: 15 additions & 1 deletion src/node/NodeExternalApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ class NodeExternalApi {
}

getAppName() {
return this.appName || this.getAppPackageJson()?.name;
const appName = this.appName || this.getAppPackageJson()?.name;
if (!appName) {
throw new Error(
'electron-log can\'t determine the app name. It tried these methods:\n'
+ '1. Use `electron.app.name`\n'
+ '2. Use productName or name from the nearest package.json`\n'
+ 'You can also set it through log.transports.file.setAppName()',
);
}

return appName;
}

/**
Expand Down Expand Up @@ -181,6 +191,10 @@ class NodeExternalApi {
});
}

setAppName(appName) {
this.appName = appName;
}

setPlatform(platform) {
this.platform = platform;
}
Expand Down
4 changes: 4 additions & 0 deletions src/node/transports/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ function fileTransportFactory(
resolvePathFn(vars) {
return path.join(vars.libraryDefaultDir, vars.fileName);
},

setAppName(name) {
logger.dependencies.externalApi.setAppName(name);
},
});

function transport(message) {
Expand Down

0 comments on commit 500ccab

Please sign in to comment.