expressjs / morgan

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

Add support for such as cloudflare #247

Closed gtchecker closed 3 years ago

gtchecker commented 3 years ago

I have modified the morgan module to suit my needs but this should be implemented for those using Cloudflare.

Line 296-300

/**
 * Cloudflare Connecting IP 
 */

morgan.token('CFIP', CFIP)

added this to line 505-507

function CFIP(req) {
  return req.headers['cf-connecting-ip']
}

Example of Usage

morgan.format('cloudflare', ':CFIP - [:date[clf]] - ":method :url HTTP/:http-version" - :status - :res[content-length] - ":referrer" - ":user-agent"')
dougwilson commented 3 years ago

There are many different custom headers many providers provide. The good thing is that we already support them -- through the use of custom tokens (like you did) or through our existing req token: :req[cf-connecting-ip]

gtchecker commented 3 years ago

There are many different custom headers many providers provide. The good thing is that we already support them -- through the use of custom tokens (like you did) or through our existing req token: :req[cf-connecting-ip]

I tried to do the way you stated but it never worked so I went ahead and made stated above.

dougwilson commented 3 years ago

Strange. That would be a bug. Can you provide a reproduction case we can debug and determine the issue? Based on your code above, :req[cf-connecting-ip] should work, and if not, that is absolutely a bug.

gtchecker commented 3 years ago

I will reproduce the case later on tomorrow as I am currently away from my Desk right now. Sorry for the inconvenience.

fcostaprojects commented 3 years ago

working fine:

morgan.token('remote-addr', function (req, res) { return req.headers['cf-connecting-ip'] });