mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
1. drop semver related code with no need to detect node version 2. drop license declaration in log4js.js for there is one in the project dir 3. other changes
121 lines
3.0 KiB
JavaScript
121 lines
3.0 KiB
JavaScript
'use strict';
|
|
|
|
const layouts = require('../layouts');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const streams = require('../streams');
|
|
const os = require('os');
|
|
const eol = os.EOL || '\n';
|
|
const openFiles = [];
|
|
|
|
// close open files on process exit.
|
|
process.on('exit', () => {
|
|
openFiles.forEach(file => {
|
|
file.end();
|
|
});
|
|
});
|
|
|
|
/**
|
|
* File Appender writing the logs to a text file. Supports rolling of logs by size.
|
|
*
|
|
* @param file file log messages will be written to
|
|
* @param layout a function that takes a logEvent and returns a string
|
|
* (defaults to basicLayout).
|
|
* @param logSize - the maximum size (in bytes) for a log file,
|
|
* if not provided then logs won't be rotated.
|
|
* @param numBackups - the number of log files to keep after logSize
|
|
* has been reached (default 5)
|
|
* @param compress - flag that controls log file compression
|
|
* @param timezoneOffset - optional timezone offset in minutes (default system local)
|
|
*/
|
|
function fileAppender(file, layout, logSize, numBackups, compress, timezoneOffset) {
|
|
file = path.normalize(file);
|
|
layout = layout || layouts.basicLayout;
|
|
numBackups = numBackups === undefined ? 5 : numBackups;
|
|
// there has to be at least one backup if logSize has been specified
|
|
numBackups = numBackups === 0 ? 1 : numBackups;
|
|
|
|
function openTheStream(filePath, fileSize, numFiles) {
|
|
let stream;
|
|
if (fileSize) {
|
|
stream = new streams.RollingFileStream(
|
|
filePath,
|
|
fileSize,
|
|
numFiles,
|
|
{ compress: compress }
|
|
);
|
|
} else {
|
|
stream = fs.createWriteStream(
|
|
filePath,
|
|
{
|
|
encoding: 'utf8',
|
|
mode: parseInt('0644', 8),
|
|
flags: 'a'
|
|
}
|
|
);
|
|
}
|
|
stream.on('error', err => {
|
|
console.error('log4js.fileAppender - Writing to file %s, error happened ', filePath, err);
|
|
});
|
|
return stream;
|
|
}
|
|
|
|
const logFile = openTheStream(file, logSize, numBackups);
|
|
|
|
// push file to the stack of open handlers
|
|
openFiles.push(logFile);
|
|
|
|
return loggingEvent => {
|
|
logFile.write(layout(loggingEvent, timezoneOffset) + eol, 'utf8');
|
|
};
|
|
}
|
|
|
|
function configure(config, options) {
|
|
let layout;
|
|
if (config.layout) {
|
|
layout = layouts.layout(config.layout.type, config.layout);
|
|
}
|
|
|
|
if (options && options.cwd && !config.absolute) {
|
|
config.filename = path.join(options.cwd, config.filename);
|
|
}
|
|
|
|
return fileAppender(
|
|
config.filename,
|
|
layout,
|
|
config.maxLogSize,
|
|
config.backups,
|
|
config.compress,
|
|
config.timezoneOffset
|
|
);
|
|
}
|
|
|
|
function shutdown(cb) {
|
|
let completed = 0;
|
|
let error;
|
|
const complete = err => {
|
|
error = error || err;
|
|
completed++;
|
|
if (completed >= openFiles.length) {
|
|
cb(error);
|
|
}
|
|
};
|
|
if (!openFiles.length) {
|
|
return cb();
|
|
}
|
|
|
|
return openFiles.forEach(file => {
|
|
if (!file.write(eol, 'utf-8')) {
|
|
file.once('drain', () => {
|
|
file.end(complete);
|
|
});
|
|
} else {
|
|
file.end(complete);
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports.appender = fileAppender;
|
|
module.exports.configure = configure;
|
|
module.exports.shutdown = shutdown;
|