question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to log request and response urls?

See original GitHub issue

I 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 server
  • proxyReq.baseUrl is not defined, I’m not sure what I should use to get the host part of the request.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
chimuraicommented, Apr 14, 2021

Think with onProxyRes you should be able to get the logging you want:

  onProxyRes: (proxyRes, req, res) => {
    // log original request and proxied request info
    const exchange = `[${req.method}] [${proxyRes.statusCode}] ${req.path} -> ${proxyRes.req.protocol}//${proxyRes.req.host}${proxyRes.req.path}`;
    console.log(exchange); // [GET] [200] / -> http://www.example.com
  },

4reactions
chimuraicommented, Feb 24, 2016

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.

[HPM] GET /v1/users/123 ~> https://my-app.herokuapp.com/api/v1/users/123

Let me know what you think.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found