Files
elasticsearch/ts/elasticlog.classes.elasticlog.ts

86 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-01-25 00:38:07 +01:00
// interfaces
2018-08-12 16:42:09 +02:00
import { Client as ElasticClient } from 'elasticsearch';
2018-11-03 23:45:21 +01:00
import { ILogContext, ILogPackage } from '@pushrocks/smartlog-interfaces';
2018-01-27 19:16:05 +01:00
// other classes
2018-08-12 16:42:09 +02:00
import { LogScheduler } from './elasticlog.classes.logscheduler';
2018-01-25 00:38:07 +01:00
export interface IStandardLogParams {
2018-01-27 19:16:05 +01:00
message: string;
severity: string;
2018-01-25 00:38:07 +01:00
}
export interface IElasticLogConstructorOptions {
2018-01-27 19:16:05 +01:00
port: number;
domain: string;
ssl: boolean;
user?: string;
pass?: string;
2018-01-25 00:38:07 +01:00
}
export class ElasticLog<T> {
2018-01-27 19:16:05 +01:00
client: ElasticClient;
logScheduler = new LogScheduler(this);
2018-01-25 00:38:07 +01:00
/**
* sets up an instance of Elastic log
* @param optionsArg
*/
2018-01-27 19:16:05 +01:00
constructor(optionsArg: IElasticLogConstructorOptions) {
2018-01-25 00:38:07 +01:00
this.client = new ElasticClient({
host: this.computeHostString(optionsArg),
2018-08-12 16:42:09 +02:00
log: 'trace'
2018-01-25 00:38:07 +01:00
});
2018-01-27 19:16:05 +01:00
}
2018-01-25 00:38:07 +01: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 19:16:05 +01:00
if (optionsArg.user && optionsArg.pass) {
hostString = `${optionsArg.user}:${optionsArg.pass}@${hostString}`;
2018-01-25 00:38:07 +01:00
}
2018-01-27 19:16:05 +01:00
if (optionsArg.ssl) {
hostString = `https://${hostString}`;
2018-01-25 00:38:07 +01:00
} else {
2018-01-27 19:16:05 +01:00
hostString = `http://${hostString}`;
2018-01-25 00:38:07 +01:00
}
return hostString;
}
2018-11-03 23:45:21 +01:00
public async log(logPackageArg: ILogPackage, scheduleOverwrite = false) {
2018-01-27 19:16:05 +01:00
const now = new Date();
if (this.logScheduler.logsScheduled && !scheduleOverwrite) {
2018-11-03 23:45:21 +01:00
this.logScheduler.scheduleLog(logPackageArg);
2018-01-27 19:16:05 +01:00
return;
}
this.client.index(
{
index: `logstash-${now.getFullYear()}.${('0' + (now.getMonth() + 1)).slice(-2)}.${(
'0' + now.getDate()
).slice(-2)}`,
2018-08-12 16:42:09 +02:00
type: 'log',
2018-01-27 19:16:05 +01:00
body: {
2018-08-12 16:42:09 +02:00
'@timestamp': now.toISOString(),
2018-11-03 23:45:21 +01:00
zone: logPackageArg.context.zone,
container: logPackageArg.context.containerName,
environment: logPackageArg.context.environment,
severity: logPackageArg.level,
message: logPackageArg.message
2018-01-27 19:16:05 +01:00
}
},
(error, response) => {
if (error) {
2018-08-12 16:42:09 +02:00
console.log('ElasticLog encountered an error:');
2018-01-27 19:16:05 +01:00
console.log(error);
2018-11-03 23:45:21 +01:00
this.logScheduler.addFailedLog(logPackageArg);
2018-01-27 19:16:05 +01:00
} else {
2018-11-03 23:45:21 +01:00
console.log(`ElasticLog: ${logPackageArg.message}`);
2018-01-27 19:16:05 +01:00
}
2018-01-25 00:38:07 +01:00
}
2018-01-27 19:16:05 +01:00
);
2018-01-25 00:38:07 +01:00
}
2018-01-27 19:16:05 +01:00
}