5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ElixirのログをJson形式にする 〜LoggerJson〜

Posted at

ブロジェクト作成

$ mix new logger_json_experiment

LoggerJson設定

mix.exs
# 〜省略〜
  defp deps do
    [
+     {:logger_json, "~> 6.0"}
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
# 〜省略〜
$ mix deps.get
config/config.exs
import Config

config :logger, :default_handler,
 formatter: {LoggerJSON.Formatters.Basic, []}

テスト用ソース

lib/logger_json_experiment.ex
defmodule LoggerJsonExperiment do
  @moduledoc """
  Documentation for `LoggerJsonExperiment`.
  """
+ require Logger

  @doc """
  Hello world.
  ## Examples
      iex> LoggerJsonExperiment.hello()
      :world
  """
  def hello do
+   Logger.error("hoge")
    :world
  end
end

実行

$ mix test

結果

Json形式になりました

{"message":"hoge","time":"2024-09-12T11:16:05.003Z","metadata":{},"severity":"error"}

logger_jsonを組まない時のメッセージ

20:12:12.160 [error] hoge

ソース(github)

5
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?