All files / src format.js

82.76% Statements 24/29
46.67% Branches 7/15
100% Functions 7/7
82.76% Lines 24/29
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102          1x 1x 1x 1x 1x       2x 2x       2x         1x 2x       2x 2x           1x       3x         3x 3x     3x 3x     3x       1x 1x   1x                           1x                     1x                                  
import winston from "winston";
import moment from "moment";
 
class FormatLogger {
  constructor(params) {
    this.logger = params.logger;
    this.logger.logMessageFormat = this.logMessageFormat.bind(this);
    this.format = params.format;
    this.appName = params.appName;
    this.debugContext = params.debugContext;
  }
 
  filterContext() {
    return winston.format((info, opts) => {
      Iif (this.debugContext !== null && info.context !== this.debugContext) {
        return false;
      }
 
      return info;
    })();
  }
 
  simpleBaseFormat() {
    const base = winston.format.printf(info => {
      const string = `${moment(info.timestamp)} [${info.app}] ${
        info.context ? `[context: ${info.context}]}` : ""
      } ${info.level}: ${info.message}`;
 
      Eif (info.data) {
        return `${string} ${JSON.stringify(info.data)}`;
      }
 
      return string;
    });
 
    return winston.format.combine(winston.format.timestamp(), base);
  }
 
  logMessageFormat(message, data, context) {
    let logMessage = {
      app: this.appName,
      message: message
    };
 
    Eif (data) {
      logMessage.data = data;
    }
 
    Eif (context) {
      logMessage.context = context;
    }
 
    return logMessage;
  }
 
  setFormat() {
    const simpleBase = this.simpleBaseFormat();
    const filterContext = this.filterContext();
 
    switch (this.format) {
      case "json":
        this.logger.add(
          new winston.transports.Console({
            format: winston.format.combine(
              this.filterContext(),
              winston.format.timestamp(),
              winston.format.json()
            )
          })
        );
 
        break;
      case "simple":
        this.logger.add(
          new winston.transports.Console({
            format: winston.format.combine(
              this.filterContext(),
              winston.format.colorize(),
              winston.format.simple(),
              simpleBase
            )
          })
        );
 
        break;
      default:
        this.logger.add(
          new winston.transports.Console({
            format: winston.format.combine(
              this.filterContext(),
              winston.format.colorize(),
              winston.format.simple(),
              simpleBase
            )
          })
        );
    }
  }
}
 
export default FormatLogger;