How to log the raw http response built by Phoenix right before it is sent to the client?

I would like to know how can we display the http response (header and body) built by Phoenix right before it is sent to the client.

You can’t, as the first byte might have already been sent before the last one is calculated, as conn is basically a sink you stream into.

1 Like

Really ? well, right after would work as well. Basically I would like a log of what has been sent to the client.

:plug doesn’t support that as well as far as I know.

What about “outside” phoenix? Google for “nginx lua log response body” if this is of any help… If you’re on production you may want to use a filter and not log every request… :wink:

1 Like

The Plug.Conn.register_before_send/2 seems to be what you want @abtrapp

3 Likes

Wasn’t my question, but I didn’t knew that one, so I suggested a possible solution until a better one appeared. register_before_send looks good. So tnx, I’m sure that I will need this one day! :slight_smile:

1 Like

Remember the register_before_send may not be called in case of errors on application side, for example.

User will get some response, but your handler may not be called.

If you need to handle logging all responses, you may want to put something like a reverse proxy in front of your app, and configure it to do full logging there.

2 Likes

Hi @hubertlepicki,

Interesting design pattern.

We have requirement to log all requests and responses for audit and compliance purpose.

Have you had a chance to implement this pattern with nginx as reverse proxy?

If you could elaborate, it will help me.

Thanks in advance.

So you wouild like to log the audit from within Nginx? No, I never did this.

Thanks @hubertlepicki .

I am currently exploring to log requests and responses to RabbitMQ and then have Broadway log them to the logs repo.

Thanks for the response. :slight_smile:

1 Like