Skip to content

Commit

Permalink
improvements on logging (#12)
Browse files Browse the repository at this point in the history
* Remove all decimals from tracing values

* New "customizeEventCodes" to be able to change or disable specified events logs

* include subscribers size on abort

* fix coverage
  • Loading branch information
PabloSzx authored Jul 28, 2022
1 parent 589146a commit 33c49d4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/two-apples-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@soundxyz/redis-pubsub": minor
---

New "customizeEventCodes" to be able to change or disable specified events logs
5 changes: 5 additions & 0 deletions .changeset/warm-falcons-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@soundxyz/redis-pubsub": patch
---

Remove all decimals from tracing values
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export interface RedisPubSubOptions {
*/
logLevel?: LogLevel;
onParseError?: (err: unknown) => void;
/**
* Customize or disable the specified event codes messages
*/
customizeEventCodes?: Partial<Record<EventCodes, string | boolean | null>>;
}

export const EventCodes = {
Expand Down Expand Up @@ -47,9 +51,12 @@ export function RedisPubSub({
logger,
logLevel = "silent",
onParseError = logger.error,
customizeEventCodes,
}: RedisPubSubOptions) {
const intLogLevel = logLevel === "silent" ? 0 : logLevel === "info" ? 1 : 2;

const customizedEventCodes = { ...EventCodes, ...customizeEventCodes };

interface DataPromise {
current: PubSubDeferredPromise<unknown>;
unsubscribe: () => Promise<void>;
Expand Down Expand Up @@ -81,10 +88,16 @@ export function RedisPubSub({

const start = performance.now();

return () => `${performance.now() - start}ms`;
return () => `${(performance.now() - start).toFixed()}ms`;
}

function logMessage(code: EventCodes, paramsObject: Record<string, string | number>) {
let codeValue = customizedEventCodes[code];

if (!codeValue) return;

if (typeof codeValue !== "string") codeValue = EventCodes[code];

let params = "";

for (const key in paramsObject) {
Expand Down Expand Up @@ -342,6 +355,7 @@ export function RedisPubSub({
if (intLogLevel) {
logMessage("SUBSCRIPTION_ABORTED", {
channel,
subscribers: dataPromises.size,
});
}
unsubscribe().catch((err) => subscriptionValue.ready.reject(err));
Expand Down
4 changes: 4 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,5 +521,9 @@ test(
"logLevel=info subscribe/unsubscribe and abort controller",
baseTest({
logLevel: "info",
customizeEventCodes: {
PUBLISH_MESSAGE: true,
SUBSCRIBE_REDIS: false,
},
})
);

0 comments on commit 33c49d4

Please sign in to comment.