All files / src index.js

95.45% Statements 21/22
60% Branches 18/30
100% Functions 4/4
95.45% Lines 21/22
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                    1x 1x 1x 1x 1x 1x   1x 1x 1x   1x         1x 1x           1x           1x               1x           1x       1x           1x       1x           1x   1x              
import winston from "winston";
import moment from "moment";
 
import UnhandledRejectionTransport from "./transports/unhandledRejection";
 
import FormatLogger from "./format";
import Metric from "./metric.js";
 
class Yodelay {
  constructor(params) {
    this.appName = params.appName;
    this.alertOnError = params.alertOnError || true;
    this.level = params.level || "debug";
    this.metricsEndpoint = params.metricsEndpoint || null;
    this.format = params.format || "json";
    this.debugContext = params.debugContext || null;
 
    this.info = this.info;
    this.error = this.error;
    this.debug = this.debug;
 
    const logger = winston.createLogger({
      level: this.level,
      exitOnError: false
    });
 
    this.logger = logger;
    this.metric = new Metric({
      metricsEndpoint: this.metricsEndpoint,
      appName: this.appName,
      logger: this.logger
    });
 
    new FormatLogger({
      debugContext: this.debugContext,
      logger: this.logger,
      ...{ format: this.format, appName: this.appName }
    }).setFormat();
 
    new UnhandledRejectionTransport({
      logger: this.logger,
      appName: this.appName,
      metric: this.metric
    }).initialize();
  }
 
  debug(message, data, context) {
    const logMessage = this.logger.logMessageFormat(
      message || "",
      data || "",
      context || ""
    );
 
    this.logger.debug(logMessage);
  }
 
  info(message, data, context) {
    const logMessage = this.logger.logMessageFormat(
      message || "",
      data || "",
      context || ""
    );
 
    this.logger.info(logMessage);
  }
 
  error(message, data, context) {
    const logMessage = this.logger.logMessageFormat(
      message || "",
      data || "",
      context || ""
    );
 
    this.logger.error(logMessage);
 
    Iif (this.metricsEndpoint) {
      this.metric.send(logMessage, "error");
    }
  }
}
 
export default Yodelay;