From 4855af8f87ef1bbad06a092f9d3af5f8ee229468 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 6 Dec 2023 16:11:01 +0100 Subject: [PATCH] feat(docker): allow to configure loglevel at runtime through env variable Signed-off-by: Thomas Citharel --- config/docker.exs | 22 ++++++++++++++++++++++ lib/mix/tasks/mobilizon/common.ex | 23 ++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/config/docker.exs b/config/docker.exs index 01ecc69e..3f9d5ddc 100644 --- a/config/docker.exs +++ b/config/docker.exs @@ -2,6 +2,26 @@ import Config +loglevels = [ + :emergency, + :alert, + :critical, + :error, + :warning, + :notice, + :info, + :debug +] + +loglevel_env = System.get_env("MOBILIZON_LOGLEVEL", "error") + +loglevel = + if loglevel_env in Enum.map(loglevels, &to_string/1) do + String.to_existing_atom(loglevel_env) + else + :error + end + listen_ip = System.get_env("MOBILIZON_INSTANCE_LISTEN_IP", "0.0.0.0") listen_ip = @@ -43,6 +63,8 @@ config :mobilizon, Mobilizon.Storage.Repo, ssl: System.get_env("MOBILIZON_DATABASE_SSL", "false") == "true", pool_size: 10 +config :logger, level: loglevel + config :mobilizon, Mobilizon.Web.Email.Mailer, adapter: Swoosh.Adapters.SMTP, relay: System.get_env("MOBILIZON_SMTP_SERVER", "localhost"), diff --git a/lib/mix/tasks/mobilizon/common.ex b/lib/mix/tasks/mobilizon/common.ex index 07b8c871..89f79505 100644 --- a/lib/mix/tasks/mobilizon/common.ex +++ b/lib/mix/tasks/mobilizon/common.ex @@ -16,7 +16,7 @@ defmodule Mix.Tasks.Mobilizon.Common do if mix_task?(), do: Mix.Task.run("app.config") unless System.get_env("DEBUG") || @env == :test do - Logger.configure(level: :error) + Logger.configure(level: loglevel()) end Application.put_env(:phoenix, :serve_endpoints, false, persistent: true) @@ -144,4 +144,25 @@ defmodule Mix.Tasks.Mobilizon.Common do defp format_name("Elixir.Mix.Tasks.Mobilizon." <> task_name) do String.downcase(task_name) end + + @loglevels [ + :emergency, + :alert, + :critical, + :error, + :warning, + :notice, + :info, + :debug + ] + + defp loglevel do + loglevel_env = System.get_env("MOBILIZON_LOGLEVEL", "error") + + if loglevel_env in Enum.map(@loglevels, &to_string/1) do + String.to_existing_atom(loglevel_env) + else + :error + end + end end