Skip to content

Commit

Permalink
Add env vars for logger configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko committed Nov 29, 2024
1 parent 8ddcfb9 commit c418b68
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/livebook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 21 additions & 8 deletions lib/livebook/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down

0 comments on commit c418b68

Please sign in to comment.