Skip to content

Commit 88a149b

Browse files
authored
fix(google-cloud): format_affected_user/1 with non-binary values (#161)
### 🐞 Problem The `google_cloud` formatter crashes when it receives a crash log where the `user context` contains values that are not binaries (e.g., integers). This happens because the formatter uses the `<>` operator for string concatenation, which raises an error when used with non-binary types. **Truncated example of a crashed log:** ```bash 2025-01-01T01:01:01.000000+00:00 error: FORMATTER CRASH: {string,<<"** (Postgrex.Error) ERROR 428... ``` ### ✅ Solution This PR replaces the use of the `<>` operator with `Enum.join/2` when formatting key-value pairs. This approach safely converts all values to strings before concatenation, preventing crashes when values are not binaries.
1 parent 6e77680 commit 88a149b

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/logger_json/formatters/google_cloud.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,10 @@ defmodule LoggerJSON.Formatters.GoogleCloud do
329329
end
330330
end
331331

332-
defp format_affected_user(%{user_id: user_id}), do: "user:" <> user_id
333-
defp format_affected_user(%{identity_id: identity_id}), do: "identity:" <> identity_id
334-
defp format_affected_user(%{actor_id: actor_id}), do: "actor:" <> actor_id
335-
defp format_affected_user(%{account_id: account_id}), do: "account:" <> account_id
332+
defp format_affected_user(%{user_id: user_id}), do: "user:#{user_id}"
333+
defp format_affected_user(%{identity_id: identity_id}), do: "identity:#{identity_id}"
334+
defp format_affected_user(%{actor_id: actor_id}), do: "actor:#{actor_id}"
335+
defp format_affected_user(%{account_id: account_id}), do: "account:#{account_id}"
336336
defp format_affected_user(_meta), do: nil
337337

338338
defp format_stacktrace(stacktrace) do

test/logger_json/formatters/google_cloud_test.exs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ defmodule LoggerJSON.Formatters.GoogleCloudTest do
338338
"""
339339
end
340340

341-
test "logs exception user context" do
341+
test "logs exception user context with binary values" do
342342
Logger.metadata(crash_reason: {{:EXIT, self()}, :foo})
343343

344344
# The keys are applied in the order of their precedence
@@ -362,6 +362,30 @@ defmodule LoggerJSON.Formatters.GoogleCloudTest do
362362
end)
363363
end
364364

365+
test "logs exception user context with non-binary values" do
366+
Logger.metadata(crash_reason: {{:EXIT, self()}, :foo})
367+
368+
# The keys are applied in the order of their precedence
369+
[:user_id, :identity_id, :actor_id, :account_id]
370+
|> Enum.reverse()
371+
|> Enum.reduce([], fn key, metadata ->
372+
metadata = Keyword.put(metadata, key, 123)
373+
Logger.metadata(metadata)
374+
375+
log_entry =
376+
capture_log(fn ->
377+
Logger.debug("Hello")
378+
end)
379+
|> decode_or_print_error()
380+
381+
[entity, _id] = key |> Atom.to_string() |> String.split("_")
382+
383+
assert log_entry["context"]["user"] == "#{entity}:123"
384+
385+
metadata
386+
end)
387+
end
388+
365389
test "logs http context" do
366390
conn =
367391
Plug.Test.conn("GET", "/", "")

0 commit comments

Comments
 (0)