How to log request and response urls?
See original GitHub issueI would like to log each request no matter what status code the target server responded. I’m trying to achieve the following format:
// Format:
// --> [METHOD] [PATH_REQUESTED_FROM_PROXY] -> [URL_REQUESTED_FROM_TARGET]
// Example:
// --> GET /v1/users/123 -> https://my-app.herokuapp.com/api/v1/users/123
My proxy options are:
var proxyOpts = {
target: 'https://my-app.herokuapp.com/',
pathRewrite: {
'^/v1' : '/api/v1'
},
onProxyReq: function onProxyReq(proxyReq, req, res) {
// Log outbound request to remote target
console.log('--> ', req.method, req.path, '->', proxyReq.baseUrl + proxyReq.path);
},
onError: function onError(err, req, res) {
console.error(err);
res.status(500);
res.json({error: 'Error when connecting to remote server.'});
},
logLevel: 'debug',
changeOrigin: true,
secure: true
};
Which outputs:
--> GET /api/v1/users/123 -> undefined/api/v1/users/123
Problems:
req.path
is already converted to the new format with pathRewrite, I would want to log the url which client actually requested from this proxy serverproxyReq.baseUrl
is not defined, I’m not sure what I should use to get the host part of the request.
Issue Analytics
- State:
- Created 8 years ago
- Comments:12 (4 by maintainers)
Top Results From Across the Web
Spring Boot - How to log all requests and responses with ...
I had defined logging level in application.properties to print requests/responses, method url in the log file logging.level.org.springframework.web=DEBUG ...
Read more >Spring - Log Incoming Requests - Baeldung
In this quick tutorial, we'll demonstrate the basics of logging incoming requests using Spring's logging filter.
Read more >How we built a Node.js Middleware to Log HTTP API ... - Moesif
A guide on how we built our Node.js middleware that logs HTTP API Calls. ... remoteAddress, remoteFamily, url }) ); response.end(); });.
Read more >Response.url - Web APIs | MDN
In our Fetch Response example (see Fetch Response live) we create a new Request object using the Request() constructor, passing it a JPG...
Read more >HTTP Logging in ASP.NET Core - Microsoft Learn
Record information about incoming requests and responses. · Filter which parts of the request and response are logged. · Filtering which headers ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Think with
onProxyRes
you should be able to get the logging you want:I think will be nice to encode the proxy behavior in the log statements too; Different arrows indicate what happend:
->
default.~>
pathRewrite applied.=>
proxyTable applied.≈>
both pathRewrite and proxyTable applied.So in your use-case; the original url will get logged, its target URI and the pathRewrite arrow.
Let me know what you think.