Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(refactor): using writer.writable instead of alive for checking #1144

Merged
merged 1 commit into from Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 24 additions & 24 deletions lib/appenders/dateFile.js
Expand Up @@ -3,13 +3,29 @@ const os = require('os');

const eol = os.EOL;

function openTheStream(filename, pattern, options) {
const stream = new streams.DateRollingFileStream(
filename,
pattern,
options
);
stream.on('error', (err) => {
console.error('log4js.dateFileAppender - Writing to file %s, error happened ', filename, err); //eslint-disable-line
});
stream.on("drain", () => {
process.emit("log4js:pause", false);
});
return stream;
}

/**
* File appender that rolls files according to a date pattern.
* @filename base filename.
* @pattern the format that will be added to the end of filename when rolling,
* @param filename base filename.
* @param pattern the format that will be added to the end of filename when rolling,
* also used to check when to roll files - defaults to '.yyyy-MM-dd'
* @layout layout function for log messages - defaults to basicLayout
* @timezoneOffset optional timezone offset in minutes - defaults to system local
* @param layout layout function for log messages - defaults to basicLayout
* @param options - options to be passed to the underlying stream
* @param timezoneOffset - optional timezone offset in minutes (default system local)
*/
function appender(
filename,
Expand All @@ -22,35 +38,19 @@ function appender(
// options should work for dateFile as well.
options.maxSize = options.maxLogSize;

let alive = true;

const logFile = new streams.DateRollingFileStream(
filename,
pattern,
options
);

logFile.on('error', (err) => {
alive = false;
console.error('log4js.dateFileAppender - Writing to file %s, error happened ', filename, err); //eslint-disable-line
});
logFile.on("drain", () => {
process.emit("log4js:pause", false);
});
const writer = openTheStream(filename, pattern, options);

const app = function (logEvent) {
if (!alive) {
if (!writer.writable) {
return;
}
if (!logFile.write(layout(logEvent, timezoneOffset) + eol, "utf8")) {
if (!writer.write(layout(logEvent, timezoneOffset) + eol, "utf8")) {
process.emit("log4js:pause", true);
}
};

app.shutdown = function (complete) {
logFile.write('', 'utf-8', () => {
logFile.end(complete);
});
writer.end('', 'utf-8', complete);
};

return app;
Expand Down
17 changes: 7 additions & 10 deletions lib/appenders/file.js
Expand Up @@ -20,6 +20,9 @@ function openTheStream(file, fileSize, numFiles, options) {
numFiles,
options
);
stream.on('error', (err) => {
console.error('log4js.fileAppender - Writing to file %s, error happened ', file, err); //eslint-disable-line
});
stream.on('drain', () => {
process.emit("log4js:pause", false);
});
Expand Down Expand Up @@ -55,25 +58,19 @@ function fileAppender(file, layout, logSize, numBackups, options, timezoneOffset
timezoneOffset, ')'
);

let alive = true;

let writer = openTheStream(file, logSize, numBackups, options);
writer.on('error', (err) => {
alive = false;
console.error('log4js.fileAppender - Writing to file %s, error happened ', file, err); //eslint-disable-line
});

const app = function (loggingEvent) {
if (!alive) {
if (!writer.writable) {
return;
}
if (options.removeColor === true) {
// eslint-disable-next-line no-control-regex
const regex = /\x1b[[0-9;]*m/g;
loggingEvent.data = loggingEvent.data.map(d => {
if (typeof d === 'string') return d.replace(regex, '')
return d
})
if (typeof d === 'string') return d.replace(regex, '');
return d;
});
}
if (!writer.write(layout(loggingEvent, timezoneOffset) + eol, "utf8")) {
process.emit('log4js:pause', true);
Expand Down