Skip to content

Commit

Permalink
observability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloSzx committed Aug 15, 2022
1 parent b77de7d commit c4d22cc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-moose-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@soundxyz/fine-grained-cache": minor
---

logEvents.events accepts custom log functions for specific events
5 changes: 5 additions & 0 deletions .changeset/soft-games-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@soundxyz/fine-grained-cache": patch
---

logEvents.log is now optional (console.log default fallback)
27 changes: 19 additions & 8 deletions src/fineGrained.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ export type EventParamsObject = Record<string, string | number | boolean | null

export type LogEventArgs = { message: string; code: Events; params: EventParamsObject };

export type LoggedEvents = Partial<Record<Events, string | boolean | null>>;
export type LoggedEvents = Partial<
Record<Events, string | boolean | null | ((args: LogEventArgs) => void)>
>;

function defaultLog({ message }: LogEventArgs) {
console.log(message);
}

export function FineGrainedCache({
redis,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = "";

Expand All @@ -179,9 +190,9 @@ export function FineGrainedCache({
paramsString += " " + key + "=" + value;
}

logEvents.log({
log({
code,
message: `[${codeValue}]${paramsString}`,
message: `[${codeMessageValue}]${paramsString}`,
params,
});
}
Expand Down

0 comments on commit c4d22cc

Please sign in to comment.