Created
August 23, 2020 10:41
-
-
Save edharmowongso/fd5cee8aa944ddf35cbaad96f09e0bbb to your computer and use it in GitHub Desktop.
Library utils (logger.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Located at app/lib/logger.js | |
const { log, error, warn } = console | |
class Log { | |
constructor(message) { | |
this.message = message | |
} | |
Write() { | |
throw new Error('Not Implemented') | |
} | |
} | |
class ErrorLog extends Log { | |
Write() { | |
const response = `Error: ${this.message}` | |
error(response) | |
return response | |
} | |
} | |
class InfoLog extends Log { | |
Write() { | |
const response = `Info: ${this.message}` | |
log(response) | |
return response | |
} | |
} | |
class WarningLog extends Log { | |
Write() { | |
const response = `Warning: ${this.message}` | |
warn(response) | |
return response | |
} | |
} | |
const Write = (type, message) => { | |
const writingLogType = { | |
error: new ErrorLog(message), | |
warning: new WarningLog(message), | |
info: new InfoLog(message) | |
} | |
if (!writingLogType[type]) { | |
throw new Error('Invalid log type!') | |
} | |
return writingLogType[type].Write() | |
} | |
module.exports = { | |
WriteLog: Write | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment