1

I'm using winston as a logger and in it you have a format namespace and function at the same time, so i can both call format() and access other functions via the namespace format.someFunction() as illustrated in the code below. Now i'm wondering how i can create an object like this myself?

const isRequest = format((info, _opts) => {
    if (info.isRequest) {
        return info
    }
    return false
})

export const logger = createLogger({
    level: 'info',
    format: format.combine(
        format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss'
        }),
        format.printf((info) => {
            return `${info.timestamp} ${info.level}: ${info.message}`
        })
    ),
    transports: [
        new transports.File({
            filename: errorLog,
            level: 'error'
        }),
        new transports.File({
            filename: requestLog,
            format: format.combine(isRequest())
        }),
        new transports.File({
            filename: combinedLog
        })
    ],
    exceptionHandlers: [
        new transports.File({
            filename: exceptionsLog
        })
    ]
})
HenriDev
  • 587
  • 7
  • 13

1 Answers1

2

It's JavaScript function expression. If you export it, it works pretty much the same. Hope this is what you mean.

More about function expression

let sayHello = function() {
  console.log("Hello")
}
sayHello.sayWorld = function() {
  console.log("World")
}

sayHello();
sayHello.sayWorld();
Huy Phạm
  • 888
  • 9
  • 24
  • ok that clears up that part thanks! now i would need to mock this in jest too, any idea on how to achieve this? – HenriDev Nov 18 '21 at 07:18
  • @HenriDeBel Sadly I'm not familiar or ever worked with jest. You could make a total different question for that. – Huy Phạm Nov 18 '21 at 07:50
  • yeah i figured i would know how to mock it with the answer to my first question thanks anyway :) – HenriDev Nov 18 '21 at 09:00