const { createLogger, format, transports } = require('winston'); const { combine, timestamp, json } = format; const getLogFileName = (category) => { const date = new Date().toISOString().slice(0, 10); return `logs/${category}/${category}-${date}.log`; }; const createCategoryLogger = (category) => { return createLogger({ level: 'info', format: combine(timestamp(), json()), transports: [ new transports.File({ filename: getLogFileName(category), level: 'info', maxsize: 1024 * 1024 * 10, // 10MB maxFiles: 5, tailable: true, }), new transports.Console({ level: 'debug', format: format.combine(format.colorize(), format.simple()), }), ], }); }; module.exports = createCategoryLogger;
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter