diff --git a/lib/util/logger.js b/lib/util/logger.js index 0cd1deb80..4f36a0556 100644 --- a/lib/util/logger.js +++ b/lib/util/logger.js @@ -54,22 +54,27 @@ const transportsToUse = [ // Add file logger when enabled // NOTE: the initiation of the logger, even when not added as transport tries to create the logging directory +const transportFileOptions = { + filename: path.join(directory, logFilename), + json: false, + level, + format: winston.format.combine( + winston.format.timestamp({format: timestampFormat}), + winston.format.printf(/* istanbul ignore next */(info) => { + const {timestamp, level, message} = info; + return `${levelWithCompensatedLength[level]} ${timestamp.split('.')[0]}: ${message}`; + }), + ), +}; + +if (settings.get().advanced.log_rotation) { + transportFileOptions.tailable = true; + transportFileOptions.maxFiles = 3; // Keep last 3 files + transportFileOptions.maxsize = 10000000; // 10MB +} + if (output.includes('file')) { - transportsToUse.push(new winston.transports.File({ - filename: path.join(directory, logFilename), - json: false, - level, - tailable: true, - maxFiles: 3, // Keep last 3 files - maxsize: 10000000, // 10MB - format: winston.format.combine( - winston.format.timestamp({format: timestampFormat}), - winston.format.printf(/* istanbul ignore next */(info) => { - const {timestamp, level, message} = info; - return `${levelWithCompensatedLength[level]} ${timestamp.split('.')[0]}: ${message}`; - }), - ), - })); + transportsToUse.push(new winston.transports.File(transportFileOptions)); } // Create logger diff --git a/lib/util/settings.js b/lib/util/settings.js index ebc06db8d..9ff26b33c 100644 --- a/lib/util/settings.js +++ b/lib/util/settings.js @@ -43,6 +43,7 @@ const defaults = { output: 'json', }, advanced: { + log_rotation: true, log_output: ['console', 'file'], log_directory: path.join(data.getPath(), 'log', '%TIMESTAMP%'), log_file: 'log.txt', @@ -167,6 +168,7 @@ const schema = { ext_pan_id: {type: 'array', items: {type: 'number'}}, channel: {type: 'number', minimum: 11, maximum: 26}, cache_state: {type: 'boolean'}, + log_rotation: {type: 'boolean'}, log_level: {type: 'string', enum: ['info', 'warn', 'error', 'debug']}, log_output: {type: 'array', items: {type: 'string'}}, log_directory: {type: 'string'}, diff --git a/test/logger.test.js b/test/logger.test.js index fd3521cf6..03f64e395 100644 --- a/test/logger.test.js +++ b/test/logger.test.js @@ -98,4 +98,14 @@ describe('Logger', () => { expect(pipes.constructor.name).toBe('Console'); expect(pipes.silent).toBe(true); }); + + it('Should allow to disable log rotation', () => { + settings.set(['advanced', 'log_rotation'], false); + const logger = require('../lib/util/logger.js'); + const pipes = logger._readableState.pipes; + expect(pipes[1].constructor.name).toBe('File'); + expect(pipes[1].maxFiles).toBeNull(); + expect(pipes[1].tailable).toBeFalsy(); + expect(pipes[1].maxsize).toBeNull(); + }); });