diff --git a/.changeset/silver-moose-wave.md b/.changeset/silver-moose-wave.md new file mode 100644 index 0000000..51e728c --- /dev/null +++ b/.changeset/silver-moose-wave.md @@ -0,0 +1,5 @@ +--- +"@soundxyz/fine-grained-cache": minor +--- + +logEvents.events accepts custom log functions for specific events diff --git a/.changeset/soft-games-bathe.md b/.changeset/soft-games-bathe.md new file mode 100644 index 0000000..a244fc9 --- /dev/null +++ b/.changeset/soft-games-bathe.md @@ -0,0 +1,5 @@ +--- +"@soundxyz/fine-grained-cache": patch +--- + +logEvents.log is now optional (console.log default fallback) diff --git a/src/fineGrained.ts b/src/fineGrained.ts index dd05ee9..0319522 100644 --- a/src/fineGrained.ts +++ b/src/fineGrained.ts @@ -73,7 +73,13 @@ export type EventParamsObject = Record>; +export type LoggedEvents = Partial< + Record void)> +>; + +function defaultLog({ message }: LogEventArgs) { + console.log(message); +} export function FineGrainedCache({ redis, @@ -108,9 +114,12 @@ export function FineGrainedCache({ * Enable event logging */ logEvents?: { - log: (args: LogEventArgs) => void; - events: LoggedEvents; + + /** + * @default console.log + */ + log?: (args: LogEventArgs) => void; }; /** * Set a maximum amount of milliseconds for getCached to wait for the GET redis response @@ -161,11 +170,13 @@ export function FineGrainedCache({ const logMessage = logEvents ? function logMessage(code: Events, params: EventParamsObject) { - let codeValue = logEvents.events[code]; + const eventValue = logEvents.events[code]; + + if (!eventValue) return; - if (!codeValue) return; + const log = typeof eventValue === "function" ? eventValue : logEvents.log || defaultLog; - if (typeof codeValue !== "string") codeValue = Events[code]; + const codeMessageValue = typeof eventValue === "string" ? eventValue : code; let paramsString = ""; @@ -179,9 +190,9 @@ export function FineGrainedCache({ paramsString += " " + key + "=" + value; } - logEvents.log({ + log({ code, - message: `[${codeValue}]${paramsString}`, + message: `[${codeMessageValue}]${paramsString}`, params, }); }