1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/utils/Logger.test.ts

87 lines
2.0 KiB
TypeScript

import Logger, { LogLevel, TargetType } from './Logger';
import { WriteFileOptions, appendFile, mkdirp, readFile, remove } from 'fs-extra';
Logger.fsDriver_ = {
appendFile: async (path, content, encoding) => {
return await appendFile(path, content, encoding as WriteFileOptions);
},
};
const testDirPath = `${__dirname}/LoggerTests`;
const logPath = () => {
return `${testDirPath}/log.txt`;
};
const getLogContent = async () => {
return readFile(logPath(), 'utf-8');
};
const createLogger = () => {
const logger = new Logger();
logger.addTarget(TargetType.File, {
prefix: 'testing',
path: logPath(),
level: LogLevel.Debug,
});
return logger;
};
describe('Logger', () => {
beforeEach(async () => {
await mkdirp(testDirPath);
});
afterEach(async () => {
await remove(testDirPath);
});
it('should log to file', async () => {
jest.useFakeTimers().setSystemTime(new Date('2020-01-01'));
const logger = createLogger();
logger.debug('one');
logger.warn('two');
logger.error('three');
await logger.waitForFileWritesToComplete_();
expect(await getLogContent()).toBe([
'2020-01-01 00:00:00: testing: one',
'2020-01-01 00:00:00: testing: [warn] two',
'2020-01-01 00:00:00: testing: [error] three',
'',
].join('\n'));
jest.useRealTimers();
});
// it('should keep the last lines', async () => {
// jest.useFakeTimers().setSystemTime(new Date('2020-01-01'));
// const logger = createLogger();
// logger.keptLineCount = 2;
// logger.info('one');
// logger.info('two');
// logger.info('three');
// await logger.waitForFileWritesToComplete_();
// expect(await getLogContent()).toBe([
// '2020-01-01 00:00:00: testing: one',
// '2020-01-01 00:00:00: testing: two',
// '2020-01-01 00:00:00: testing: three',
// '',
// ].join('\n'));
// expect(logger.keptLines).toEqual([
// '2020-01-01 00:00:00: testing: two',
// '2020-01-01 00:00:00: testing: three',
// ]);
// jest.useRealTimers();
// });
});