8000 Added test for `removeColor` option · xaviernoder/log4js-node@0fda5c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0fda5c5

Browse files
committed
Added test for removeColor option
Signed-off-by: Cocoa <0xbbc@0xbbc.com>
1 parent e26d35a commit 0fda5c5

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/appenders/file.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ function fileAppender(file, layout, logSize, numBackups, options, timezoneOffset
5454

5555
const app = function (loggingEvent) {
5656
if (options.removeColor === true) {
57-
const regex = new RegExp("\x1b[[0-9;]*m", "g"); // eslint-disable-line no-control-regex
57+
// eslint-disable-next-line no-control-regex
58+
const regex = new RegExp("\x1b[[0-9;]*m", "g");
5859
for (let i = 0; i < loggingEvent.data.length; i += 1) {
5960
let d = loggingEvent.data[i];
6061
d = d.replace(regex, '');

test/tap/fileAppender-test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,46 @@ test("log4js fileAppender", batch => {
333333
});
334334
t.end();
335335
});
336+
337+
batch.test("with removeColor fileAppender settings", async t => {
338+
const testFilePlain = path.join(__dirname, "fa-removeColor-test.log");
339+
const testFileAsIs = path.join(__dirname, "fa-asIs-test.log");
340+
const logger = log4js.getLogger("default-settings");
341+
await removeFile(testFilePlain);
342+
await removeFile(testFileAsIs);
343+
344+
t.tearDown(async () => {
345+
await new Promise(resolve => log4js.shutdown(resolve));
346+
await removeFile(testFilePlain);
347+
await removeFile(testFileAsIs);
348+
});
349+
350+
log4js.configure({
351+
appenders: {
352+
plainFile: { type: "file", filename: testFilePlain, removeColor: true },
353+
asIsFile: { type: "file", filename: testFilePlain, removeColor: false }
354+
},
355+
categories: { default: { appenders: ["plainFile", "asIsFile"], level: "debug" } }
356+
});
357+
358+
logger.info("This should be in the file. \x1b[33mColor\x1b[0m \x1b[93;41mshould\x1b[0m be \x1b[38;5;8mplain\x1b[0m.");
359+
360+
await sleep(100);
361+
let fileContents = await fs.readFile(testFilePlain, "utf8");
362+
t.include(fileContents, `This should be in the file. Color should be plain.${EOL}`);
363+
t.match(
364+
fileContents,
365+
/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
366+
);
367+
368+
fileContents = await fs.readFile(testFileAsIs, "utf8");
369+
t.include(fileContents, `This should be in the file. \x1b[33mColor\x1b[0m \x1b[93;41mshould\x1b[0m be \x1b[38;5;8mplain\x1b[0m.${EOL}`);
370+
t.match(
371+
fileContents,
372+
/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
373+
);
374+
t.end();
375+
});
336376

337377
batch.end();
338378
});

0 commit comments

Comments
 (0)
0