all files / src/ open-id-connect-logger.ts

100% Statements 30/30
100% Branches 14/14
100% Functions 8/8
100% Lines 30/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79       30×   26×     30× 29×                                                                 29×               29×     27× 27× 27×    
import { Log } from 'oidc-client';
 
export default class OpenIdConnectLogger {
 
  // tslint:disable-next-line:variable-name
  private _level: number = Log.NONE;
 
  public get level() {
    return this._level;
  }
 
  constructor(level: number) {
    if (level !== null && level !== undefined) {
      this.setLogLevel(level);
    }
  }
 
  // +++++++++++++++++++++++++++++++++++++++++++++
  // TODO: Use currying or function overloading to encapsulate shared logic.
 
  public debug(msg: string) {
    if (this.level >= Log.DEBUG) {
      /* tslint:disable no-console */
      console.debug(`DEBUG [OpenIdConnect] ${msg}`);
    }
  }
 
  public info(msg: string) {
    if (this.level >= Log.INFO) {
      /* tslint:disable no-console */
      console.info(`INFO [OpenIdConnect] ${msg}`);
    }
  }
 
  public warn(msg: string) {
    if (this.level >= Log.WARN) {
      /* tslint:disable no-console */
      console.warn(`WARN [OpenIdConnect] ${msg}`);
    }
  }
 
  public error(msg: string) {
    if (this.level >= Log.ERROR) {
      /* tslint:disable no-console */
      console.error(`ERROR [OpenIdConnect] ${msg}`);
    }
  }
 
  // END TODO
  // +++++++++++++++++++++++++++++++++++++++++++++++
 
  /**
   * Set the log level for both the aurelia-open-id-connect logger
   * and the underlying oidc-client logger.
   */
  private setLogLevel(level: number) {
 
    // Levels are ordered from most to least verbose.
    // The DEBUG level includes all the log levels.
    const validOidcClientLevels: number[] = [
      Log.DEBUG, // 4
      Log.INFO, // 3
      Log.WARN, // 2
      Log.ERROR, // 1
      Log.NONE, // 0
    ];
 
    if (!validOidcClientLevels.includes(level)) {
      const levels: string = validOidcClientLevels.join(', ');
      const msg: string = `The log level must be one of ${levels}`;
      throw new RangeError(msg);
    }
 
    this._level = level;
    Log.level = level;
    Log.logger = console;
  }
}