UNPKG

oidc-client-rx

Version:

ReactiveX enhanced OIDC and OAuth2 protocol support for browser-based JavaScript applications

63 lines (62 loc) 3.33 kB
import { Injectable } from "injection-js"; import { injectAbstractType } from "../injection/inject.js"; import { AbstractLoggerService } from "./abstract-logger.service.js"; import { LogLevel } from "./log-level.js"; function _ts_decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc); else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } class LoggerService { logError(configuration, message, ...args) { if (this.loggingIsTurnedOff(configuration)) return; const { configId } = configuration; const messageToLog = this.isObject(message) ? JSON.stringify(message) : message; if (args && args.length) this.abstractLoggerService.logError(`[ERROR] ${configId} - ${messageToLog}`, ...args); else this.abstractLoggerService.logError(`[ERROR] ${configId} - ${messageToLog}`); } logWarning(configuration, message, ...args) { if (!this.logLevelIsSet(configuration)) return; if (this.loggingIsTurnedOff(configuration)) return; if (!this.currentLogLevelIsEqualOrSmallerThan(configuration, LogLevel.Warn)) return; const { configId } = configuration; const messageToLog = this.isObject(message) ? JSON.stringify(message) : message; if (args && args.length) this.abstractLoggerService.logWarning(`[WARN] ${configId} - ${messageToLog}`, ...args); else this.abstractLoggerService.logWarning(`[WARN] ${configId} - ${messageToLog}`); } logDebug(configuration, message, ...args) { if (!configuration) return; if (!this.logLevelIsSet(configuration)) return; if (this.loggingIsTurnedOff(configuration)) return; if (!this.currentLogLevelIsEqualOrSmallerThan(configuration, LogLevel.Debug)) return; const { configId } = configuration; const messageToLog = this.isObject(message) ? JSON.stringify(message) : message; if (args && args.length) this.abstractLoggerService.logDebug(`[DEBUG] ${configId} - ${messageToLog}`, ...args); else this.abstractLoggerService.logDebug(`[DEBUG] ${configId} - ${messageToLog}`); } currentLogLevelIsEqualOrSmallerThan(configuration, logLevelToCompare) { const { logLevel } = configuration || {}; if (!logLevel) return false; return logLevel <= logLevelToCompare; } logLevelIsSet(configuration) { const { logLevel } = configuration || {}; if (null === logLevel) return false; return void 0 !== logLevel; } loggingIsTurnedOff(configuration) { const { logLevel } = configuration || {}; return logLevel === LogLevel.None; } isObject(possibleObject) { return "[object Object]" === Object.prototype.toString.call(possibleObject); } constructor(){ this.abstractLoggerService = injectAbstractType(AbstractLoggerService); } } LoggerService = _ts_decorate([ Injectable() ], LoggerService); export { LoggerService };