Merge pull request #1252 from log4js-node/feat/tilde-expansion

feat: tilde expansion for filename
This commit is contained in:
Lam Wei Li 2022-05-22 16:52:06 +08:00 committed by GitHub
commit f2ddd9fc26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 0 deletions

View File

@ -31,6 +31,10 @@ function fileAppender(file, layout, logSize, numBackups, options, timezoneOffset
throw new Error(`Invalid filename: ${file}`);
} else if (file.endsWith(path.sep)) {
throw new Error(`Filename is a directory: ${file}`);
} else {
// handle ~ expansion: https://github.com/nodejs/node/issues/684
// exclude ~ and ~filename as these can be valid files
file = file.replace(new RegExp(`^~(?=${path.sep}.+)`), os.homedir());
}
file = path.normalize(file);
numBackups = (!numBackups && numBackups !== 0) ? 5 : numBackups;

View File

@ -166,6 +166,10 @@ function fileAppender(file, layout, logSize, numBackups, options, timezoneOffset
throw new Error(`Invalid filename: ${file}`);
} else if (file.endsWith(path.sep)) {
throw new Error(`Filename is a directory: ${file}`);
} else {
// handle ~ expansion: https://github.com/nodejs/node/issues/684
// exclude ~ and ~filename as these can be valid files
file = file.replace(new RegExp(`^~(?=${path.sep}.+)`), os.homedir());
}
file = path.normalize(file);
numBackups = (!numBackups && numBackups !== 0) ? 5 : numBackups;