1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Enable prefixed loggers to chain

This commit is contained in:
J. Ryan Stinnett
2021-02-26 17:47:52 +00:00
parent 198c9a2507
commit e217bf9e37
3 changed files with 27 additions and 17 deletions

View File

@@ -59,17 +59,27 @@ log.methodFactory = function(methodName, logLevel, loggerName) {
* Drop-in replacement for <code>console</code> using {@link https://www.npmjs.com/package/loglevel|loglevel}.
* Can be tailored down to specific use cases if needed.
*/
export const logger = log.getLogger(DEFAULT_NAMESPACE);
export const logger: PrefixedLogger = log.getLogger(DEFAULT_NAMESPACE);
logger.setLevel(log.levels.DEBUG);
interface PrefixedLogger extends Logger {
prefix?: any;
withPrefix?: (prefix: string) => PrefixedLogger;
prefix?: string;
}
export function getPrefixedLogger(prefix): PrefixedLogger {
function extendLogger(logger: PrefixedLogger) {
logger.withPrefix = function(prefix: string) {
return getPrefixedLogger(this.prefix + prefix);
};
}
extendLogger(logger);
function getPrefixedLogger(prefix): PrefixedLogger {
const prefixLogger: PrefixedLogger = log.getLogger(`${DEFAULT_NAMESPACE}-${prefix}`);
if (prefixLogger.prefix !== prefix) {
// Only do this setup work the first time through, as loggers are saved by name.
extendLogger(prefixLogger);
prefixLogger.prefix = prefix;
prefixLogger.setLevel(log.levels.DEBUG);
}