123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- const debug = require('debug')('log4js:file');
- const path = require('path');
- const streams = require('streamroller');
- const os = require('os');
- const eol = os.EOL;
- let mainSighupListenerStarted = false;
- const sighupListeners = new Set();
- function mainSighupHandler() {
- sighupListeners.forEach((app) => {
- app.sighupHandler();
- });
- }
- function fileAppender(file, layout, logSize, numBackups, options, timezoneOffset) {
- if (typeof file !== "string" || file.length === 0) {
- throw new Error(`Invalid filename: ${file}`);
- } else if (file.endsWith(path.sep)) {
- throw new Error(`Filename is a directory: ${file}`);
- } else {
-
-
- file = file.replace(new RegExp(`^~(?=${path.sep}.+)`), os.homedir());
- }
- file = path.normalize(file);
- numBackups = (!numBackups && numBackups !== 0) ? 5 : numBackups;
- debug(
- 'Creating file appender (',
- file, ', ',
- logSize, ', ',
- numBackups, ', ',
- options, ', ',
- timezoneOffset, ')'
- );
- function openTheStream(filePath, fileSize, numFiles, opt) {
- const stream = new streams.RollingFileStream(
- filePath,
- fileSize,
- numFiles,
- opt
- );
- stream.on('error', (err) => {
-
- console.error('log4js.fileAppender - Writing to file %s, error happened ', filePath, err);
- });
- stream.on('drain', () => {
- process.emit("log4js:pause", false);
- });
- return stream;
- }
- let writer = openTheStream(file, logSize, numBackups, options);
- const app = function (loggingEvent) {
- if (!writer.writable) {
- return;
- }
- if (options.removeColor === true) {
-
- const regex = /\x1b[[0-9;]*m/g;
- loggingEvent.data = loggingEvent.data.map(d => {
- if (typeof d === 'string') return d.replace(regex, '');
- return d;
- });
- }
- if (!writer.write(layout(loggingEvent, timezoneOffset) + eol, "utf8")) {
- process.emit('log4js:pause', true);
- }
- };
- app.reopen = function () {
- writer.end(() => { writer = openTheStream(file, logSize, numBackups, options); });
- };
- app.sighupHandler = function () {
- debug('SIGHUP handler called.');
- app.reopen();
- };
- app.shutdown = function (complete) {
- sighupListeners.delete(app);
- if (sighupListeners.size === 0 && mainSighupListenerStarted) {
- process.removeListener('SIGHUP', mainSighupHandler);
- mainSighupListenerStarted = false;
- }
- writer.end('', 'utf-8', complete);
- };
-
-
-
- sighupListeners.add(app);
- if (!mainSighupListenerStarted) {
- process.on('SIGHUP', mainSighupHandler);
- mainSighupListenerStarted = true;
- }
- return app;
- }
- function configure(config, layouts) {
- let layout = layouts.basicLayout;
- if (config.layout) {
- layout = layouts.layout(config.layout.type, config.layout);
- }
-
- config.mode = config.mode || 0o600;
- return fileAppender(
- config.filename,
- layout,
- config.maxLogSize,
- config.backups,
- config,
- config.timezoneOffset
- );
- }
- module.exports.configure = configure;
|