-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (39 loc) · 1.16 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
/*
- Implement logging with the following methods:
info(), warn(),debug(),error()
- Logger accepts different strategies to define how message is logged
*/
import { appendFile} from 'fs'
class Logger {
constructor(loggingStrategy){
this.loggingStrategy = loggingStrategy
}
debug(msg){
this.loggingStrategy.log("DEBUG",msg)
}
info(msg){
this.loggingStrategy.log("INFO",msg)
}
warn(msg){
this.loggingStrategy.log("WARN",msg)
}
error(msg){
this.loggingStrategy.log("ERROR",msg)
}
}
const consoleStrategy ={
log: (level,msg) => console.log(`${new Date()} : ${level} : ${msg}`)
}
const fileStrategy ={
log: (level,msg) => appendFile("log.txt",`${new Date()} : ${level} : ${msg}\n`, err => {if (err ) throw err})
}
const consoleLogger = new Logger(consoleStrategy)
const fileLogger = new Logger(fileStrategy)
consoleLogger.debug("Bad Function")
consoleLogger.warn("Warning")
consoleLogger.info("Process started")
consoleLogger.error("Something went wrong")
fileLogger.debug("Bad Function")
fileLogger.warn("Warning")
fileLogger.info("Process started")
fileLogger.error("Something went wrong")