【问题标题】:How to pretty print conn content?如何漂亮地打印 conn 内容?
【发布时间】:2016-01-02 10:55:39
【问题描述】:

我尝试了以下

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

但我明白了

protocol String.Chars not implemented for %Plug.Conn

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

【问题讨论】:

    标签: elixir phoenix-framework


    【解决方案1】:

    您应该可以使用Kernel.inspect/2 漂亮地打印conn

    Logger.debug inspect(conn)
    

    【讨论】:

    • 它将打印连接,但不幸的是结果一点也不漂亮......
    • 这个答案不完整。见下文。
    【解决方案2】:

    使用inspect conn, pretty: true

    ... 或:

    inspect conn, pretty: true, limit: 30000

    ...因为Conn 结构非常大。

    【讨论】:

    • 如果您不介意滚动太多,也可以使用limit: :infinity
    • 这应该是公认的答案,尽管@rob-raisch 的答案也比公认的答案好
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      您确实可以使用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
      

      【讨论】:

        猜你喜欢
        • 2017-09-25
        • 2012-07-21
        • 2015-01-22
        • 1970-01-01
        • 1970-01-01
        • 2010-09-13
        • 2012-10-08
        • 2011-02-04
        • 2011-07-28
        相关资源
        最近更新 更多