Last active
December 5, 2019 22:57
-
-
Save ekpangmichael/b257fb3fdac7090779a5a5f9c75a2312 to your computer and use it in GitHub Desktop.
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
const { createLogger, format, transports } = require('winston'); | |
const { combine, timestamp, printf} = format; | |
const { Worker, isMainThread } = require('worker_threads'); | |
const logFormat = printf(({ level, message, timestamp }) => { | |
return `{ "Date": "${timestamp}", "level": "${level.toUpperCase()}", "message": "${message}" }`; | |
}); | |
class Logger { | |
log(message, logType=null) { | |
if (isMainThread && !this.thread) { | |
this.thread = new Worker(__filename); | |
} | |
this.getLogger(message) | |
switch(logType){ | |
case "error": | |
this.getLogger().error(message); | |
break; | |
case "info": | |
this.getLogger().info(message); | |
break; | |
case "warn": | |
this.getLogger().warn(message); | |
break; | |
default: | |
this.getLogger | |
} | |
} | |
getLogger() { | |
if (!this.logger) { | |
if (process.env.NODE_ENV !== 'production') { | |
this.logger = createLogger({ | |
transports: [ | |
new transports.Console({ | |
format: combine( | |
timestamp(), | |
logFormat, | |
), | |
}) | |
] | |
}); | |
} else { | |
this.logger = createLogger({ | |
transports: [ | |
new transports.File({ | |
filename: 'error.log', | |
format: combine( | |
timestamp(), | |
logFormat, | |
), | |
}) | |
] | |
}); | |
} | |
} | |
return this.logger; | |
} | |
} | |
exports.Logger = Logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line 10 needs one indentation
Needs indentation