Showing posts with label winston. Show all posts
Showing posts with label winston. Show all posts

Thursday, 16 January 2020

Winston logger with file name and time stamp nodejs

logger.js

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, label, printf } = format;
const path = require('path');

const myFormat = printf(({ level, message, label, timestamp }) => {
  return `${timestamp} [${label}] ${level}: ${message}`;
});
const getLabel = function(callingModule) {
    const parts = callingModule.filename.split('/');
    return path.join(parts[parts.length - 2], parts.pop());
  };

const logger = (callingModule)=> createLogger({
  format: combine(
    label({ label: getLabel(callingModule) }),
    timestamp(),
    myFormat
  ),
  transports: [new transports.Console()]
});

module.exports = {
    logger:logger
}

use as below:

const logger = require('../util/logger').logger(module);
logger.info('logger file..');

//util/logger is my file path of logger.js

output:

2020-01-16T08:28:41.893Z [controller/shop.js] info: logger file..

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...