Skip to content

Commit

Permalink
Ensure logger suite passes when IO.ANSI colors is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Feb 1, 2025
1 parent cff55fb commit 80aad35
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/logger/lib/logger/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ defmodule Logger.Formatter do
warning: Keyword.get(colors, :warning, :yellow),
notice: Keyword.get(colors, :info, :normal),
info: Keyword.get(colors, :info, :normal),
debug: Keyword.get(colors, :debug, :cyan)
debug: Keyword.get(colors, :debug, :cyan),
enabled: Keyword.get(colors, :enabled, &IO.ANSI.enabled?/0)
}
end

Expand Down Expand Up @@ -221,12 +222,14 @@ defmodule Logger.Formatter do

defp format_fa(fun, arity), do: [Atom.to_string(fun), "/", Integer.to_string(arity)]

defp colorize(data, level, colors, md) do
if Map.get_lazy(colors, :enabled, &IO.ANSI.enabled?/0) do
color = md[:ansi_color] || Map.fetch!(colors, level)
[IO.ANSI.format_fragment(color, true), data | IO.ANSI.reset()]
else
data
defp colorize(data, level, %{enabled: enabled} = colors, md) do
case if(is_function(enabled, 0), do: enabled.(), else: enabled) do
true ->
color = md[:ansi_color] || Map.fetch!(colors, level)
[IO.ANSI.format_fragment(color, true), data | IO.ANSI.reset()]

false ->
data
end
end

Expand Down

0 comments on commit 80aad35

Please sign in to comment.