From d2e875a94f29bfd0a94954b35d1d714c8ee75662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Mon, 23 Oct 2023 20:01:05 +0200 Subject: [PATCH] Add env var for debug logs (#2292) --- README.md | 3 +++ lib/livebook.ex | 4 ++++ lib/livebook/config.ex | 13 +++++++++++++ 3 files changed, 20 insertions(+) diff --git a/README.md b/README.md index 9215cdc01..38e7450c0 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,9 @@ The following environment variables can be used to configure Livebook on boot: configuration. Defaults to "livebook" under the default user data directory. + * LIVEBOOK_DEBUG - enables verbose logging, when set to "true". Disabled + by default. + * LIVEBOOK_DEFAULT_RUNTIME - sets the runtime type that is used by default when none is started explicitly for the given notebook. Must be either "standalone" (Elixir standalone), "attached:NODE:COOKIE" (Attached node) diff --git a/lib/livebook.ex b/lib/livebook.ex index 671d62925..3f3807296 100644 --- a/lib/livebook.ex +++ b/lib/livebook.ex @@ -91,6 +91,10 @@ defmodule Livebook do Livebook.Config.secret!("LIVEBOOK_SECRET_KEY_BASE") || Base.encode64(:crypto.strong_rand_bytes(48)) + if Livebook.Config.debug!("LIVEBOOK_DEBUG") do + config :logger, level: :debug + end + if port = Livebook.Config.port!("LIVEBOOK_PORT") do config :livebook, LivebookWeb.Endpoint, http: [port: port] end diff --git a/lib/livebook/config.ex b/lib/livebook/config.ex index f3dc1dc14..c74f3cf2a 100644 --- a/lib/livebook/config.ex +++ b/lib/livebook/config.ex @@ -420,6 +420,19 @@ defmodule Livebook.Config do end end + @doc """ + Parses and validates debug mode from env. + """ + def debug!(env) do + if debug = System.get_env(env) do + cond do + debug in ["1", "true"] -> true + debug in ["0", "false"] -> false + true -> abort!("expected #{env} to be a boolean, got: #{inspect(debug)}") + end + end + end + @doc """ Parses and validates the port from env. """