Skip to content

Instantly share code, notes, and snippets.

@ekpangmichael
Last active December 5, 2019 22:57
Show Gist options
  • Save ekpangmichael/b257fb3fdac7090779a5a5f9c75a2312 to your computer and use it in GitHub Desktop.
Save ekpangmichael/b257fb3fdac7090779a5a5f9c75a2312 to your computer and use it in GitHub Desktop.
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;
@Danielshow
Copy link

line 10 needs one indentation

this.logger = createLogger({
        transports: [
          new transports.Console({
            format: combine(
              timestamp(),
              logFormat,
            ),
          })
        ]
      });

Needs indentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment