2019-09-09 10:48:09 -07:00
|
|
|
const tmp = require('tmp');
|
|
|
|
const dir = tmp.dirSync();
|
2019-11-29 15:36:57 -07:00
|
|
|
let settings;
|
2019-09-09 10:48:09 -07:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2019-11-29 15:36:57 -07:00
|
|
|
const data = require('./stub/data');
|
|
|
|
let stdOutWriteOriginal;
|
|
|
|
const rimraf = require('rimraf');
|
2021-01-19 10:14:40 -07:00
|
|
|
const Transport = require('winston-transport');
|
2019-09-09 10:48:09 -07:00
|
|
|
|
|
|
|
describe('Logger', () => {
|
2019-11-29 15:36:57 -07:00
|
|
|
beforeEach(async () => {
|
|
|
|
data.writeDefaultConfiguration();
|
|
|
|
jest.resetModules();
|
|
|
|
settings = require('../lib/util/settings');
|
|
|
|
settings.set(['advanced', 'log_directory'], dir.name + '/%TIMESTAMP%');
|
2021-03-09 11:50:05 -07:00
|
|
|
settings.reRead();
|
2019-11-29 15:36:57 -07:00
|
|
|
stdOutWriteOriginal = console._stdout.write;
|
|
|
|
console._stdout.write = () => {};
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
console._stdout.write = stdOutWriteOriginal;
|
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Create log directory', () => {
|
2019-11-29 15:36:57 -07:00
|
|
|
const logger = require('../lib/util/logger.js');
|
2020-10-07 08:15:46 -07:00
|
|
|
logger.logOutput();
|
2019-09-09 10:48:09 -07:00
|
|
|
const dirs = fs.readdirSync(dir.name);
|
|
|
|
expect(dirs.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should cleanup', () => {
|
2019-11-29 15:36:57 -07:00
|
|
|
const logger = require('../lib/util/logger.js');
|
2020-10-07 08:15:46 -07:00
|
|
|
logger.logOutput();
|
2019-11-29 15:36:57 -07:00
|
|
|
|
|
|
|
for (const d of fs.readdirSync(dir.name)) {
|
|
|
|
rimraf.sync(path.join(dir.name, d));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < 21; i++) {
|
2019-09-09 10:48:09 -07:00
|
|
|
fs.mkdirSync(path.join(dir.name, `log_${i}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(fs.readdirSync(dir.name).length).toBe(21);
|
|
|
|
logger.cleanup();
|
|
|
|
expect(fs.readdirSync(dir.name).length).toBe(10);
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not cleanup when there is no timestamp set', () => {
|
2019-11-29 15:36:57 -07:00
|
|
|
const logger = require('../lib/util/logger.js');
|
2020-10-07 08:15:46 -07:00
|
|
|
logger.logOutput();
|
2019-09-09 10:48:09 -07:00
|
|
|
for (let i = 30; i < 40; i++) {
|
|
|
|
fs.mkdirSync(path.join(dir.name, `log_${i}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.set(['advanced', 'log_directory'], dir.name + '/bla');
|
|
|
|
expect(fs.readdirSync(dir.name).length).toBe(20);
|
|
|
|
logger.cleanup();
|
|
|
|
expect(fs.readdirSync(dir.name).length).toBe(20);
|
|
|
|
})
|
2019-09-25 01:55:50 -07:00
|
|
|
|
|
|
|
it('Set and get log level', () => {
|
2019-11-29 15:36:57 -07:00
|
|
|
const logger = require('../lib/util/logger.js');
|
2020-10-07 08:15:46 -07:00
|
|
|
logger.logOutput();
|
2019-09-25 01:55:50 -07:00
|
|
|
logger.setLevel('debug');
|
|
|
|
expect(logger.getLevel()).toBe('debug');
|
|
|
|
});
|
2019-11-29 15:36:57 -07:00
|
|
|
|
2021-01-19 10:14:40 -07:00
|
|
|
it('Add transport', () => {
|
|
|
|
class DummyTransport extends Transport {
|
|
|
|
log(info, callback) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const logger = require('../lib/util/logger.js');
|
|
|
|
expect(logger.transports.length).toBe(2);
|
|
|
|
logger.addTransport(new DummyTransport());
|
|
|
|
expect(logger.transports.length).toBe(3);
|
|
|
|
});
|
|
|
|
|
2021-01-18 10:31:47 -07:00
|
|
|
it('Set and get log level warn <-> warning', () => {
|
|
|
|
const logger = require('../lib/util/logger.js');
|
|
|
|
logger.logOutput();
|
|
|
|
logger.setLevel('warn');
|
|
|
|
expect(logger.transports[0].level).toBe('warning');
|
|
|
|
expect(logger.getLevel()).toBe('warn');
|
|
|
|
});
|
|
|
|
|
2019-11-29 15:36:57 -07:00
|
|
|
it('Logger should be console and file by default', () => {
|
|
|
|
const logger = require('../lib/util/logger.js');
|
2020-10-07 08:15:46 -07:00
|
|
|
logger.logOutput();
|
2019-11-29 15:36:57 -07:00
|
|
|
const pipes = logger._readableState.pipes;
|
|
|
|
expect(pipes.length).toBe(2);
|
|
|
|
expect(pipes[0].constructor.name).toBe('Console');
|
|
|
|
expect(pipes[0].silent).toBe(false);
|
|
|
|
expect(pipes[1].constructor.name).toBe('File');
|
|
|
|
expect(pipes[1].dirname.startsWith(dir.name)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Logger can be file only', () => {
|
|
|
|
settings.set(['advanced', 'log_output'], ['file']);
|
|
|
|
const logger = require('../lib/util/logger.js');
|
2020-10-07 08:15:46 -07:00
|
|
|
logger.logOutput();
|
2019-11-29 15:36:57 -07:00
|
|
|
const pipes = logger._readableState.pipes;
|
|
|
|
expect(pipes.length).toBe(2);
|
|
|
|
expect(pipes[0].constructor.name).toBe('Console');
|
|
|
|
expect(pipes[0].silent).toBe(true);
|
|
|
|
expect(pipes[1].constructor.name).toBe('File');
|
|
|
|
expect(pipes[1].dirname.startsWith(dir.name)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Logger can be console only', () => {
|
|
|
|
settings.set(['advanced', 'log_output'], ['console']);
|
|
|
|
const logger = require('../lib/util/logger.js');
|
2020-10-07 08:15:46 -07:00
|
|
|
logger.logOutput();
|
2019-11-29 15:36:57 -07:00
|
|
|
const pipes = logger._readableState.pipes;
|
|
|
|
expect(pipes.constructor.name).toBe('Console');
|
|
|
|
expect(pipes.silent).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Logger can be nothing', () => {
|
|
|
|
settings.set(['advanced', 'log_output'], []);
|
|
|
|
const logger = require('../lib/util/logger.js');
|
2020-10-07 08:15:46 -07:00
|
|
|
logger.logOutput();
|
2019-11-29 15:36:57 -07:00
|
|
|
const pipes = logger._readableState.pipes;
|
|
|
|
expect(pipes.constructor.name).toBe('Console');
|
|
|
|
expect(pipes.silent).toBe(true);
|
|
|
|
});
|
2020-04-04 10:46:43 -07:00
|
|
|
|
|
|
|
it('Should allow to disable log rotation', () => {
|
|
|
|
settings.set(['advanced', 'log_rotation'], false);
|
|
|
|
const logger = require('../lib/util/logger.js');
|
2020-10-07 08:15:46 -07:00
|
|
|
logger.logOutput();
|
2020-04-04 10:46:43 -07:00
|
|
|
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();
|
|
|
|
});
|
2019-09-09 10:48:09 -07:00
|
|
|
});
|