8000 fix(fileSync): options not used · wxqGitHub/log4js-node@dc2079e · GitHub
[go: up one dir, main page]

Skip to content

Commit dc2079e

Browse files
author
Rami Cohen
committed
fix(fileSync): options not used
1 parent 94d58b4 commit dc2079e

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

lib/appenders/fileSync.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ const os = require('os');
77

88
const eol = os.EOL || '\n';
99

10+
function touchFile(file, options) {
11+
// if the file exists, nothing to do
12+
if (fs.existsSync(file)) {
13+
return;
14+
}
15+
16+
// touch the file to apply flags (like w to truncate the file)
17+
const id = fs.openSync(file, options.flags, options.mode);
18+
fs.closeSync(id);
19+
}
20+
1021
class RollingFileSync {
1122
constructor(filename, size, backups, options) {
1223
debug('In RollingFileStream');
@@ -22,16 +33,17 @@ class RollingFileSync {
2233
this.filename = filename;
2334
this.size = size;
2435
this.backups = backups || 1;
25-
this.options = options || { encoding: 'utf8', mode: parseInt('0644', 8), flags: 'a' }; // eslint-disable-line
36+
this.options = options;
2637
this.currentSize = 0;
2738

2839
function currentFileSize(file) {
2940
let fileSize = 0;
41+
3042
try {
3143
fileSize = fs.statSync(file).size;
3244
} catch (e) {
3345
// file does not exist
34-
fs.appendFileSync(filename, '');
46+
touchFile(file, options);
3547
}
3648
return fileSize;
3749
}
@@ -130,8 +142,9 @@ class RollingFileSync {
130142
* has been reached (default 5)
131143
* @param timezoneOffset - optional timezone offset in minutes
132144
* (default system local)
145+
* @param options - passed as is to fs options
133146
*/
134-
function fileAppender(file, layout, logSize, numBackups, timezoneOffset) {
147+
function fileAppender(file, layout, logSize, numBackups, timezoneOffset, options) {
135148
debug('fileSync appender created');
136149
file = path.normalize(file);
137150
numBackups = numBackups === undefined ? 5 : numBackups;
@@ -145,14 +158,13 @@ function fileAppender(file, layout, logSize, numBackups, timezoneOffset) {
145158
stream = new RollingFileSync(
146159
filePath,
147160
fileSize,
148-
numFiles
161+
numFiles,
162+
options
149163
);
150164
} else {
151165
stream = (((f) => {
152-
// create file if it doesn't exist
153-
if (!fs.existsSync(f)) {
154-
fs.appendFileSync(f, '');
155-
}
166+
// touch the file to apply flags (like w to truncate the file)
167+
touchFile(f, options);
156168

157169
return {
158170
write(data) {
@@ -178,12 +190,19 @@ function configure(config, layouts) {
178190
layout = layouts.layout(config.layout.type, config.layout);
179191
}
180192

193+
const options = {
194+
flags: config.flags || 'a',
195+
encoding: config.encoding || 'utf8',
196+
mode: config.mode || 0o644
197+
};
198+
181199
return fileAppender(
182200
config.filename,
183201
layout,
184202
config.maxLogSize,
185203
config.backups,
186-
config.timezoneOffset
204+
config.timezoneOffset,
205+
options
187206
);
188207
}
189208

test/tap/fileSyncAppender-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,31 @@ test('log4js fileSyncAppender', (batch) => {
158158
});
159159
});
160160

161+
batch.test('test options', (t) => {
162+
// using non-standard options
163+
log4js.configure({
164+
appenders: {
165+
sync: {
166+
type: 'fileSync',
167+
filename: 'tmp-options-tests.log',
168+
layout: { type: 'messagePassThrough' },
169+
flags: 'w',
170+
encoding: 'ascii',
171+
mode: 0o666
172+
}
173+
},
174+
categories: {
175+
default: { appenders: ['sync'], level: 'info' }
176+
}
177+
});
178+
const logger = log4js.getLogger();
179+
logger.warn('log message');
180+
181+
fs.readFile('tmp-options-tests.log', 'ascii', (err, contents) => {
182+
t.include(contents, `log message${EOL}`);
183+
t.end();
184+
});
185+
});
186+
161187
batch.end();
162188
});

0 commit comments

Comments
 (0)
0