Skip to content

Commit

Permalink
fix(file): Prevent creating default log dir, closes #368
Browse files Browse the repository at this point in the history
  • Loading branch information
megahertz committed Sep 25, 2023
1 parent 97f0174 commit ea7f7ce
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-log",
"version": "5.0.0-beta.29",
"version": "5.0.0-rc.1",
"description": "Just a simple logging module for your Electron application",
"main": "src/index.js",
"browser": "src/renderer/index.js",
Expand Down
23 changes: 17 additions & 6 deletions src/main/transports/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,21 @@ function fileTransportFactory(logger, registry = globalRegistry) {
return;
}

pathVariables = variables.getPathVariables(process.platform);
// Make a shallow copy of pathVariables to keep getters intact
pathVariables = Object.create(
Object.prototype,
{
...Object.getOwnPropertyDescriptors(
variables.getPathVariables(process.platform),
),
fileName: {
get() {
return transport.fileName;
},
enumerable: true,
},
},
);

if (typeof transport.archiveLog === 'function') {
transport.archiveLogFn = transport.archiveLog;
Expand All @@ -100,9 +114,7 @@ function fileTransportFactory(logger, registry = globalRegistry) {
function getFile(msg) {
initializeOnFirstAccess();

const vars = { ...pathVariables, fileName: transport.fileName };

const filePath = transport.resolvePathFn(vars, msg);
const filePath = transport.resolvePathFn(pathVariables, msg);
return registry.provide({
filePath,
writeAsync: !transport.sync,
Expand All @@ -111,8 +123,7 @@ function fileTransportFactory(logger, registry = globalRegistry) {
}

function readAllLogs({ fileFilter = (f) => f.endsWith('.log') } = {}) {
const vars = { ...pathVariables, fileName: transport.fileName };
const logsPath = path.dirname(transport.resolvePathFn(vars));
const logsPath = path.dirname(transport.resolvePathFn(pathVariables));

return fs.readdirSync(logsPath)
.map((fileName) => path.join(logsPath, fileName))
Expand Down
4 changes: 3 additions & 1 deletion src/main/transports/file/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ function getPathVariables(platform) {
appData: getAppData(platform),
appName,
appVersion,
electronDefaultDir: electronApi.getPath('logs'),
get electronDefaultDir() {
return electronApi.getPath('logs');
},
home: getHome(),
libraryDefaultDir: getLibraryDefaultDir(platform, appName),
libraryTemplate: getLibraryTemplate(platform),
Expand Down

0 comments on commit ea7f7ce

Please sign in to comment.