Phoenix Server运行时如何在Controller中记录某些内容? [英] How to Log something in Controller when Phoenix Server is running?

查看:41
本文介绍了Phoenix Server运行时如何在Controller中记录某些内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在服务器运行时从我的Phoenix应用程序中的一个控制器中打印一些调试信息.

I'm trying to print some debug information from one of my Controllers in my Phoenix app when the server is running.

defmodule PhoenixApp.TopicController do
  use PhoenixApp.Web, :controller

  def index(conn, _params) do
    log("this text")

    # ... 
  end
end

推荐答案

好吧,事实证明这很简单.您需要在控制器中使用Logger elixir模块并调用其中一种方法来记录文本.

Okay, turns out it's pretty straight forward. You need to require the Logger elixir module in your controller and call one of it's methods to log your text.

defmodule PhoenixApp.TopicController do
    require Logger

    def index(conn, params) do
        Logger.info  "Logging this text!"
        Logger.debug "Var value: #{inspect(params)}"

        # ...
    end
end

支持的级别是:

  • :debug-用于调试相关消息
  • :info-用于获取任何类型的信息
  • :warn-用于警告
  • :error-错误
  • :debug - for debug-related messages
  • :info - for information of any kind
  • :warn - for warnings
  • :error - for errors

来源: Elixir-记录器文档

这篇关于Phoenix Server运行时如何在Controller中记录某些内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆