From c418b689078878cc079801d37cbedebba2730f04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Fri, 29 Nov 2024 18:22:29 +0800 Subject: [PATCH] Add env vars for logger configuration --- README.md | 10 +++++++--- config/dev.exs | 6 ++++-- lib/livebook.ex | 8 ++++++-- lib/livebook/config.ex | 29 +++++++++++++++++++++-------- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 17e56f6ac74a..3711c7ea4586 100644 --- a/README.md +++ b/README.md @@ -220,9 +220,6 @@ 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" (Standalone), "attached:NODE:COOKIE" (Attached node) @@ -261,6 +258,13 @@ The following environment variables can be used to configure Livebook on boot: * `LIVEBOOK_IP` - sets the ip address to start the web application on. Must be a valid IPv4 or IPv6 address. + * `LIVEBOOK_LOG_LEVEL` - sets the logger level, allowing for more verbose + logging, either of: error, warning, notice, info, debug. Defaults to warning. + + * `LIVEBOOK_LOG_METADATA` - a comma-separated list of metadata keys that should + be included in the log messages. Currently the only Livebook-spcecific key is + users (attached to evaluation logs). By default includes only request_id. + * `LIVEBOOK_NODE` - sets the node name for running Livebook in a cluster. Note that Livebook always runs using long names distribution, so the node host name must use a fully qualified domain name (FQDN) or an IP diff --git a/config/dev.exs b/config/dev.exs index 4049d77b5645..59348b447ddd 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -61,8 +61,10 @@ config :livebook, LivebookWeb.Endpoint, web_console_logger: true ] -# Do not include metadata nor timestamps in development logs -config :logger, :console, format: "[$level] $message\n" +# Do not include timestamps in development logs +config :logger, :console, + format: "$metadata[$level] $message\n", + metadata: [] # Include HEEx debug annotations as HTML comments in rendered markup config :phoenix_live_view, :debug_heex_annotations, true diff --git a/lib/livebook.ex b/lib/livebook.ex index ac085cf0de11..188d298c2fae 100644 --- a/lib/livebook.ex +++ b/lib/livebook.ex @@ -97,8 +97,12 @@ defmodule Livebook do Livebook.Config.secret!("LIVEBOOK_SECRET_KEY_BASE") || Livebook.Utils.random_secret_key_base() - if Livebook.Config.debug!("LIVEBOOK_DEBUG") do - config :logger, level: :debug + if level = Livebook.Config.log_level!("LIVEBOOK_LOG_LEVEL") do + config :logger, level: level + end + + if metadata = Livebook.Config.log_metadata!("LIVEBOOK_LOG_METADATA") do + config :logger, :console, metadata: metadata end if port = Livebook.Config.port!("LIVEBOOK_PORT") do diff --git a/lib/livebook/config.ex b/lib/livebook/config.ex index 9a8b9e97c2f8..a5c8e392a41b 100644 --- a/lib/livebook/config.ex +++ b/lib/livebook/config.ex @@ -444,18 +444,31 @@ defmodule Livebook.Config do 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)}") + Parses and validates log level from env. + """ + def log_level!(env) do + levels = ~w(error warning notice info debug) + + if level = System.get_env(env) do + if level in levels do + String.to_atom(level) + else + abort!("expected #{env} to be one of #{Enum.join(levels, ", ")}, got: #{inspect(levels)}") end end end + @doc """ + Parses and validates log metadata keys from env. + """ + def log_metadata!(env) do + if metadata = System.get_env(env) do + for item <- String.split(metadata, ","), + key = String.trim(item), + do: String.to_atom(key) + end + end + @doc """ Parses and validates the port from env. """