From ac029f24cca91a50e93c1a6dfaa7f2a59e344693 Mon Sep 17 00:00:00 2001 From: nwittstruck Date: Thu, 11 Jan 2024 10:00:47 +0100 Subject: [PATCH] chore: use json adapter for logging --- config/runtime.exs | 21 +++++++++++++++------ lib/mindwendel/application.ex | 15 +++++++++++++++ mix.exs | 3 ++- mix.lock | 1 + 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/config/runtime.exs b/config/runtime.exs index b1f0ad79..0e33d668 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -5,13 +5,18 @@ import Config require Logger -if config_env() != :test do - unless System.get_env("DATABASE_URL") do - Logger.warning( - "Environment variable DATABASE_URL is missing, e.g. Database_URL=ecto://USER:PASS@HOST/DATABASE" - ) - end +if config_env() == :prod do + # configure logging: + config :logger_json, :backend, + metadata: [:request_id], + json_encoder: Jason, + formatter: LoggerJSON.Formatters.BasicLogger + + # override Elixir's Logger with logger_json: + config :logger, backends: [LoggerJSON] +end +if config_env() != :test do unless System.get_env("DATABASE_HOST") do Logger.warning( "Environment variable DATABASE_HOST is missing, e.g. DATABASE_HOST=localhost or DATABASE_HOST=postgres" @@ -34,6 +39,9 @@ if config_env() != :test do ) end + # disable on prod, because logger_json will take care of this. set to :debug for test and dev + ecto_log_level = if config_env() == :prod, do: false, else: :debug + config :mindwendel, Mindwendel.Repo, database: System.get_env("DATABASE_NAME"), hostname: System.get_env("DATABASE_HOST"), @@ -43,6 +51,7 @@ if config_env() != :test do port: String.to_integer(System.get_env("DATABASE_PORT", "5432")), url: System.get_env("DATABASE_URL"), timeout: String.to_integer(System.get_env("DATABASE_TIMEOUT", "15000")), + log: ecto_log_level, ssl: System.get_env("DATABASE_SSL", "true") == "true", ssl_opts: [ verify: :verify_peer, diff --git a/lib/mindwendel/application.ex b/lib/mindwendel/application.ex index 26692131..8f2f8910 100644 --- a/lib/mindwendel/application.ex +++ b/lib/mindwendel/application.ex @@ -4,6 +4,7 @@ defmodule Mindwendel.Application do @moduledoc false use Application + require Logger def start(_type, _args) do # The URI module in elixir does not know about the default port for the websocket protocol, @@ -20,6 +21,9 @@ defmodule Mindwendel.Application do URI.default_port("ws", 80) URI.default_port("wss", 443) + # instruct oban to use the default logger for json output: + Oban.Telemetry.attach_default_logger() + children = [ # Start the Ecto repository Mindwendel.Repo, @@ -34,6 +38,17 @@ defmodule Mindwendel.Application do {Oban, oban_config()} ] + # when logger_json is defined, we also want it to take care of ecto: + if Application.get_env(:qrstorage, :logger_json) do + :ok = + :telemetry.attach( + "logger-json-ecto", + [:qrstorage, :repo, :query], + &LoggerJSON.Ecto.telemetry_logging_handler/4, + Logger.level() + ) + end + # See https://hexdocs.pm/elixir/Supervisor.html # for other strategies and supported options opts = [strategy: :one_for_one, name: Mindwendel.Supervisor] diff --git a/mix.exs b/mix.exs index bc9b1bee..44f8fa73 100644 --- a/mix.exs +++ b/mix.exs @@ -61,7 +61,8 @@ defmodule Mindwendel.MixProject do {:sobelow, "0.13.0", only: :dev}, {:telemetry_metrics, "0.6.1"}, {:telemetry_poller, "1.0.0"}, - {:timex, "3.7.11"} + {:timex, "3.7.11"}, + {:logger_json, "5.1.2"} ] end diff --git a/mix.lock b/mix.lock index 7e525134..1d3ee07a 100644 --- a/mix.lock +++ b/mix.lock @@ -23,6 +23,7 @@ "httpoison": {:hex, :httpoison, "2.2.1", "87b7ed6d95db0389f7df02779644171d7319d319178f6680438167d7b69b1f3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "51364e6d2f429d80e14fe4b5f8e39719cacd03eb3f9a9286e61e216feac2d2df"}, "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, "jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"}, + "logger_json": {:hex, :logger_json, "5.1.2", "7dde5f6dff814aba033f045a3af9408f5459bac72357dc533276b47045371ecf", [:mix], [{:ecto, "~> 2.1 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix, ">= 1.5.0", [hex: :phoenix, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "ed42047e5c57a60d0fa1450aef36bc016d0f9a5e6c0807ebb0c03d8895fb6ebc"}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, "mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"}, "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},