Logging Websocket Requests

I’m having problem logging initial requests to a Phoenix Websocket.

We have a Phoenix Application with a GraphQL API (using Absinthe) and a Phoenix Websocket for Graphql Subscriptions. The connect/2 function in our Socket checks the JWT token and returns either an :ok tuple or :error.

Our Load Balancer records the GET request with a 403 response, but the elixir application does not log anything, even using use Phoenix.Socket, log: :debug in our socket.

I was not able to find how to add Phoenix HTTP-like logging to WebSockets. I understand that for an established connection the logging is different, but the initial HTTP Request could be logged in the same way I assume?

1 Like

Might be useful:

I think [:phoenix, :socket_connected] is what I’d look into first. connection: upgrade requests might be unable to reach phoenix (and its logger) since they are probably terminated in cowboy and phoenix only receives the resulting websocket connection.

You can also try tracing the relevant modules to check why the tokens fail.

You can attach a function to the telemetry event that is executed when somebody connects.
Put this inside your MyApp.Application.start/2

:telemetry.attach("channel-logger", [:phoenix, :socket_connected], &SocketLogger.handle/4, :ok)

Then, you need to write SocketLogger to do whatever you wanna accomplish when there is a new connection.

1 Like