如何漂亮地打印conn内容?

How to pretty print conn content?

我尝试了以下方法

def index(conn, _params) do
    Logger.debug conn
     ......

但是我明白了

protocol String.Chars not implemented for %Plug.Conn

我什至尝试过 Apex,但也没有用。

您应该能够使用 Kernel.inspect/2 来漂亮地打印 conn:

Logger.debug inspect(conn)

使用inspect conn, pretty: true

... 或:

inspect conn, pretty: true, limit: 30000

... 因为 Conn 结构相当大。

IO.inspect 很好。我在我的副项目中使用过,比如 ruby awsome_print

您确实可以使用 Kernel.inspect/2 来漂亮地打印 %Plug.Conn{} 的内容:

def index(conn, _params) do
  :logger.info inspect(conn, pretty: true)
  ....
end

请注意,之前使用 Logger 的答案应该提到您在使用它之前需要 require Logger,如:

require Logger

def index(conn, _params) do
  Logger.info inspect(conn, pretty: true)
  ....
end