All files / services/internal logService.ts

49.12% Statements 28/57
55.55% Branches 5/9
50% Functions 3/6
49.12% Lines 28/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 581x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x                   10x 10x 30x 3x     30x 10x 10x 10x                                 10x 10x 10x 10x 10x 10x     10x 1x 1x  
import { LogType } from '@constants/enum/logType';
import { ILogMessage } from '@web/contracts/logMessage';
import { Container, Service } from 'typedi';
import { getAppType, getConfig } from './configService';
import { AppType } from '@constants/enum/appType';
 
@Service()
export class LogService {
  private _logs: Array<ILogMessage> = [];
 
  private _logStyle = (type: LogType) => {
    switch (type) {
      case 'log':
        return 'color: green; font-size: medium';
      case 'warn':
        return 'color: orange; font-size: medium';
      case 'error':
        return 'color: red; font-size: large';
    }
  };
 
  private isWebAndProd = () => {
    if (!getConfig().isProd()) return false;
    if (getAppType() !== AppType.UI) return false;

    return true;
  };
 
  private _track =
    (type: LogType) =>
    (message: string, ...optionalParams: Array<unknown>) => {
      const log: ILogMessage = {
        type,
        message,
        optionalParams: optionalParams.map((op) => JSON.stringify(op, null, 2)),
      };
      this._logs.push(log);

      const logStyle = this._logStyle(log.type);
      console.log(
        (log.optionalParams ?? []).length > 0
          ? `%c${log.message}\nadditional params:\n${(log.optionalParams ?? []).join('\n\r')}`
          : `%c${log.message}`,
        logStyle,
      );
    };
 
  i = this.isWebAndProd() ? this._track('log') : console.log;
  w = this.isWebAndProd() ? this._track('warn') : console.warn;
  e = this.isWebAndProd() ? this._track('error') : console.error;
 
  getLogs = () => {
    return this._logs;
  };
}
 
export const getLog = () => Container.get(LogService);