You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-11-29 22:48:10 +02:00
Desktop, Cli: Improved log formatting and allow saving last lines of log to memory
This commit is contained in:
89
packages/utils/Logger.test.ts
Normal file
89
packages/utils/Logger.test.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
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'));
|
||||
|
||||
// Shouldn't have kept any line, since keptLineCount was not set
|
||||
expect(logger.keptLines).toEqual([]);
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user