Skip to content

Commit dd41323

Browse files
MareStareMeow
authored andcommitted
Add tracing's EnvFilter-like log filtering with PHILOMENA_LOG env var (#495)
* Add `tracing`'s `EnvFilter`-like log filtering with `PHILOMENA_LOG` env var * Add logs about thumbnails generation * Better comment * Remove nesting via `with` * Use `debug` logging level for ecto by default as per Meow
1 parent e527a27 commit dd41323

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ services:
1313
context: .
1414
dockerfile: ./docker/app/Dockerfile
1515
environment:
16+
# Use this env variable to control the logs levels from different modules.
17+
# The syntax is `{module.function}=level,...`. The filter evaluation stops
18+
# on the first `{module.function}` that has a prefix match against the log
19+
# event's module and function. The last entry in the list of filters should
20+
# be a bare `level` which will be used as a catch-all for all other log
21+
# events that do not match any of the previous filters.
22+
- PHILOMENA_LOG=${PHILOMENA_LOG-Ecto=debug,Exq=none,PhilomenaMedia.Objects=info,debug}
1623
- MIX_ENV=dev
1724
- PGPASSWORD=postgres
1825
- ANONYMOUS_NAME_SALT=2fmJRo0OgMFe65kyAJBxPT0QtkVes/jnKDdtP21fexsRqiw8TlSY7yO+uFyMZycp

lib/philomena/application.ex

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ defmodule Philomena.Application do
66
use Application
77

88
def start(_type, _args) do
9+
configure_logging()
10+
911
# List all child processes to be supervised
1012
children = [
1113
# Start the Ecto repository
@@ -56,4 +58,50 @@ defmodule Philomena.Application do
5658
do: Base.encode16(:crypto.strong_rand_bytes(6))
5759

5860
defp valid_node_name(node), do: node
61+
62+
defp configure_logging() do
63+
# Log filtering design is borrowed from the Rust's `tracing` observability framework.
64+
# Specifically from the `EnvFilter` syntax:
65+
# https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html
66+
# However, it implements a simpler subset of that syntax which is just prefix matching.
67+
#
68+
# It would also be cool to get tracing's spans model for better low-level and
69+
# concurrent logs context. But spans implementation would require a lot of work,
70+
# unless there is an existing library for that. Anyway, for now, this should suffice.
71+
filters =
72+
System.get_env("PHILOMENA_LOG", "")
73+
|> String.split(",")
74+
|> Enum.map(&String.trim(&1))
75+
|> Enum.reject(&(&1 == ""))
76+
|> Enum.map(fn directive ->
77+
{selector, level} =
78+
case String.split(directive, "=", parts: 2) do
79+
[selector, level] -> {selector, level}
80+
[level] -> {nil, level}
81+
end
82+
83+
{selector, String.to_existing_atom(level)}
84+
end)
85+
86+
if not Enum.empty?(filters) do
87+
allow_log_event? = fn event ->
88+
with {module, function, _arity} <- Map.get(event.meta, :mfa),
89+
scope <- "#{inspect(module)}.#{function}",
90+
{_selector, level} when not is_nil(filters) <-
91+
filters
92+
|> Enum.find(fn {selector, _level} ->
93+
is_nil(selector) or String.starts_with?(scope, selector)
94+
end) do
95+
Logger.compare_levels(event.level, level) != :lt
96+
else
97+
_ -> false
98+
end
99+
end
100+
101+
:logger.add_primary_filter(
102+
:sql_logs,
103+
{fn event, _ -> if(allow_log_event?.(event), do: :ignore, else: :stop) end, []}
104+
)
105+
end
106+
end
59107
end

lib/philomena/images/thumbnailer.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ defmodule Philomena.Images.Thumbnailer do
1515
alias Philomena.Images.Image
1616
alias Philomena.Repo
1717

18+
require Logger
19+
1820
@versions [
1921
thumb_tiny: {50, 50},
2022
thumb_small: {150, 150},
@@ -75,6 +77,9 @@ defmodule Philomena.Images.Thumbnailer do
7577

7678
def generate_thumbnails(image_id) do
7779
image = Repo.get!(Image, image_id)
80+
81+
Logger.debug("Generating thumbnails for the image #{image.id}")
82+
7883
file = download_image_file(image)
7984
{:ok, analysis} = Analyzers.analyze_path(file)
8085

0 commit comments

Comments
 (0)