2018-01-24 23:38:07 +00:00
|
|
|
"use strict";
|
2018-01-27 18:16:05 +00:00
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
2018-01-24 23:38:07 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
// interfaces
|
|
|
|
const elasticsearch_1 = require("elasticsearch");
|
2018-01-27 18:16:05 +00:00
|
|
|
// other classes
|
|
|
|
const elasticlog_classes_logscheduler_1 = require("./elasticlog.classes.logscheduler");
|
2018-01-24 23:38:07 +00:00
|
|
|
class ElasticLog {
|
|
|
|
/**
|
|
|
|
* sets up an instance of Elastic log
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
|
|
|
constructor(optionsArg) {
|
2018-01-27 18:16:05 +00:00
|
|
|
this.logScheduler = new elasticlog_classes_logscheduler_1.LogScheduler(this);
|
2018-01-24 23:38:07 +00:00
|
|
|
this.logContext = optionsArg.logContext;
|
|
|
|
this.client = new elasticsearch_1.Client({
|
|
|
|
host: this.computeHostString(optionsArg),
|
2018-01-27 18:16:05 +00:00
|
|
|
log: "trace"
|
2018-01-24 23:38:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* computes the host string from the constructor options
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
|
|
|
computeHostString(optionsArg) {
|
|
|
|
let hostString = `${optionsArg.domain}:${optionsArg.port}`;
|
|
|
|
if (optionsArg.user && optionsArg.pass) {
|
|
|
|
hostString = `${optionsArg.user}:${optionsArg.pass}@${hostString}`;
|
|
|
|
}
|
|
|
|
if (optionsArg.ssl) {
|
|
|
|
hostString = `https://${hostString}`;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
hostString = `http://${hostString}`;
|
|
|
|
}
|
|
|
|
return hostString;
|
|
|
|
}
|
2018-01-27 18:16:05 +00:00
|
|
|
log(logObject, scheduleOverwrite = false) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
const now = new Date();
|
|
|
|
if (this.logScheduler.logsScheduled && !scheduleOverwrite) {
|
|
|
|
this.logScheduler.scheduleLog(logObject);
|
|
|
|
return;
|
2018-01-24 23:38:07 +00:00
|
|
|
}
|
2018-01-27 18:16:05 +00:00
|
|
|
this.client.index({
|
|
|
|
index: `logs-${now.getFullYear()}.${("0" + (now.getMonth() + 1)).slice(-2)}.${now.getDate()}`,
|
|
|
|
type: "log",
|
|
|
|
body: {
|
|
|
|
"@timestamp": now.toISOString(),
|
|
|
|
container: this.logContext.containerName,
|
|
|
|
environment: this.logContext.environment,
|
|
|
|
severity: logObject.severity,
|
|
|
|
message: logObject.message
|
|
|
|
}
|
|
|
|
}, (error, response) => {
|
|
|
|
if (error) {
|
|
|
|
console.log("ElasticLog encountered an error:");
|
|
|
|
console.log(error);
|
|
|
|
this.logScheduler.addFailedLog(logObject);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log(`ElasticLog: ${logObject.message}`);
|
|
|
|
}
|
|
|
|
});
|
2018-01-24 23:38:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.ElasticLog = ElasticLog;
|
2018-01-27 18:16:05 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxhc3RpY2xvZy5jbGFzc2VzLmVsYXN0aWNsb2cuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9lbGFzdGljbG9nLmNsYXNzZXMuZWxhc3RpY2xvZy50cyJdL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