logdna/ts/logdna.logdnaaccount.ts

71 lines
1.8 KiB
TypeScript
Raw 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.
*/
public async sendLogDnaMessage(logdnaMessageArg: LogdnaMessage) {
const lm = logdnaMessageArg;
const euc = encodeURIComponent;
2018-11-02 18:12:16 +00:00
2018-11-02 12:57:14 +00:00
// let construct the request uri
2018-11-04 20:11:48 +00:00
const requestUrlWithParams = `?hostname=${euc(lm.options.hostname)}&mac=${euc(
2018-11-02 18:12:16 +00:00
lm.options.mac
2018-11-04 20:11:48 +00:00
)}&ip=1${euc(lm.options.ip)}&tags=${euc(
2018-11-04 17:44:28 +00:00
(() => {
return lm.options.tags.reduce((reduced, newItem) => {
return `${reduced},${newItem}`;
});
})()
)}`;
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-03 22:31:26 +00:00
this.sendLogDnaMessage(LogdnaMessage.fromSmartLogPackage(smartlogPackageArg));
}
/**
* 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
}