mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
fix(fileSync): options not used
This commit is contained in:
parent
94d58b4a20
commit
dc2079e4c1
@ -7,6 +7,17 @@ const os = require('os');
|
||||
|
||||
const eol = os.EOL || '\n';
|
||||
|
||||
function touchFile(file, options) {
|
||||
// if the file exists, nothing to do
|
||||
if (fs.existsSync(file)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// touch the file to apply flags (like w to truncate the file)
|
||||
const id = fs.openSync(file, options.flags, options.mode);
|
||||
fs.closeSync(id);
|
||||
}
|
||||
|
||||
class RollingFileSync {
|
||||
constructor(filename, size, backups, options) {
|
||||
debug('In RollingFileStream');
|
||||
@ -22,16 +33,17 @@ class RollingFileSync {
|
||||
this.filename = filename;
|
||||
this.size = size;
|
||||
this.backups = backups || 1;
|
||||
this.options = options || { encoding: 'utf8', mode: parseInt('0644', 8), flags: 'a' }; // eslint-disable-line
|
||||
this.options = options;
|
||||
this.currentSize = 0;
|
||||
|
||||
function currentFileSize(file) {
|
||||
let fileSize = 0;
|
||||
|
||||
try {
|
||||
fileSize = fs.statSync(file).size;
|
||||
} catch (e) {
|
||||
// file does not exist
|
||||
fs.appendFileSync(filename, '');
|
||||
touchFile(file, options);
|
||||
}
|
||||
return fileSize;
|
||||
}
|
||||
@ -130,8 +142,9 @@ class RollingFileSync {
|
||||
* has been reached (default 5)
|
||||
* @param timezoneOffset - optional timezone offset in minutes
|
||||
* (default system local)
|
||||
* @param options - passed as is to fs options
|
||||
*/
|
||||
function fileAppender(file, layout, logSize, numBackups, timezoneOffset) {
|
||||
function fileAppender(file, layout, logSize, numBackups, timezoneOffset, options) {
|
||||
debug('fileSync appender created');
|
||||
file = path.normalize(file);
|
||||
numBackups = numBackups === undefined ? 5 : numBackups;
|
||||
@ -145,14 +158,13 @@ function fileAppender(file, layout, logSize, numBackups, timezoneOffset) {
|
||||
stream = new RollingFileSync(
|
||||
filePath,
|
||||
fileSize,
|
||||
numFiles
|
||||
numFiles,
|
||||
options
|
||||
);
|
||||
} else {
|
||||
stream = (((f) => {
|
||||
// create file if it doesn't exist
|
||||
if (!fs.existsSync(f)) {
|
||||
fs.appendFileSync(f, '');
|
||||
}
|
||||
// touch the file to apply flags (like w to truncate the file)
|
||||
touchFile(f, options);
|
||||
|
||||
return {
|
||||
write(data) {
|
||||
@ -178,12 +190,19 @@ function configure(config, layouts) {
|
||||
layout = layouts.layout(config.layout.type, config.layout);
|
||||
}
|
||||
|
||||
const options = {
|
||||
flags: config.flags || 'a',
|
||||
encoding: config.encoding || 'utf8',
|
||||
mode: config.mode || 0o644
|
||||
};
|
||||
|
||||
return fileAppender(
|
||||
config.filename,
|
||||
layout,
|
||||
config.maxLogSize,
|
||||
config.backups,
|
||||
config.timezoneOffset
|
||||
config.timezoneOffset,
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -158,5 +158,31 @@ test('log4js fileSyncAppender', (batch) => {
|
||||
});
|
||||
});
|
||||
|
||||
batch.test('test options', (t) => {
|
||||
// using non-standard options
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
sync: {
|
||||
type: 'fileSync',
|
||||
filename: 'tmp-options-tests.log',
|
||||
layout: { type: 'messagePassThrough' },
|
||||
flags: 'w',
|
||||
encoding: 'ascii',
|
||||
mode: 0o666
|
||||
}
|
||||
},
|
||||
categories: {
|
||||
default: { appenders: ['sync'], level: 'info' }
|
||||
}
|
||||
});
|
||||
const logger = log4js.getLogger();
|
||||
logger.warn('log message');
|
||||
|
||||
fs.readFile('tmp-options-tests.log', 'ascii', (err, contents) => {
|
||||
t.include(contents, `log message${EOL}`);
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
batch.end();
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user