55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import * as plugins from './logdna.plugins';
|
|
import { LogdnaAccount } from './logdna.logdnaaccount';
|
|
|
|
export interface ILogCandidate {
|
|
urlIdentifier: string;
|
|
logLines: any[];
|
|
}
|
|
|
|
export class LogAggregator {
|
|
private apiKey: string;
|
|
private baseUrl = 'https://logs.logdna.com/logs/ingest';
|
|
|
|
private logObjectMap = new plugins.lik.Objectmap<ILogCandidate>();
|
|
|
|
constructor(apiKeyArg: string) {
|
|
this.apiKey = apiKeyArg;
|
|
}
|
|
|
|
/**
|
|
* Create basic authentication
|
|
*/
|
|
private createBasicAuth() {
|
|
const userNamePasswordString = `${this.apiKey}:`;
|
|
return `Basic ${plugins.smartstring.base64.encode(userNamePasswordString)}`;
|
|
}
|
|
|
|
public addLog(urlIdentifierArg: string, logLineArg: any) {
|
|
let existinglogCandidate = this.logObjectMap.find(logCandidate => {
|
|
return logCandidate.urlIdentifier === urlIdentifierArg;
|
|
});
|
|
if (!existinglogCandidate) {
|
|
existinglogCandidate = { urlIdentifier: urlIdentifierArg, logLines: [] };
|
|
this.logObjectMap.add(existinglogCandidate);
|
|
setTimeout(() => {
|
|
this.sendAggregatedLogs(existinglogCandidate);
|
|
}, 500);
|
|
}
|
|
existinglogCandidate.logLines.push(logLineArg);
|
|
}
|
|
|
|
private async sendAggregatedLogs(logCandidate: ILogCandidate) {
|
|
this.logObjectMap.remove(logCandidate);
|
|
// lets post the message to logdna
|
|
await plugins.smartrequest.postJson(`${this.baseUrl}${logCandidate.urlIdentifier}&now=${Date.now()}` , {
|
|
headers: {
|
|
Authorization: this.createBasicAuth(),
|
|
charset: 'UTF-8'
|
|
},
|
|
requestBody: {
|
|
lines: logCandidate.logLines
|
|
}
|
|
});
|
|
}
|
|
}
|