From 8ceafd8dfa14791f9590410f97b156fe22f92850 Mon Sep 17 00:00:00 2001 From: MareStare Date: Mon, 7 Apr 2025 01:42:07 +0000 Subject: [PATCH 1/5] Add `tracing`'s `EnvFilter`-like log filtering with `PHILOMENA_LOG` env var --- docker-compose.yml | 8 ++++++ lib/philomena/application.ex | 56 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 709c95426..2470cee6c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,6 +13,14 @@ services: context: . dockerfile: ./docker/app/Dockerfile environment: + # Use this env variable to control the logs levels from different modules. + # The syntax is `{module.function}=level,...` and the `module.function`. + # The filter evaluation stops on the first `{module.function}` that has a + # prefix match against the log event's module and function. The last entry + # in the list of filters should be a bare `level` which will be used as a + # catch-all for all other log events that do not match any of the previous + # filters. + - PHILOMENA_LOG=${PHILOMENA_LOG-Ecto=none,Exq=none,PhilomenaMedia.Objects=info,debug} - MIX_ENV=dev - PGPASSWORD=postgres - ANONYMOUS_NAME_SALT=2fmJRo0OgMFe65kyAJBxPT0QtkVes/jnKDdtP21fexsRqiw8TlSY7yO+uFyMZycp diff --git a/lib/philomena/application.ex b/lib/philomena/application.ex index 3ecfcc030..e9fcee155 100644 --- a/lib/philomena/application.ex +++ b/lib/philomena/application.ex @@ -6,6 +6,8 @@ defmodule Philomena.Application do use Application def start(_type, _args) do + configure_logging() + # List all child processes to be supervised children = [ # Start the Ecto repository @@ -56,4 +58,58 @@ defmodule Philomena.Application do do: Base.encode16(:crypto.strong_rand_bytes(6)) defp valid_node_name(node), do: node + + defp configure_logging() do + # Log filtering design is borrowed from the Rust's `tracing` observability framework. + # Specifically from the `EnvFilter` syntax: + # https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html + # However, it implements a simpler subset of that syntax which is just prefix matching. + # + # It would also be cool to get tracing's spans model for better low-level and + # concurrent logs context. But spans implementation would require a lot of work, + # unless there is an existing library for that. Anyway, for now, this should suffice. + filters = + System.get_env("PHILOMENA_LOG", "") + |> String.split(",") + |> Enum.map(&String.trim(&1)) + |> Enum.reject(&(&1 == "")) + |> Enum.map(fn directive -> + {selector, level} = + case String.split(directive, "=", parts: 2) do + [selector, level] -> {selector, level} + [level] -> {nil, level} + end + + {selector, String.to_existing_atom(level)} + end) + + if not Enum.empty?(filters) do + allow_log_event? = fn event -> + case(Map.get(event.meta, :mfa)) do + nil -> + false + + {module, function, _arity} -> + scope = "#{inspect(module)}.#{function}" + + filters + |> Enum.find(fn {selector, _level} -> + is_nil(selector) or String.starts_with?(scope, selector) + end) + |> case do + nil -> + false + + {_selector, level} -> + Logger.compare_levels(event.level, level) != :lt + end + end + end + + :logger.add_primary_filter( + :sql_logs, + {fn event, _ -> if(allow_log_event?.(event), do: :ignore, else: :stop) end, []} + ) + end + end end From d1d8988a8be3ac2f3878fefd22b9cfa6296af085 Mon Sep 17 00:00:00 2001 From: MareStare Date: Mon, 7 Apr 2025 01:44:17 +0000 Subject: [PATCH 2/5] Add logs about thumbnails generation --- lib/philomena/images/thumbnailer.ex | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/philomena/images/thumbnailer.ex b/lib/philomena/images/thumbnailer.ex index c90a638bf..1beea1465 100644 --- a/lib/philomena/images/thumbnailer.ex +++ b/lib/philomena/images/thumbnailer.ex @@ -15,6 +15,8 @@ defmodule Philomena.Images.Thumbnailer do alias Philomena.Images.Image alias Philomena.Repo + require Logger + @versions [ thumb_tiny: {50, 50}, thumb_small: {150, 150}, @@ -75,6 +77,9 @@ defmodule Philomena.Images.Thumbnailer do def generate_thumbnails(image_id) do image = Repo.get!(Image, image_id) + + Logger.debug("Generating thumbnails for the image #{image.id}") + file = download_image_file(image) {:ok, analysis} = Analyzers.analyze_path(file) From efa8cd0c18101b355499103447c067969dab66b0 Mon Sep 17 00:00:00 2001 From: MareStare Date: Mon, 7 Apr 2025 04:19:14 +0000 Subject: [PATCH 3/5] Better comment --- docker-compose.yml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 2470cee6c..6336f90fa 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,12 +14,11 @@ services: dockerfile: ./docker/app/Dockerfile environment: # Use this env variable to control the logs levels from different modules. - # The syntax is `{module.function}=level,...` and the `module.function`. - # The filter evaluation stops on the first `{module.function}` that has a - # prefix match against the log event's module and function. The last entry - # in the list of filters should be a bare `level` which will be used as a - # catch-all for all other log events that do not match any of the previous - # filters. + # The syntax is `{module.function}=level,...`. The filter evaluation stops + # on the first `{module.function}` that has a prefix match against the log + # event's module and function. The last entry in the list of filters should + # be a bare `level` which will be used as a catch-all for all other log + # events that do not match any of the previous filters. - PHILOMENA_LOG=${PHILOMENA_LOG-Ecto=none,Exq=none,PhilomenaMedia.Objects=info,debug} - MIX_ENV=dev - PGPASSWORD=postgres From b828def6f304fc32a4701784f26ca78c4ad6ad8c Mon Sep 17 00:00:00 2001 From: MareStare Date: Mon, 7 Apr 2025 04:20:48 +0000 Subject: [PATCH 4/5] Remove nesting via `with` --- lib/philomena/application.ex | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/lib/philomena/application.ex b/lib/philomena/application.ex index e9fcee155..e1c4e76f5 100644 --- a/lib/philomena/application.ex +++ b/lib/philomena/application.ex @@ -85,24 +85,16 @@ defmodule Philomena.Application do if not Enum.empty?(filters) do allow_log_event? = fn event -> - case(Map.get(event.meta, :mfa)) do - nil -> - false - - {module, function, _arity} -> - scope = "#{inspect(module)}.#{function}" - - filters - |> Enum.find(fn {selector, _level} -> - is_nil(selector) or String.starts_with?(scope, selector) - end) - |> case do - nil -> - false - - {_selector, level} -> - Logger.compare_levels(event.level, level) != :lt - end + with {module, function, _arity} <- Map.get(event.meta, :mfa), + scope <- "#{inspect(module)}.#{function}", + {_selector, level} when not is_nil(filters) <- + filters + |> Enum.find(fn {selector, _level} -> + is_nil(selector) or String.starts_with?(scope, selector) + end) do + Logger.compare_levels(event.level, level) != :lt + else + _ -> false end end From df5d9488dcc19a19f8db5462f815c15de59a60d4 Mon Sep 17 00:00:00 2001 From: MareStare Date: Wed, 7 May 2025 15:39:16 +0000 Subject: [PATCH 5/5] Use `debug` logging level for ecto by default as per Meow --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 6336f90fa..3375d3977 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,7 +19,7 @@ services: # event's module and function. The last entry in the list of filters should # be a bare `level` which will be used as a catch-all for all other log # events that do not match any of the previous filters. - - PHILOMENA_LOG=${PHILOMENA_LOG-Ecto=none,Exq=none,PhilomenaMedia.Objects=info,debug} + - PHILOMENA_LOG=${PHILOMENA_LOG-Ecto=debug,Exq=none,PhilomenaMedia.Objects=info,debug} - MIX_ENV=dev - PGPASSWORD=postgres - ANONYMOUS_NAME_SALT=2fmJRo0OgMFe65kyAJBxPT0QtkVes/jnKDdtP21fexsRqiw8TlSY7yO+uFyMZycp