-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (82 loc) · 2.3 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
- Create Logger using Template Pattern
*/
import { appendFile} from 'fs'
class Logger {
log(level,msg){
switch(level.toUpperCase()){
case("DEBUG"):
this._debug(msg)
break
case("INFO"):
this._info(msg)
break
case("WARN"):
this._warn(msg)
break
case("ERROR"):
this._error(msg)
break
default:
this._warn(msg)
break
}
}
_debug(msg){
throw new Error('method must be implemented')
}
_info(msg){
throw new Error('method must be implemented')
}
_warn(msg){
throw new Error('method must be implemented')
}
_error(msg){
throw new Error('method must be implemented')
}
}
class ConsoleLogger extends Logger {
_debug(msg){
console.log(`${new Date()} : DEBUG : ${msg}`)
}
_info(msg){
console.log(`${new Date()} : INFO : ${msg}`)
}
_warn(msg){
console.log(`${new Date()} : WARN : ${msg}`)
}
_error(msg){
console.log(`${new Date()} : ERROR : ${msg}`)
}
}
class FileLogger extends Logger {
_debug(msg){
appendFile("log.txt",`${new Date()} : DEBUG : ${msg}\n`, err => {if (err ) throw err})
console.log("DEBUG: Logged in file")
}
_info(msg){
appendFile("log.txt",`${new Date()} : DEBUG : ${msg}\n`, err => {if (err ) throw err})
console.log("INFO: Logged in file")
}
_warn(msg){
appendFile("log.txt",`${new Date()} : DEBUG : ${msg}\n`, err => {if (err ) throw err})
console.log("WARN: Logged in file")
}
_error(msg){
appendFile("log.txt",`${new Date()} : DEBUG : ${msg}\n`, err => {if (err ) throw err})
console.log("ERROR: Logged in file")
}
}
function main(){
const consoleLogger = new ConsoleLogger()
const fileLogger = new FileLogger()
consoleLogger.log("DEBUG", "Bad Function")
consoleLogger.log("INFO", "Process Started")
consoleLogger.log("ERROR", "Something Went Wrong")
consoleLogger.log("WARN", "Warning")
fileLogger.log("DEBUG", "Bad Function")
fileLogger.log("INFO", "Process Started")
fileLogger.log("ERROR", "Something Went Wrong")
fileLogger.log("WARN", "Warning")
}
main()