expressjs / morgan

HTTP request logger middleware for node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

skip function not working with 'immediate:true' configuration

madhav7324 opened this issue · comments

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

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.

morgan

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

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.