From 04b8336a75bff51f43840fac8ce2a32c94719850 Mon Sep 17 00:00:00 2001 From: Damiano Date: Fri, 23 Feb 2024 16:11:07 +0100 Subject: [PATCH] fix: Logger instance --- lib/LogFlake.ts | 6 ++++-- lib/index.ts | 2 +- lib/init.ts | 12 +++++++++++- tests/init.test.ts | 4 ++-- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/LogFlake.ts b/lib/LogFlake.ts index 31a9d69..78fcd75 100644 --- a/lib/LogFlake.ts +++ b/lib/LogFlake.ts @@ -67,10 +67,12 @@ export class LogFlake { } public async sendException(exception: E, options?: Pick) { + const { stack, message, ...params } = exception + return this.post(Queue.LOGS, { - content: exception.stack ?? exception.message ?? "Unknown error", + content: stack ?? message ?? "Unknown error", level: LogLevels.EXCEPTION, - params: exception, + params, hostname: this.hostname, ...options, }) diff --git a/lib/index.ts b/lib/index.ts index 716645a..1dc3a29 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,4 +1,4 @@ import "whatwg-fetch" -export * from "./LogFlake" +export { LogFlake } from "./LogFlake" export * from "./init" diff --git a/lib/init.ts b/lib/init.ts index 65bee35..fc17bce 100644 --- a/lib/init.ts +++ b/lib/init.ts @@ -1,12 +1,22 @@ import { LogFlake } from "./LogFlake" +import { IBodyLog } from "./types" -export let Logger: LogFlake | null = null +let Logger: LogFlake | null = null interface IInitOptions { hostname?: string enableCompression?: boolean } +export const getLogger = () => { + return Logger +} + +export const sendLog = (content: string, options?: Omit) => getLogger()?.sendLog(content, options) +export const sendException = (error: Error, options?: Pick) => getLogger()?.sendException(error, options) +export const measurePerformance = (label: string) => getLogger()?.measurePerformance(label) +export const sendPerformance = (label: string, duration: number) => getLogger()?.sendPerformance(label, duration) + export function initialize(appId: string, server?: string, options?: IInitOptions) { if (Logger !== null) { throw new Error("LogFlake is already initialized, if you want to initialize a new instance, use the LogFlake class directly.") diff --git a/tests/init.test.ts b/tests/init.test.ts index 505eef4..1580436 100644 --- a/tests/init.test.ts +++ b/tests/init.test.ts @@ -1,4 +1,4 @@ -import { Logger, initialize } from "../lib/init" +import { getLogger, initialize } from "../lib/init" describe("initialize", () => { expect(process.env.APP_ID).toBeDefined() @@ -15,6 +15,6 @@ describe("initialize", () => { expect(logInstance?.appId).toBe(APP_ID) // @ts-expect-error expect(logInstance?.server).toBe(SERVER) - expect(Logger).toBe(logInstance) + expect(getLogger()).toBe(logInstance) }) })