@churchapps/apihelper
Version:
Library of helper functions not specific to any one ChurchApps project or framework.
83 lines • 3.09 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoggingHelper = void 0;
const winston_1 = __importDefault(require("winston"));
const winston_cloudwatch_1 = __importDefault(require("winston-cloudwatch"));
//import AWS from "aws-sdk";
const _1 = require(".");
class LoggingHelper {
constructor() {
this._logger = null;
this.pendingMessages = false;
this.logGroupName = _1.EnvironmentBase.appName + "_" + _1.EnvironmentBase.appEnv;
this.logDestination = "console";
}
error(msg) {
if (this._logger === null)
this.init("API");
this.pendingMessages = true;
if (_1.EnvironmentBase.appEnv === "dev")
console.log(msg);
this._logger.error(msg);
this._logger.error(new Error().stack.toString());
}
info(msg) {
if (this._logger === null)
this.init("API");
this.pendingMessages = true;
this._logger.info(msg);
}
log(streamName, level, msg) {
if (this._logger === null)
this.init(streamName);
this.pendingMessages = true;
if (level === "info")
this._logger.info(msg);
else
this._logger.error(msg);
}
init(streamName) {
this.pendingMessages = false;
//AWS.config.update({ region: "us-east-2" });
if (_1.EnvironmentBase.appEnv === "staging")
this.logDestination = "cloudwatch";
else if (_1.EnvironmentBase.appEnv === "prod")
this.logDestination = "cloudwatch";
if (this.logDestination === "cloudwatch") {
this.wc = new winston_cloudwatch_1.default({ logGroupName: this.logGroupName, logStreamName: streamName, name: this.logGroupName + "_" + streamName });
this._logger = winston_1.default.createLogger({ transports: [this.wc], format: winston_1.default.format.json() });
}
else
this._logger = winston_1.default.createLogger({ transports: [new winston_1.default.transports.Console()], format: winston_1.default.format.json() });
this._logger.info("Logger initialized");
}
flush() {
const promise = new Promise((resolve) => {
if (this.pendingMessages) {
if (this.wc) {
this.wc.kthxbye(() => {
// this._logger = null;
this.pendingMessages = false;
});
}
resolve(null);
}
else
resolve(null);
});
return promise;
}
}
exports.LoggingHelper = LoggingHelper;
LoggingHelper._current = null;
LoggingHelper.getCurrent = () => {
if (LoggingHelper._current === null) {
LoggingHelper._current = new LoggingHelper();
LoggingHelper._current.init("API");
}
return LoggingHelper._current;
};
//# sourceMappingURL=LoggingHelper.js.map