logtail/ts/index.ts

38 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-07-06 19:30:07 +00:00
import * as plugins from './logtail.plugins';
export class LogTailAccount {
private token: string;
constructor(logTailToken: string) {
this.token = logTailToken;
}
async log(logPackage: plugins.smartlogInterfaces.ILogPackage<{[key: string]: any}>) {
const requestBody = {
dt: new Date(logPackage.timestamp).toISOString(),
level: logPackage.level,
2021-07-06 22:06:12 +00:00
message: `${logPackage.context?.containerName}: ${logPackage.message}`,
originalMessage: logPackage.message,
2021-07-06 19:30:07 +00:00
context: logPackage.context,
correlation: logPackage.correlation,
data: logPackage.data,
type: logPackage.type
};
const response = await plugins.smartrequest.request('https://in.logtail.com', {
method: 'post',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.token}`
},
requestBody,
keepAlive: true,
});
}
public get smartlogDestination(): plugins.smartlogInterfaces.ILogDestination {
return {
handleLog: async (logPackage: plugins.smartlogInterfaces.ILogPackage) => {
await this.log(logPackage);
}
}
}
}