File

C:/GoDev/src/TCSTK-Angular/projects/tibco-tcstk/tc-core-lib/src/lib/services/tc-logging.service.ts

Index

Properties
Methods

Methods

debug
debug(msg: string, ...optionalParams: any[])
Parameters :
Name Type Optional
msg string No
optionalParams any[] No
Returns : void
error
error(msg: string, ...optionalParams: any[])
Parameters :
Name Type Optional
msg string No
optionalParams any[] No
Returns : void
fatal
fatal(msg: string, ...optionalParams: any[])
Parameters :
Name Type Optional
msg string No
optionalParams any[] No
Returns : void
Private formatParams
formatParams(params: any[])
Parameters :
Name Type Optional
params any[] No
Returns : string
Private getLevelString
getLevelString(level)
Parameters :
Name Optional
level No
Returns : string
info
info(msg: string, ...optionalParams: any[])
Parameters :
Name Type Optional
msg string No
optionalParams any[] No
Returns : void
log
log(msg: string, ...optionalParams: any[])
Parameters :
Name Type Optional
msg string No
optionalParams any[] No
Returns : void
Private shouldLog
shouldLog(level: LogLevel)
Parameters :
Name Type Optional
level LogLevel No
Returns : boolean
warn
warn(msg: string, ...optionalParams: any[])
Parameters :
Name Type Optional
msg string No
optionalParams any[] No
Returns : void
Private writeToLog
writeToLog(msg: string, level: LogLevel, params: any[])
Parameters :
Name Type Optional
msg string No
level LogLevel No
params any[] No
Returns : void

Properties

level
Type : LogLevel
Default value : LogLevel.All
logWithDate
Default value : true
import { Injectable } from '@angular/core';

@Injectable()
export class LogService {

  level: LogLevel = LogLevel.All;
  logWithDate = true;

  private writeToLog(msg: string,
                     level: LogLevel,
                     params: any[]) {
    if (this.shouldLog(level)) {
      let value = '';

      // Build log string
      if (this.logWithDate) {
        value = new Date() + ' - ';
      }
      value += '[My Cloud Starter] [' + LogLevel[this.level] + '](' + this.getLevelString(level) + ')';
      value += ' - Message: ' + msg;
      if (params.length) {
        value += ' - Extra Info: '
          + this.formatParams(params);
      }

      // Log the value
      console.log(value);
    }
  }

  private shouldLog(level: LogLevel): boolean {
    let ret = false;
    if ((level >= this.level &&
      level !== LogLevel.Off) ||
      this.level === LogLevel.All) {
      ret = true;
    }
    return ret;
  }

  private formatParams(params: any[]): string {
    let ret: string = params.join(',');
    // Is there at least one object in the array?
    if (params.some(p => typeof p === 'object')) {
      ret = '';
      // Build comma-delimited string
      for (const item of params) {
        ret += JSON.stringify(item) + ',';
      }
    }
    return ret;
  }


  debug(msg: string, ...optionalParams: any[]) {
    this.writeToLog(msg, LogLevel.Debug,
      optionalParams);
  }

  info(msg: string, ...optionalParams: any[]) {
    this.writeToLog(msg, LogLevel.Info,
      optionalParams);
  }

  warn(msg: string, ...optionalParams: any[]) {
    this.writeToLog(msg, LogLevel.Warn,
      optionalParams);
  }

  error(msg: string, ...optionalParams: any[]) {
    this.writeToLog(msg, LogLevel.Error,
      optionalParams);
  }

  fatal(msg: string, ...optionalParams: any[]) {
    this.writeToLog(msg, LogLevel.Fatal,
      optionalParams);
  }

  log(msg: string, ...optionalParams: any[]) {
    this.writeToLog(msg, LogLevel.All,
      optionalParams);
  }
  private getLevelString(level) {
    let re = '';
    switch (level) {
      case LogLevel.All: {
        re = 'All';
        break;
      }
      case LogLevel.Debug: {
        re = 'Debug';
        break;
      }
      case LogLevel.Error: {
        re = 'Error';
        break;
      }
      case LogLevel.Fatal: {
        re = 'Fatal';
        break;
      }
      case LogLevel.Info: {
        re = 'Info';
        break;
      }
      case LogLevel.Off: {
        re = 'Off';
        break;
      }
      case LogLevel.Warn: {
        re = 'Warn';
        break;
      }
      default: {
        // statements;
        break;
      }
    }
    return re;

  }
}

export enum LogLevel {
  All = 0,
  Debug = 1,
  Info = 2,
  Warn = 3,
  Error = 4,
  Fatal = 5,
  Off = 6
}

result-matching ""

    No results matching ""