Skip to content

Commit 2cff58b

Browse files
committed
feat(logger): Implement configurable and colorful logging
This commit introduces a new logging utility to the microstream-client SDK, allowing developers to control the verbosity of logs using a configurable logLevel option. The logLevel supports the following levels: "debug", "info", "warn", "error", and "silent". Additionally, logs are now color-coded based on their type for better readability. Changes include: - Creation of a logger.ts file in src/utils/ with methods for debug, info, warn, and error, respecting the logLevel setting. - Addition of a logLevel option to the MicrostreamClientOptions interface. - Initialization of the logger in the MicrostreamClient constructor using the provided logLevel. - Replacement of all console.log, console.warn, and console.error calls with the appropriate logger methods. - Color-coding of log messages using the chalk library.
1 parent 4506ce3 commit 2cff58b

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

bun.lockb

0 Bytes
Binary file not shown.

package-lock.json

Lines changed: 7 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"author": "Arijit Banerjee <[email protected]>",
3333
"license": "MIT",
3434
"dependencies": {
35+
"chalk": "^4.1.2",
3536
"nanoid": "^4.0.0",
3637
"socket.io-client": "^4.5.0"
3738
},
@@ -40,4 +41,4 @@
4041
"jest": "^29.0.0",
4142
"typescript": "^5.7.3"
4243
}
43-
}
44+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export class MicrostreamClient {
189189
`[${this.serviceName}] Starting heartbeat with interval ${this.heartbeatInterval}ms`
190190
);
191191
this.heartbeatTimer = setInterval(() => {
192-
this.logger.debug(`[${this.serviceName}] Sending heartbeat}`);
192+
this.logger.debug(`[${this.serviceName}] Sending heartbeat`);
193193
this.socket.emit("heartbeat", { serviceName: this.serviceName });
194194
}, this.heartbeatInterval);
195195
}

src/utils/logger.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import chalk from "chalk";
2+
13
export type LogLevel = "debug" | "info" | "warn" | "error" | "silent";
24

35
export default class Logger {
@@ -12,11 +14,34 @@ export default class Logger {
1214
return levels.indexOf(level) >= levels.indexOf(this.logLevel);
1315
}
1416

15-
private formatMessage(level: string, ...args: any[]): string {
16-
const timestamp = new Date().toISOString();
17-
return `[MicroStream Client][${timestamp}][${level.toUpperCase()}] ${args.join(
18-
" "
19-
)}`;
17+
private formatMessage(level: LogLevel, ...args: any[]): string {
18+
if (level === "silent") {
19+
return ""; // Skip formatting for "silent" logs
20+
}
21+
22+
const prefix = chalk.magenta(`[MicroStream Client]`);
23+
const timestamp = chalk.gray(`[${new Date().toISOString()}]`);
24+
25+
// Define levelLabel with a default value
26+
const levelLabel =
27+
{
28+
debug: chalk.greenBright,
29+
info: chalk.cyan,
30+
warn: chalk.yellow,
31+
error: chalk.red,
32+
}[level] || chalk.white; // Default to white if level is invalid
33+
34+
const formattedLevel = levelLabel(`[${level.toUpperCase()}]`);
35+
const message = args
36+
.map((arg) =>
37+
arg instanceof Error
38+
? arg.stack
39+
: typeof arg === "object"
40+
? JSON.stringify(arg, null, 2)
41+
: arg
42+
)
43+
.join(" ");
44+
return `${prefix}${timestamp}${formattedLevel} ${message}`;
2045
}
2146

2247
debug(...args: any[]) {

0 commit comments

Comments
 (0)