'Displaying objects in logs

I am trying to log objects in my logs, in somewhat readable format.

This is currently what I am working with:

const { format, createLogger, transports } = require("winston");

const { combine, timestamp, printf, errors, splat, simple } = format;

const LEVEL = Symbol.for("level");

const customLevels = {
  levels: {
    trade: 0,
    purchase: 1,
    selling: 2,
    withdraw: 3,
    priceAdjust: 4,
  },
};

const logFormat = printf(({ message, timestamp }) => {
  return `${timestamp} ${message}`;
});

function filterOnly(level) {
  return format(function (info) {
    if (info[LEVEL] === level) {
      return info;
    }
  })();
}

const logger = createLogger({
  format: combine(errors({ stack: true }), timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), logFormat, splat(),),
  levels: customLevels.levels,
  transports: [
    new transports.File({ level: "trade", format: filterOnly("trade"), filename: "logs/trade.log" }),
    new transports.File({ level: "withdraw", format: filterOnly("withdraw"), filename: "logs/withdraw.log" }),
  ],
});

if (process.env.NODE_ENV !== "production") {
  logger.add(
    new winston.transports.Console({
      format: winston.format.simple(),
    })
  );
}

if I pass something as:

logger.trade("Doing trade:", {some object});

I get output in the logs only as:

2022-05-15 11:49:44 Doing trade:

What I am missing to see the object as well?

I have tried, using %O, %j utils, before object, but then getting only

2022-05-15 11:49:44 Doing trade %O:

I was able to display objects by using format.simple(), but then it becomes not really nice readable format.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source