Last active
April 23, 2022 07:19
-
-
Save dipenparmar12/017aff517e2de1840638cca3c80f0d52 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 winston = require('winston'); | |
const { format } = require('winston'); | |
const appRoot = require('app-root-path'); | |
const config = require('./config'); | |
const printf = (info) => { | |
if (typeof info.message === 'object') { | |
// eslint-disable-next-line no-param-reassign | |
info.message = JSON.stringify(info.message, null, 3); | |
} | |
return `[${info.timestamp}] ${info.level}:: ${info.message}`; | |
}; | |
const options = { | |
console: { | |
level: 'debug', | |
handleExceptions: true, | |
json: false, | |
colorize: true, | |
stderrLevels: ['error'], | |
format: format.combine( | |
format.prettyPrint(), | |
format.splat(), | |
format.colorize(), | |
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), | |
format.printf(printf) | |
// format.printf((info) => `${info.timestamp} ${info.level}: ${info.message}`), | |
), | |
}, | |
file: { | |
level: 'info', | |
filename: `${appRoot}/logs/${new Date().toISOString().slice(0, 10)}.log`, // Date=YYYY-MM-DD | |
handleExceptions: true, | |
json: false, | |
maxsize: 5242880, // 5MB | |
maxFiles: 30, | |
colorize: false, | |
format: format.combine( | |
format.prettyPrint(), | |
format.splat(), | |
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), | |
format.printf(printf) | |
// format.printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`), | |
), | |
}, | |
email: { | |
level: 'info', | |
filename: `${appRoot}/logs/mail/mail-${new Date().toISOString().slice(0, 10)}.log`, // Date=YYYY-MM-DD | |
handleExceptions: true, | |
json: true, | |
maxsize: 5242880, // 5MB | |
maxFiles: 365, | |
colorize: false, | |
format: format.combine( | |
format.prettyPrint(), | |
format.splat(), | |
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), | |
format.printf(printf) | |
), | |
}, | |
}; | |
const logger = winston.createLogger({ | |
level: config.env === 'development' ? 'debug' : 'info', | |
transports: [new winston.transports.Console(options.console), new winston.transports.File(options.file)], | |
}); | |
const mailLog = winston.createLogger({ | |
level: config.env === 'development' ? 'debug' : 'info', | |
transports: [new winston.transports.File(options.email)], | |
}); | |
module.exports = logger; | |
module.exports.mailLog = mailLog; | |
/// Usage example | |
// logger.info('Log: info'); | |
// logger.warn('Log: warn'); | |
// logger.debug('Log: debug'); | |
// logger.error('Log: error'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment