Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata in logger for 1.15 Elixir #1097

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Config

import_config "#{config_env()}.exs"
5 changes: 5 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Config

config :logger, :default_formatter, metadata: [:key_new]

config :logger, :console, metadata: [:key_old]
16 changes: 12 additions & 4 deletions lib/credo/check/warning/missed_metadata_key_in_logger_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,20 @@ defmodule Credo.Check.Warning.MissedMetadataKeyInLoggerConfig do
end

defp find_metadata_keys(params) do
metadata_keys = Params.get(params, :metadata_keys, __MODULE__)
with [] <- Params.get(params, :metadata_keys, __MODULE__) do
find_metadata_from_config()
end
end

if metadata_keys == [] do
:logger |> Application.get_env(:console, []) |> Keyword.get(:metadata, [])
defp find_metadata_from_config do
with true <- Version.match?(System.version(), "~> 1.15"),
{:ok, options} <- Application.fetch_env(:logger, :default_formatter) do
Keyword.get(options, :metadata, [])
else
metadata_keys
_ ->
:logger
|> Application.get_env(:console, [])
|> Keyword.get(:metadata, [])
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,68 @@ defmodule Credo.Check.Warning.MissedMetadataKeyInLoggerConfigTest do
end
end

describe "Version-specific" do
for fun <- @logger_functions do
if Version.match?(System.version(), "~> 1.15") do
test "it should report a violation when Logger.#{fun}/2 is used with metadata not present in default_formatter but present in console in ~> 1.15 Elixir" do
"""
defmodule CredoSampleModule do
def some_function(parameter1, parameter2) do
var_1 = "Hello world"
Logger.#{unquote(fun)}("The module: #\{var1\}", key_old: "value")
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> assert_issue()
end

test "it should NOT report a violation when Logger.#{fun}/2 is used with metadata present in default_formatter in ~> 1.15 Elixir" do
"""
defmodule CredoSampleModule do
def some_function(parameter1, parameter2) do
var_1 = "Hello world"
Logger.#{unquote(fun)}("The module: #\{var1\}", key_new: "value")
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> refute_issues()
end
else
test "it should NOT report a violation when Logger.#{fun}/2 is used with metadata not present in default_formatter but present in console in <= 1.15 Elixir" do
"""
defmodule CredoSampleModule do
def some_function(parameter1, parameter2) do
var_1 = "Hello world"
Logger.#{unquote(fun)}("The module: #\{var1\}", key_old: "value")
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> refute_issues()
end

test "it should report a violation when Logger.#{fun}/2 is used with metadata present in default_formatter in <= 1.15 Elixir" do
"""
defmodule CredoSampleModule do
def some_function(parameter1, parameter2) do
var_1 = "Hello world"
Logger.#{unquote(fun)}("The module: #\{var1\}", key_new: "value")
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> assert_issue()
end
end
end
end

for fun <- @logger_functions do
test "it should report a violation when Logger.#{fun}/2 is used with disallowed metadata" do
"""
Expand Down
Loading