-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to use new hookshot logging code
- Loading branch information
Showing
2 changed files
with
236 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,86 @@ | ||
import { LogService } from "matrix-bot-sdk"; | ||
import { CustomLogger, Logger } from "../.."; | ||
import { Writable } from "stream"; | ||
import { Logger, GlobalLogger } from "../../src/index"; | ||
|
||
const tortureArgs: [unknown, ...unknown[]][] = [ | ||
["test-msg"], | ||
[Number.MAX_VALUE], | ||
[false], | ||
[Buffer.from('foo')], | ||
[new Error('Test')], | ||
[undefined], | ||
[null], | ||
[NaN], | ||
[[]], | ||
[() => { /*dummy*/}], | ||
["Foo", "test-msg"], | ||
["Foo", Number.MAX_VALUE], | ||
["Foo", false], | ||
["Foo", Buffer.from('foo')], | ||
["Foo", new Error('Test')], | ||
["Foo", undefined], | ||
["Foo", null], | ||
["Foo", NaN], | ||
["Foo", []], | ||
["Foo", () => { /*dummy*/}], | ||
] | ||
|
||
type LoggerReturn = {level: string, msg: string, metadata: unknown}; | ||
const MODULE_NAME = 'LogTesting'; | ||
|
||
async function loggerGenerator(): Promise<{level: string, msg: string, metadata: unknown}>{ | ||
return new Promise(res => { | ||
Logger.configure({ logger: { | ||
debug: (msg, metadata) => res({level: 'debug', msg, metadata}), | ||
verbose: (msg, metadata) => res({level: 'verbose', msg, metadata}), | ||
info: (msg, metadata) => res({level: 'info', msg, metadata}), | ||
error: (msg, metadata) => res({level: 'error', msg, metadata}), | ||
warn: (msg, metadata) => res({level: 'warn', msg, metadata}), | ||
}}); | ||
}); | ||
} | ||
describe('Logger', () => { | ||
describe('text logger torture test', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let data: any; | ||
const global = new GlobalLogger(); | ||
global.configureLogging({ | ||
json: false, | ||
console: 'debug', | ||
}, new Writable({ | ||
write(chunk, _encoding, callback) { | ||
data = chunk.toString(); | ||
callback(); | ||
}, | ||
})); | ||
|
||
describe("Logger", function() { | ||
beforeEach(() =>{ | ||
// Reset instance before each test. | ||
Logger.innerLog = undefined; | ||
}); | ||
it("can be configured with a basic log level", () => { | ||
Logger.configure({ console: 'info' }); | ||
expect(Logger.innerLog).toBeDefined(); | ||
}); | ||
it("can handle a simple log statement", async () => { | ||
const logger = loggerGenerator(); | ||
const log = new Logger('FooLog'); | ||
log.info('Hello!'); | ||
expect(await logger).toEqual({level: 'info', msg: 'Hello!', metadata: { module: 'FooLog' }}); | ||
}); | ||
it("can handle a log statement with a requestId", async () => { | ||
const logger = loggerGenerator(); | ||
const log = new Logger('FooLog', { requestId: '123'}); | ||
log.info('Hello!'); | ||
expect(await logger).toEqual({level: 'info', msg: 'Hello!', metadata: { module: 'FooLog', requestId: '123' }}); | ||
}); | ||
it("redirects noisy bot-sdk messages to debug", async () => { | ||
for (const messages of [ | ||
{ errcode: 'M_NOT_FOUND', error: 'Room account data not found'}, | ||
{ errcode: 'M_NOT_FOUND', error: 'Event not found.'}, | ||
{ errcode: 'M_USER_IN_USE'}, | ||
{ body: { errcode: 'M_NOT_FOUND', error: 'Room account data not found'}}, | ||
{ body: { errcode: 'M_NOT_FOUND', error: 'Event not found.'}}, | ||
{ body: { errcode: 'M_USER_IN_USE'}}, | ||
]) { | ||
const logger = loggerGenerator(); | ||
LogService.error('BotModule', messages); | ||
const { level } = await logger; | ||
expect(level).toBe('debug'); | ||
} | ||
}); | ||
const log = new Logger(MODULE_NAME, {}, global); | ||
for (const args of tortureArgs) { | ||
it(`handles logging '${args.map(t => typeof t).join(', ')}'`, () => { | ||
for (const level of ['debug', 'info', 'warn', 'error']) { | ||
log[level as 'debug'|'info'|'warn'|'error'](args[0], ...args.slice(1)); | ||
expect(data).toBeDefined(); | ||
expect(data).toContain(level.toUpperCase()); | ||
expect(data).toContain(MODULE_NAME); | ||
} | ||
}) | ||
} | ||
}); | ||
describe('JSON logger torture test', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let data: any; | ||
const global = new GlobalLogger(); | ||
global.configureLogging({ | ||
json: true, | ||
console: 'debug', | ||
}, new Writable({ | ||
write(chunk, _encoding, callback) { | ||
data = JSON.parse(chunk.toString()); | ||
callback(); | ||
}, | ||
})); | ||
|
||
const log = new Logger(MODULE_NAME, {}, global); | ||
for (const args of tortureArgs) { | ||
it(`handles logging '${args.map(t => typeof t).join(', ')}'`, () => { | ||
for (const level of ['debug', 'info', 'warn', 'error']) { | ||
log[level as 'debug'|'info'|'warn'|'error'](args[0], ...args.slice(1)); | ||
expect(data.level).toEqual(level.toUpperCase()); | ||
expect(data.module).toEqual(MODULE_NAME); | ||
expect(data.message).toBeDefined(); | ||
expect(data.timestamp).toBeDefined(); | ||
if (args.length > 1) { | ||
expect(data.args).toHaveSize(args.length-1); | ||
} | ||
} | ||
}) | ||
} | ||
}); | ||
}); |
Oops, something went wrong.