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

Add check for piping into Logger functions #1182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
57 changes: 57 additions & 0 deletions lib/credo/check/warning/pipe_to_logger.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
defmodule Credo.Check.Warning.PipeToLogger do

Check warning

Code scanning / Credo

Modules should have a @moduledoc tag.

Modules should have a @moduledoc tag.
@moduledoc false

use Credo.Check,
id: "EX??",
base_priority: :normal,
category: :warning,
explanations: [
check: """
Calls into Logger can be purged when `:compile_time_purge_matching` is enabled in
the Logger configuration. This will remove the entire pipeline.
Use |> tap(&Logger.info(&1)) instead.
"""
]

@logger_functions [
:debug,
:info,
:notice,
:warn,
:warning,
:error,
:critical,
:alert,
:emergency
]

def run(%SourceFile{} = source_file, params) do
issue_meta = IssueMeta.for(source_file, params)

Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
end

defp traverse(
{:|>, meta, [_, {{:., _, [{:__aliases__, _, [:Logger]}, function]}, _, _}]} = ast,
issues,
issue_meta
)
when function in @logger_functions do
trigger = "|> Logger.#{function}"

new_issue =
format_issue(
issue_meta,
message: "There should be no `@#{trigger} calls.",
trigger: "#{trigger}",
line_no: meta[:line],
column: meta[:column]
)

{ast, issues ++ [new_issue]}
end

defp traverse(ast, issues, _issue_meta) do
{ast, issues}
end
end
153 changes: 153 additions & 0 deletions test/credo/check/warning/pipe_to_logger_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
defmodule Credo.Check.Warning.PipeToLoggerTest do
use Credo.Test.Case

@described_check Credo.Check.Warning.PipeToLogger

#
# cases NOT raising issues
#

test "it should NOT report when Logger is used without piping" do
"""
defmodule CredoSampleModule do
require Logger

def some_function(parameter1) do
Logger.warn("This is a direct call")
Logger.info("Another direct call")

something_else()
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> refute_issues()
end

test "it should NOT report when piping to other functions" do
"""
defmodule CredoSampleModule do
def some_function(parameter1) do
parameter1
|> process_data()
|> transform_result()
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> refute_issues()
end

test "it should NOT report when Logger is in a different position" do
"""
defmodule CredoSampleModule do
require Logger

def some_function(parameter1) do
Logger.warn(parameter1 |> transform_data())

# Other code
result = other_function() |> process_result()

result
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> refute_issues()
end

#
# cases raising issues
#

test "it should report a violation when piping to Logger.warn" do
"""
defmodule CredoSampleModule do
require Logger

def some_function(parameter1) do
parameter1
|> transform_data()
|> Logger.warn()

:ok
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> assert_issue(fn issue ->
assert issue.line_no == 7
assert issue.column == 5
assert issue.trigger == "|> Logger.warn"
end)
end

test "it should report a violation when piping to any Logger function" do
"""
defmodule CredoSampleModule do
require Logger

def some_function(parameter1) do
"Starting process"
|> Logger.info()

parameter1
|> transform_data()
|> Logger.error()

"Debug information"
|> Logger.debug()

:ok
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> assert_issues(fn issues ->
assert length(issues) == 3

[issue1, issue2, issue3] = issues

assert issue1.line_no == 6
assert issue1.column == 5
assert issue1.trigger == "|> Logger.info"

assert issue2.line_no == 10
assert issue2.column == 5
assert issue2.trigger == "|> Logger.error"

assert issue3.line_no == 13
assert issue3.column == 5
assert issue3.trigger == "|> Logger.debug"
end)
end

test "it should report a violation when piping to Logger with options" do
"""
defmodule CredoSampleModule do
require Logger

def some_function(error) do
error
|> inspect()
|> Logger.error(some_metadata: "value")

:error
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> assert_issue(fn issue ->
assert issue.line_no == 7
assert issue.column == 5
assert issue.trigger == "|> Logger.error"
end)
end
end