2018-01-27 18:16:05 +00:00
|
|
|
export type Environment = "local" | "test" | "staging" | "production";
|
2018-01-24 23:38:07 +00:00
|
|
|
|
|
|
|
// interfaces
|
2018-01-27 18:16:05 +00:00
|
|
|
import { Client as ElasticClient } from "elasticsearch";
|
|
|
|
import { IWinstonStandardLogParams } from "./elasticlog.classes.winstontransport";
|
|
|
|
|
|
|
|
// other classes
|
|
|
|
import { LogScheduler } from "./elasticlog.classes.logscheduler";
|
2018-01-24 23:38:07 +00:00
|
|
|
|
|
|
|
export interface LogContext {
|
2018-01-27 18:16:05 +00:00
|
|
|
zone?: string;
|
|
|
|
containerName?: string;
|
|
|
|
environment: Environment;
|
2018-01-24 23:38:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 18:16:05 +00:00
|
|
|
export type TLogSeverity = "log" | "info" | "warn" | "error" | "fatal";
|
2018-01-24 23:38:07 +00:00
|
|
|
|
|
|
|
export interface IStandardLogParams {
|
2018-01-27 18:16:05 +00:00
|
|
|
message: string;
|
|
|
|
severity: string;
|
2018-01-24 23:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface IElasticLogConstructorOptions {
|
2018-01-27 18:16:05 +00:00
|
|
|
port: number;
|
|
|
|
domain: string;
|
|
|
|
ssl: boolean;
|
|
|
|
user?: string;
|
|
|
|
pass?: string;
|
|
|
|
logContext: LogContext;
|
2018-01-24 23:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ElasticLog<T> {
|
2018-01-27 18:16:05 +00:00
|
|
|
client: ElasticClient;
|
|
|
|
logContext: LogContext;
|
|
|
|
logScheduler = new LogScheduler(this);
|
2018-01-24 23:38:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* sets up an instance of Elastic log
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
2018-01-27 18:16:05 +00:00
|
|
|
constructor(optionsArg: IElasticLogConstructorOptions) {
|
|
|
|
this.logContext = optionsArg.logContext;
|
2018-01-24 23:38:07 +00:00
|
|
|
this.client = new ElasticClient({
|
|
|
|
host: this.computeHostString(optionsArg),
|
2018-01-27 18:16:05 +00:00
|
|
|
log: "trace"
|
2018-01-24 23:38:07 +00:00
|
|
|
});
|
2018-01-27 18:16:05 +00:00
|
|
|
}
|
2018-01-24 23:38:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* computes the host string from the constructor options
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
|
|
|
private computeHostString(optionsArg: IElasticLogConstructorOptions): string {
|
|
|
|
let hostString = `${optionsArg.domain}:${optionsArg.port}`;
|
2018-01-27 18:16:05 +00:00
|
|
|
if (optionsArg.user && optionsArg.pass) {
|
|
|
|
hostString = `${optionsArg.user}:${optionsArg.pass}@${hostString}`;
|
2018-01-24 23:38:07 +00:00
|
|
|
}
|
2018-01-27 18:16:05 +00:00
|
|
|
if (optionsArg.ssl) {
|
|
|
|
hostString = `https://${hostString}`;
|
2018-01-24 23:38:07 +00:00
|
|
|
} else {
|
2018-01-27 18:16:05 +00:00
|
|
|
hostString = `http://${hostString}`;
|
2018-01-24 23:38:07 +00:00
|
|
|
}
|
|
|
|
return hostString;
|
|
|
|
}
|
|
|
|
|
2018-01-27 18:16:05 +00:00
|
|
|
async log(logObject: IStandardLogParams, scheduleOverwrite = false) {
|
|
|
|
const now = new Date();
|
|
|
|
if (this.logScheduler.logsScheduled && !scheduleOverwrite) {
|
|
|
|
this.logScheduler.scheduleLog(logObject);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2018-01-27 18:16:05 +00:00
|
|
|
);
|
2018-01-24 23:38:07 +00:00
|
|
|
}
|
2018-01-27 18:16:05 +00:00
|
|
|
}
|