Skip to content

Instantly share code, notes, and snippets.

@waqasisme
Created May 3, 2018 18:06
Show Gist options
  • Save waqasisme/dd6d4ae07caa763e725f72ab60887bbd to your computer and use it in GitHub Desktop.
Save waqasisme/dd6d4ae07caa763e725f72ab60887bbd to your computer and use it in GitHub Desktop.
Basic winstonjs logger setup
// Libraries
const util = require('util');
const winston = require('winston');
const dailyRotater = require('winston-daily-rotate-file'); // for daily file rotating
const fs = require('fs');
const path = require('path');
const chalk = require('chalk'); // for colors
const moment = require('moment');
/**
*
*/
class appLogger {
/**
*
*/
constructor() {
// our logs folder
this.logDir = path.resolve(__dirname, 'logs');
const dir = this.logDir;
// Create the logs directory if it does not exist
try {
if (!fs.existsSync(this.logDir)) {
fs.mkdirSync(this.logDir);
}
} catch (e) {
console.log(e);
}
// levels for our logs
const logLevels = {
levels: {
error: 0,
warn: 1,
info: 2,
verbose: 3,
debug: 4,
silly: 5
}
};
const colors = {
error: chalk.red,
warn: chalk.yellow,
info: chalk.green,
verbose: chalk.white,
debug: chalk.blue,
silly: chalk.magenta
};
// timestamp format
const tsFormat = () => moment().format('YYYY-MM-DD HH:mm:ss');
// transports for our logs
const logTransports = [];
// specific options for each transport -- console has a separate format for colors
const consoleOptions = {
format: winston.format.printf(info => colors[info.level](`${tsFormat()} [${info.level.toUpperCase()}]: ${info.message.trim()}`))
};
const debugFileOptions = {
level: 'debug',
filename: 'debug-%DATE%.log',
dirname: dir,
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d'
};
const combinedLogFileOptions = {
level: 'info',
filename: '%DATE%.log',
dirname: dir,
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d'
};
// push transports to offer
logTransports.push(new winston.transports.Console(consoleOptions));
logTransports.push(new dailyRotater(debugFileOptions));
logTransports.push(new dailyRotater(combinedLogFileOptions));
// THE LOGGER
this.logger = winston.createLogger({
levels: logLevels.levels,
level: 'silly',
exitOnError: false,
format: winston.format.printf(info => (`${tsFormat()} [${info.level.toUpperCase()}]: ${info.message.trim()}`)),
transports: logTransports
});
} // end ctor
// expose our logger
getLogger() {
return this.logger;
}
// expose our root log dir
getRoot() {
return this.logDir;
}
}
const logMonitor = new appLogger();
const logger = logMonitor.getLogger();
function formatArgs(args) {
return [util.format.apply(util.format, Array.prototype.slice.call(args))];
}
// replace console stdout with Winston
console.log = function () {
logger.info.apply(logger, formatArgs(arguments));
};
console.info = function () {
logger.info.apply(logger, formatArgs(arguments));
};
console.warn = function () {
logger.warn.apply(logger, formatArgs(arguments));
};
console.error = function () {
logger.error.apply(logger, formatArgs(arguments));
};
console.debug = function () {
logger.debug.apply(logger, formatArgs(arguments));
};
// export our Logger Singleton
module.exports = logger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment