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

All: Improves formatting of log statements

This commit is contained in:
Laurent Cozic 2024-05-01 10:36:20 +01:00
parent 8ec233f59c
commit aac8d58372
2 changed files with 37 additions and 11 deletions

View File

@ -57,6 +57,22 @@ describe('Logger', () => {
jest.useRealTimers(); jest.useRealTimers();
}); });
test.each([
[['one', 'two'], 'one two'],
[[true, false, undefined, null], '<true> <false> <undefined> <null>'],
[['123', 123], '123 123'],
[[['a', 'b', ['sub1', 'sub2']]], '[a, b, [sub1, sub2]]'],
[[''], ''],
[[{ that: 'is json', sub: { key1: 'abc', key2: 'def' } }], '{"that":"is json","sub":{"key1":"abc","key2":"def"}}'],
])('should format messages correctly', async (input, expected) => {
jest.useFakeTimers().setSystemTime(new Date('2020-01-01'));
const logger = createLogger();
logger.info(...input);
await logger.waitForFileWritesToComplete_();
expect(await getLogContent()).toBe(`2020-01-01 00:00:00: testing: ${expected}\n`);
jest.useRealTimers();
});
// it('should keep the last lines', async () => { // it('should keep the last lines', async () => {
// jest.useFakeTimers().setSystemTime(new Date('2020-01-01')); // jest.useFakeTimers().setSystemTime(new Date('2020-01-01'));

View File

@ -177,7 +177,23 @@ class Logger {
public objectToString(object: any) { public objectToString(object: any) {
let output = ''; let output = '';
if (typeof object === 'object') { if (Array.isArray(object)) {
const serialized: string[] = [];
for (const e of object) {
serialized.push(this.objectToString(e));
}
output = `[${serialized.join(', ')}]`;
} else if (typeof object === 'string') {
output = object;
} else if (object === undefined) {
output = '<undefined>';
} else if (object === null) {
output = '<null>';
} else if (object === true) {
output = '<true>';
} else if (object === false) {
output = '<false>';
} else if (typeof object === 'object') {
if (object instanceof Error) { if (object instanceof Error) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
object = object as any; object = object as any;
@ -190,7 +206,7 @@ class Logger {
output = JSON.stringify(object); output = JSON.stringify(object);
} }
} else { } else {
output = object; output = object.toString();
} }
return output; return output;
@ -199,16 +215,10 @@ class Logger {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
public objectsToString(...object: any[]) { public objectsToString(...object: any[]) {
const output = []; const output = [];
if (object.length === 1) {
// Quoting when there is only one argument can make the log more difficult to read,
// particularly when formatting is handled elsewhere.
output.push(this.objectToString(object[0]));
} else {
for (let i = 0; i < object.length; i++) { for (let i = 0; i < object.length; i++) {
output.push(`"${this.objectToString(object[i])}"`); output.push(this.objectToString(object[i]));
} }
} return output.join(' ');
return output.join(', ');
} }
public static databaseCreateTableSql() { public static databaseCreateTableSql() {