1
|
{"version":3,"file":"logger.js","names":["_loglevel","_interopRequireDefault","require","DEFAULT_NAMESPACE","log","methodFactory","methodName","logLevel","loggerName","args","prefix","unshift","supportedByConsole","console","logger","getLogger","exports","setLevel","levels","DEBUG","extendLogger","withPrefix","existingPrefix","getPrefixedLogger","prefixLogger"],"sources":["../src/logger.ts"],"sourcesContent":["/*\nCopyright 2018 André Jaenisch\nCopyright 2019, 2021 The Matrix.org Foundation C.I.C.\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport log, { Logger } from \"loglevel\";\n\n// This is to demonstrate, that you can use any namespace you want.\n// Namespaces allow you to turn on/off the logging for specific parts of the\n// application.\n// An idea would be to control this via an environment variable (on Node.js).\n// See https://www.npmjs.com/package/debug to see how this could be implemented\n// Part of #332 is introducing a logging library in the first place.\nconst DEFAULT_NAMESPACE = \"matrix\";\n\n// because rageshakes in react-sdk hijack the console log, also at module load time,\n// initializing the logger here races with the initialization of rageshakes.\n// to avoid the issue, we override the methodFactory of loglevel that binds to the\n// console methods at initialization time by a factory that looks up the console methods\n// when logging so we always get the current value of console methods.\nlog.methodFactory = function (methodName, logLevel, loggerName) {\n return function (this: PrefixedLogger, ...args): void {\n /* eslint-disable @typescript-eslint/no-invalid-this */\n if (this.prefix) {\n args.unshift(this.prefix);\n }\n /* eslint-enable @typescript-eslint/no-invalid-this */\n const supportedByConsole =\n methodName === \"error\" || methodName === \"warn\" || methodName === \"trace\" || methodName === \"info\";\n /* eslint-disable no-console */\n if (supportedByConsole) {\n return console[methodName](...args);\n } else {\n return console.log(...args);\n }\n /* eslint-enable no-console */\n };\n};\n\n/**\n * Drop-in replacement for `console` using {@link https://www.npmjs.com/package/loglevel|loglevel}.\n * Can be tailored down to specific use cases if needed.\n */\nexport const logger = log.getLogger(DEFAULT_NAMESPACE) as PrefixedLogger;\nlogger.setLevel(log.levels.DEBUG, false);\n\nexport interface PrefixedLogger extends Logger {\n withPrefix: (prefix: string) => PrefixedLogger;\n prefix: string;\n}\n\nfunction extendLogger(logger: Logger): void {\n (<PrefixedLogger>logger).withPrefix = function (prefix: string): PrefixedLogger {\n const existingPrefix = this.prefix || \"\";\n return getPrefixedLogger(existingPrefix + prefix);\n };\n}\n\nextendLogger(logger);\n\nfunction getPrefixedLogger(prefix: string): PrefixedLogger {\n const prefixLogger = log.getLogger(`${DEFAULT_NAMESPACE}-${prefix}`) as PrefixedLogger;\n if (prefixLogger.prefix !== prefix) {\n // Only do this setup work the first time through, as loggers are saved by name.\n extendLogger(prefixLogger);\n prefixLogger.prefix = prefix;\n prefixLogger.setLevel(log.levels.DEBUG, false);\n }\n return prefixLogger;\n}\n"],"mappings":";;;;;;;AAiBA,IAAAA,SAAA,GAAAC,sBAAA,CAAAC,OAAA;AAjBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,iBAAiB,GAAG,QAAQ;;AAElC;AACA;AACA;AACA;AACA;AACAC,iBAAG,CAACC,aAAa,GAAG,UAAUC,UAAU,EAAEC,QAAQ,EAAEC,UAAU,EAAE;EAC5D,OAAO,UAAgC,GAAGC,IAAI,EAAQ;IAClD;IACA,IAAI,IAAI,CAACC,MAAM,EAAE;MACbD,IAAI,CAACE,OAAO,CAAC,IAAI,CAACD,MAAM,CAAC;IAC7B;IACA;IACA,MAAME,kBAAkB,GACpBN,UAAU,KAAK,OAAO,IAAIA,UAAU,KAAK,MAAM,IAAIA,UAAU,KAAK,OAAO,IAAIA,UAAU,KAAK,MAAM;IACtG;IACA,IAAIM,kBAAkB,EAAE;MACpB,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