1
0
Fork 0
js-utils/src/Logger.ts

32 lines
711 B
TypeScript
Raw Normal View History

2020-03-29 13:43:52 +00:00
import { ConsoleLogger, Logger, NullLogger } from 'noicejs';
2022-10-08 19:10:17 +00:00
import { isDebug } from './Env.js';
2020-03-29 13:43:52 +00:00
/**
* Get a test logger. Returns a null logger unless `verbose` is true or run under debug mode.
*
* @public
*/
2020-03-29 13:43:52 +00:00
export function getTestLogger(verbose = false): Logger {
if (verbose || isDebug()) {
return ConsoleLogger.global;
2020-03-29 13:43:52 +00:00
} else {
return NullLogger.global;
2020-03-29 13:43:52 +00:00
}
}
/**
* Create a spy logger using the provided methods, which returns itself as a child.
*
* @internal
*/
2020-03-29 13:43:52 +00:00
export function spyLogger(spies: Partial<Logger>): Logger {
2022-10-08 19:52:35 +00:00
// TODO: ensure all methods are present by extending null logger
2020-03-29 13:43:52 +00:00
const logger = {
...spies,
child: () => logger,
} as Logger;
2020-03-29 13:43:52 +00:00
return logger;
}