Skip to content

Commit

Permalink
chore: use json adapter for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nwittstruck committed Jan 11, 2024
1 parent 8cc0806 commit ac029f2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
21 changes: 15 additions & 6 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"),
Expand All @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions lib/mindwendel/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit ac029f2

Please sign in to comment.