0

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

Chrome

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)
})
rufus05
  • 43
  • 1
  • 10
  • We need more information if you want a regex solution for this. More than 2 example strings would be ideal. – emsimpson92 May 27 '20 at 18:33
  • @emsimpson92 update! – rufus05 May 27 '20 at 18:41
  • [this is close](https://regex101.com/r/0J7ZEm/4) but I still need a bit more information to fine tune it. Looking at your 2nd example for chrome, it lists both chrome and safari. How do you determine which they are using? – emsimpson92 May 27 '20 at 18:48
  • He's using chrome, I don't know why he identified safari ... – rufus05 May 27 '20 at 18:59
  • my guess is that it has something to do with this... [Of course, there is absolutely no guarantee that another browser will not hijack some of these things (like Chrome hijacked the Safari string in the past). That's why browser detection using the user agent string is unreliable and should be done only with the check of the version number (hijacking of past versions is less likely).](https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent) – emsimpson92 May 27 '20 at 19:05
  • @emsimpson92 So if the version is greater than 400 for example, does it mean that it is a safari? – rufus05 May 27 '20 at 20:00

2 Answers2

0

var result = accessLogEntry.replace(/(?<=auth\s-\s).*(?=(?:Firefox|Chrome)\/[\d\.]+)/g, "");

This takes care of most of your problem. In your 2nd chrome example it will leave both the Chrome and Safari versions listed. You'll need to determine which one is the correct browser. This will trim out all the garbage in the middle of the log entry.

Demo

emsimpson92
  • 1,779
  • 1
  • 9
  • 24
0

I'm use a component called: app.use(require('express-useragent').express())

const navigator = req.useragent.browser

rufus05
  • 43
  • 1
  • 10