logdna/ts/logdna.logdnaaccount.ts

73 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2018-11-01 17:13:34 +00:00
import * as plugins from './logdna.plugins';
import { LogdnaMessage } from './logdna.classes.logmessage';
2018-11-04 20:11:48 +00:00
import { LogAggregator } from './logdna.aggregator';
import { ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces';
2018-11-01 17:13:34 +00:00
/**
* the main logdna account
*/
export class LogdnaAccount {
2018-11-04 20:11:48 +00:00
private logAggregator: LogAggregator;
2018-11-01 17:13:34 +00:00
/**
*
* @param apiKeyArg
*/
constructor(apiKeyArg: string) {
2018-11-04 20:11:48 +00:00
this.logAggregator = new LogAggregator(apiKeyArg);
2018-11-01 17:13:34 +00:00
}
/**
* sends a logmessage
* async, so it returns a Promise. In most cases it does not make sense to wait for it though.
*/
2018-11-05 22:52:39 +00:00
public async sendLogdnaMessage(logdnaMessageArg: LogdnaMessage) {
2018-11-01 17:13:34 +00:00
const lm = logdnaMessageArg;
const euc = encodeURIComponent;
2018-11-02 18:12:16 +00:00
2018-11-05 22:25:53 +00:00
const uriHostname = euc(lm.options.hostname);
const uriMac = euc(lm.options.mac);
const uriIp = euc(lm.options.ip);
const uriTags = euc(
2018-11-04 17:44:28 +00:00
(() => {
return lm.options.tags.reduce((reduced, newItem) => {
return `${reduced},${newItem}`;
});
})()
2018-11-05 22:25:53 +00:00
);
// let construct the request uri
const requestUrlWithParams = `?hostname=${uriHostname}&mac=${uriMac}&ip=1${uriIp}&tags=${uriTags}`;
2018-11-04 20:11:48 +00:00
const logLine = {
timestamp: lm.options.timestamp,
line: lm.options.line,
app: lm.options.app,
level: lm.options.level,
env: lm.options.env,
meta: lm.options.meta
2018-11-02 18:12:16 +00:00
};
2018-11-04 20:11:48 +00:00
this.logAggregator.addLog(requestUrlWithParams, logLine);
2018-11-01 17:13:34 +00:00
}
2018-11-03 22:31:26 +00:00
/**
* convenience function for smartlog
*/
2018-11-04 17:44:28 +00:00
public async sendSmartlogPackage(smartlogPackageArg: ILogPackage) {
2018-11-05 22:52:39 +00:00
this.sendLogdnaMessage(LogdnaMessage.fromSmartLogPackage(smartlogPackageArg));
2018-11-03 22:31:26 +00:00
}
/**
* returns a smartlog compatible log destination
*/
2018-11-04 17:44:28 +00:00
public get smartlogDestination(): ILogDestination {
2018-11-04 14:25:27 +00:00
return {
2018-11-04 17:44:28 +00:00
handleLog: logPackageArg => {
2018-11-04 14:25:27 +00:00
this.sendSmartlogPackage(logPackageArg);
}
};
}
2018-11-01 17:13:34 +00:00
}