Skip to content

Commit 11130a6

Browse files
authored
Merge pull request #56 from satyam-seth-learnings/custom-logger
Add custom logger demo
2 parents b055a79 + a88addf commit 11130a6

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed

Custom Logger/demo.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Custom Logger</title>
7+
</head>
8+
<body>
9+
<h1>Please check console</h1>
10+
<script src="script.js"></script>
11+
</body>
12+
</html>

Custom Logger/script.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
2+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
3+
if (ar || !(i in from)) {
4+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
5+
ar[i] = from[i];
6+
}
7+
}
8+
return to.concat(ar || Array.prototype.slice.call(from));
9+
};
10+
var Logger = /** @class */ (function () {
11+
function Logger() {
12+
this.logLevels = ["log", "info", "warn", "error"];
13+
this.logLevel = "log";
14+
}
15+
Logger.prototype.setLevel = function (logLevel) {
16+
// Set the log level
17+
this.logLevel = logLevel;
18+
};
19+
Object.defineProperty(Logger, "instance", {
20+
get: function () {
21+
if (Logger._instance === undefined) {
22+
Logger._instance = new Logger();
23+
}
24+
return Logger._instance;
25+
},
26+
enumerable: false,
27+
configurable: true
28+
});
29+
// Helper method to get the current timestamp
30+
Logger.prototype.getTimestamp = function () {
31+
var now = new Date();
32+
return now.toISOString();
33+
};
34+
// Method to check if a message should be logged based on log level
35+
Logger.prototype.shouldLog = function (level) {
36+
var currentLevelIndex = this.logLevels.indexOf(this.logLevel);
37+
var messageLevelIndex = this.logLevels.indexOf(level);
38+
return messageLevelIndex >= currentLevelIndex;
39+
};
40+
// Main log method that accepts multiple arguments
41+
Logger.prototype.log = function () {
42+
var args = [];
43+
for (var _i = 0; _i < arguments.length; _i++) {
44+
args[_i] = arguments[_i];
45+
}
46+
if (this.shouldLog("log")) {
47+
console.log.apply(console, __spreadArray(["%cLOG:",
48+
"color: white; background-color: #405d27", "[".concat(this.getTimestamp(), "]")], args, false));
49+
}
50+
};
51+
// Method to log errors
52+
Logger.prototype.error = function () {
53+
var args = [];
54+
for (var _i = 0; _i < arguments.length; _i++) {
55+
args[_i] = arguments[_i];
56+
}
57+
if (this.shouldLog("error")) {
58+
console.error.apply(console, __spreadArray(["%cERROR:",
59+
"color: white; background-color: #c94c4c", "[".concat(this.getTimestamp(), "]")], args, false));
60+
}
61+
};
62+
// Method to log warnings
63+
Logger.prototype.warn = function () {
64+
var args = [];
65+
for (var _i = 0; _i < arguments.length; _i++) {
66+
args[_i] = arguments[_i];
67+
}
68+
if (this.shouldLog("warn")) {
69+
console.warn.apply(console, __spreadArray(["%cWARNING:",
70+
"color: black; background-color: #feb236", "[".concat(this.getTimestamp(), "]")], args, false));
71+
}
72+
};
73+
// Method to log info
74+
Logger.prototype.info = function () {
75+
var args = [];
76+
for (var _i = 0; _i < arguments.length; _i++) {
77+
args[_i] = arguments[_i];
78+
}
79+
if (this.shouldLog("info")) {
80+
console.info.apply(console, __spreadArray(["%cINFO:",
81+
"color: white; background-color: #4040a1", "[".concat(this.getTimestamp(), "]")], args, false));
82+
}
83+
};
84+
return Logger;
85+
}());
86+
// Example usage of the Logger class
87+
Logger.instance.log("Test value", 5);
88+
// Set the log level to "info"
89+
Logger.instance.setLevel("info");
90+
// This will log a "log" message (will not appear because the log level is "info")
91+
Logger.instance.log("This is a log message that won't be displayed.");
92+
// This will log an "info" message
93+
Logger.instance.info("This is an info message.");
94+
// This will log a "warn" message
95+
Logger.instance.warn("This is a warning message.");
96+
// This will log an "error" message
97+
Logger.instance.error("This is an error message.");
98+
// Change log level to "warn"
99+
Logger.instance.setLevel("warn");
100+
// This will log a "warn" message (will be displayed)
101+
Logger.instance.warn("Another warning message.");
102+
// This will log an "error" message (will be displayed)
103+
Logger.instance.error("Another error message.");
104+
// This will not log an "info" message (will not be displayed)
105+
Logger.instance.info("This info message will not be displayed because the log level is set to warn.");

Custom Logger/script.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
type LogLevel = "error" | "warn" | "info" | "log";
2+
3+
class Logger {
4+
private static _instance?: Logger;
5+
6+
private logLevels: Array<LogLevel> = ["log", "info", "warn", "error"];
7+
8+
private logLevel: LogLevel = "log";
9+
10+
private constructor() {}
11+
12+
setLevel(logLevel: LogLevel) {
13+
// Set the log level
14+
this.logLevel = logLevel;
15+
}
16+
17+
static get instance() {
18+
if (Logger._instance === undefined) {
19+
Logger._instance = new Logger();
20+
}
21+
return Logger._instance;
22+
}
23+
24+
// Helper method to get the current timestamp
25+
private getTimestamp(): string {
26+
const now = new Date();
27+
return now.toISOString();
28+
}
29+
30+
// Method to check if a message should be logged based on log level
31+
private shouldLog(level: LogLevel): boolean {
32+
const currentLevelIndex = this.logLevels.indexOf(this.logLevel);
33+
const messageLevelIndex = this.logLevels.indexOf(level);
34+
return messageLevelIndex >= currentLevelIndex;
35+
}
36+
37+
// Main log method that accepts multiple arguments
38+
public log(...args: unknown[]): void {
39+
if (this.shouldLog("log")) {
40+
console.log(
41+
"%cLOG:",
42+
"color: white; background-color: #405d27",
43+
`[${this.getTimestamp()}]`,
44+
...args
45+
);
46+
}
47+
}
48+
49+
// Method to log errors
50+
public error(...args: unknown[]): void {
51+
if (this.shouldLog("error")) {
52+
console.error(
53+
"%cERROR:",
54+
"color: white; background-color: #c94c4c",
55+
`[${this.getTimestamp()}]`,
56+
...args
57+
);
58+
}
59+
}
60+
61+
// Method to log warnings
62+
public warn(...args: unknown[]): void {
63+
if (this.shouldLog("warn")) {
64+
console.warn(
65+
"%cWARNING:",
66+
"color: black; background-color: #feb236",
67+
`[${this.getTimestamp()}]`,
68+
...args
69+
);
70+
}
71+
}
72+
73+
// Method to log info
74+
public info(...args: unknown[]): void {
75+
if (this.shouldLog("info")) {
76+
console.info(
77+
"%cINFO:",
78+
"color: white; background-color: #4040a1",
79+
`[${this.getTimestamp()}]`,
80+
...args
81+
);
82+
}
83+
}
84+
}
85+
86+
// Example usage of the Logger class
87+
Logger.instance.log("Test value", 5);
88+
89+
// Set the log level to "info"
90+
Logger.instance.setLevel("info");
91+
92+
// This will log a "log" message (will not appear because the log level is "info")
93+
Logger.instance.log("This is a log message that won't be displayed.");
94+
95+
// This will log an "info" message
96+
Logger.instance.info("This is an info message.");
97+
98+
// This will log a "warn" message
99+
Logger.instance.warn("This is a warning message.");
100+
101+
// This will log an "error" message
102+
Logger.instance.error("This is an error message.");
103+
104+
// Change log level to "warn"
105+
Logger.instance.setLevel("warn");
106+
107+
// This will log a "warn" message (will be displayed)
108+
Logger.instance.warn("Another warning message.");
109+
110+
// This will log an "error" message (will be displayed)
111+
Logger.instance.error("Another error message.");
112+
113+
// This will not log an "info" message (will not be displayed)
114+
Logger.instance.info(
115+
"This info message will not be displayed because the log level is set to warn."
116+
);

0 commit comments

Comments
 (0)