I need to collect the user access log in the application, mainly the name and version of the browser he is using. However, morgan is bringing many details that I don't need, can you help me?
Currently:
Firefox
- ::1 - OPTIONS - /signin - 204 - 0 - 0.126 ms http://localhost:8080/auth - Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0 -
Chrome
::1 - POST - /signin - 200 - 545 - 106.758 ms http://localhost:8080/auth - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36 -
::1 - OPTIONS - /signin - 204 - 0 - 0.163 ms http://localhost:8080/auth - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36 -
Expected:
Firefox
- ::1 - OPTIONS - /signin - 204 - 0 - 0.126 ms http://localhost:8080/auth - Firefox/76.0 -
Chrome
- ::1 - POST - /signin - 200 - 545 - 106.758 ms http://localhost:8080/auth - Chrome/83.0.4103.61 -
My code:
app.use((req,res,next) => {
const logger = morgan(function (tokens, req, res) {
return [
tokens['remote-addr'](req, res), '-',
tokens.method(req, res), '-',
tokens.url(req, res), '-',
tokens.status(req, res), '-',
tokens.res(req, res, 'content-length'), '-',
tokens['response-time'](req, res), 'ms',
tokens.referrer(req, res), '-',
tokens['user-agent'](req, res), '-',
].join(' ')
})
logger(req,res,next)
})