I am trying to make logger class with debug, info, warn and error methods that "extends" basic log method.
However it is not written using DRY principle, how can I make it DRY, not WET?
Code snippet
const logLevels = {
0: 'debug',
1: 'info',
2: 'warn',
3: 'error',
'debug': 0,
'info': 1,
'warn': 2,
'error': 3,
}
class Logger {
logLevel = logLevels.debug
log(logLevel, ...messages) {
if (logLevel >= this.logLevel) {
const logType = logLevels[logLevel]
messages.unshift(logType)
console.log(...messages)
}
}
debug(...messages) {
this.log(logLevels.debug, ...messages)
}
info(...messages) {
this.log(logLevels.info, ...messages)
}
warn(...messages) {
this.log(logLevels.warn, ...messages)
}
error(...messages) {
this.log(logLevels.error, ...messages)
}
}
const myLogger = new Logger()
const dateNowHex = () => Date.now().toString(16)
myLogger.debug('this is a test', dateNowHex())
myLogger.info('this is a test', dateNowHex())
myLogger.warn('this is a test', dateNowHex())
myLogger.error('this is a test', dateNowHex())
Should I make better class composition? Can you provide examples?