-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (58 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"use strict";
const fs = require("fs");
const createLoggingComponent = (loggingStrategy) => ({
debug(msg) {
loggingStrategy.debug(msg);
},
info(msg) {
loggingStrategy.info(msg);
},
warn(msg) {
loggingStrategy.warn(msg);
},
error(msg) {
loggingStrategy.error(msg);
},
});
class ConsoleStrategy {
constructor() {}
debug(msg) {
console.debug(msg);
}
info(msg) {
console.info(msg);
}
warn(msg) {
console.warn(msg);
}
error(msg) {
console.error(msg);
}
}
class FileStrategy {
#ws = null;
constructor(filename) {
this.filename = filename;
this.#ws = fs.createWriteStream("./log.txt", { flags: "a" });
}
debug(msg) {
this.#ws.write("DEBUG " + msg + "\n");
}
info(msg) {
this.#ws.write("INFO " + msg + "\n");
}
warn(msg) {
this.#ws.write("WARN " + msg + "\n");
}
error(msg) {
this.#ws.write("ERROR " + msg + "\n");
}
}
let loggingComponent = createLoggingComponent(new ConsoleStrategy());
loggingComponent.info("This is written in console");
loggingComponent.debug("This is written in console");
loggingComponent.warn("This is written in console");
loggingComponent = createLoggingComponent(new FileStrategy());
loggingComponent.info("This is written in file");
loggingComponent.debug("This is written in file");
loggingComponent.warn("This is written in file");