expressjs / morgan

HTTP request logger middleware for node.js
MIT License
7.92k stars 533 forks source link

skip function not working with 'immediate:true' configuration #259

Closed madhav7324 closed 2 years ago

madhav7324 commented 2 years ago

app.use(morgan(':date[iso] [info]: Starting :id :method ___ :url', { immediate: true, skip: (req) => req.baseUrl.startsWith('/api/health/') }));

dougwilson commented 2 years ago

Hello, sorry you are having trouble. If you add a console.log or put a debugger break point in your skip function, you will see that it is called even with immediate: true.

The issue is that your condition is based on data set after the morgan middleware, but immediate: true will log immediately, not wait until your response. This means any data you want to be available need to be added to the req or res objects needs to be done before the morgan middleware.

I hope this helps.

madhav7324 commented 2 years ago

morgan

any suggestion or solution to skip the request along with immediate: true configuration?

dougwilson commented 2 years ago

You need to add the data you would like to make the decision to skip or not to the req or res in a middleware that runs before morgan.