/var/log/messages

Mar 3, 2019 - 2 minute read - Comments - programming

プログラミング Elixir (24)

13.11 タスク: 実行可能なコマンドを作成、の節。このあたりからどんどんすすめることができるのかどうか。

とりあえず

mix.exs の修正なのかな。というか、git の面倒全然見れてなくて微妙。

実行ファイルの仕組みとしては

  • unix ベースのプラットフォームで実行できるファイルにパッケージできる
  • erlang の escript ユーティリティを使っている
  • プリコンパイルして zipped したものを実行できる模様

とのこと。とりあえず erlang には依存しているのか。escript が実行するとき

  • mix.exs を読み込む

ので mix.exs の修正が必要になる模様。project に

  def project do
    [
      app: :issues,
      escript: escript_config,
      version: "0.1.0",
      elixir: "~> 1.7",
      build_embedded:  Mix.env() == :prod,
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end
  • escript な属性
  • build_embedded な属性

を追加してます。あと、escript_config という手続きを定義。

  defp escript_config do
    [ main_module: Issues.CLI ]
  end

で、Issues.CLI の run を main に、とのこと。

  def main(argv) do
    argv
    |> parse_args
    |> process
  end

で、

$ mix escript.build
warning: variable "escript_config" does not exist and is being expanded to "escript_config()", please use parentheses to remove the ambiguity or change the variable name
  mix.exs:7

Compiling 4 files (.ex)
Generated issues app
Generated escript issues with MIX_ENV=dev

たしかにファイルができていますね。

-rwxr-xr-x   1 rms  staff  1714374 Feb 27 20:10 issues

動きます。

$ ./issues elixir-lang elixir 3
numb | created_at           | title                                                  
-----+----------------------+--------------------------------------------------------
6248 | 2017-06-22T09:41:01Z | Improve ExUnit output for assertions on process mailbox
6611 | 2017-09-28T08:56:29Z | Support Erlang 21 new features                         
6738 | 2017-10-09T16:07:43Z | with clause cannot match when case is inside else block

そのまますすめます。

13.12 タスク: ロギングの追加

logger は既に使える状態になっているはず。

  def application do
    [
      extra_applications: [:logger, :httpoison]
    ]
  end

この節はこのあたりの使い方、な模様。ログレベルは以下が用意されているとのこと。

  • debug
  • info
  • warn
  • error

コンパイル時に設定される最小ログレベルは config/config.exs に書くとのこと。

config :logger, compile_time_purge_level: :info

これで debug は出力されないのか。

ログの出力方法

以下な方法があるとのこと。

  • Logger.debug "Order total #{total(order)}"
  • Logger.debug fn -> "Order total #{total(order)}" end

lambda を渡す場合、ログレベルが ignore だったとしても上の場合だと total は呼び出されるけど、下の場合だと lambda は必要な場合にのみ呼び出されるとのこと。lambda 好きだよ lambda。

defmodule Issues.GithubIssues do

  require Logger

  @user_agent [ {"User-agent", "Elixir dave@"}]
  @github_url Application.get_env(:issues, :github_url)

  def fetch(user, project) do
    Logger.info "Fetching user #{user}'s project #{project}"
    issues_url(user, project)
    |> HTTPoison.get(@user_agent)
    |> handle_response
  end

  def issues_url(user, project) do
    "#{@github_url}/repos/#{user}/#{project}/issues"
  end

  def handle_response({ :ok, %{status_code: 200, body: body}}) do
    Logger.info "Successful response"
    Logger.debug fn -> inspect(body) end
    { :ok, Poison.Parser.parse!(body)}
  end

  def handle_response({ _, %{status_code: ___, body: body}}) do
    Logger.error "Error #{status} returned"
    { :error, Poison.Parser.parse!(body)}
  end
end

ロギングを追加して iex で試していますので、こちらでも。って iex で実行する方法が忘却の彼方orz

こうでした (iex での実行)

$ iex -S mix
Erlang/OTP 21 [erts-10.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

warning: variable "escript_config" does not exist and is being expanded to "escript_config()", please use parentheses to remove the ambiguity or change the variable name
  mix.exs:7

Compiling 4 files (.ex)
Generated issues app
Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Issues.CLI.process {"elixir-lang", "elixir", 1}

20:31:39.090 [info]  Fetching user elixir-lang's project elixir
 
20:31:41.177 [info]  Successful response
numb | created_at           | title                                                  
-----+----------------------+--------------------------------------------------------
6248 | 2017-06-22T09:41:01Z | Improve ExUnit output for assertions on process mailbox
:ok

たしかに debug なナニは出力されていないですね。ここで一回エントリ締めます。

プログラミング Elixir (23) プログラミング Elixir (25)

comments powered by Disqus