Skip to content

Commit 12c71ad

Browse files
authored
Prevent logging with a level mismatch (#447)
1 parent 7314c46 commit 12c71ad

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

integration_test/sql/logging.exs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ defmodule Ecto.Integration.LoggingTest do
131131
TestRepo.insert!(%Post{title: "1"}, log: false)
132132
end) == ""
133133
end
134+
135+
test "with a log: true override when logging is disabled" do
136+
repo_conf = Application.get_env(:ecto_sql, TestRepo)
137+
138+
on_exit(fn -> Application.put_env(:ecto_sql, TestRepo, repo_conf) end)
139+
140+
Application.put_env(:ecto_sql, TestRepo, log: false)
141+
142+
refute capture_log(fn ->
143+
TestRepo.insert!(%Post{title: "1"}, log: true)
144+
end) =~ "an exception was raised logging"
145+
end
134146
end
135147

136148
describe "parameter logging" do
@@ -202,7 +214,6 @@ defmodule Ecto.Integration.LoggingTest do
202214

203215
param_logs = Regex.named_captures(param_regex, log)
204216

205-
206217
# Autogenerated fields
207218
assert param_logs["bid1"] =~ @uuid_regex
208219
assert param_logs["bid2"] =~ @uuid_regex
@@ -243,7 +254,7 @@ defmodule Ecto.Integration.LoggingTest do
243254
int2 = 2
244255
uuid2 = Ecto.UUID.generate()
245256
int_query = from l in Logging, where: l.int == ^int2 and l.uuid == ^uuid2, select: l.int
246-
257+
247258
row2 = [
248259
int: int_query,
249260
uuid: uuid2,

lib/ecto/adapters/sql.ex

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,20 +1069,23 @@ defmodule Ecto.Adapters.SQL do
10691069
:telemetry.execute(event_name, measurements, metadata)
10701070
end
10711071

1072-
case Keyword.get(opts, :log, log) do
1073-
true ->
1072+
case {opts[:log], log} do
1073+
{false, _level} ->
1074+
:ok
1075+
1076+
{true, false} ->
1077+
:ok
1078+
1079+
{true, level} ->
10741080
Logger.log(
1075-
log,
1081+
level,
10761082
fn -> log_iodata(measurements, repo, source, query, opts[:cast_params] || params, result, stacktrace) end,
10771083
ansi_color: sql_color(query)
10781084
)
10791085

1080-
false ->
1081-
:ok
1082-
1083-
level ->
1086+
{opts_level, args_level} ->
10841087
Logger.log(
1085-
level,
1088+
opts_level || args_level,
10861089
fn -> log_iodata(measurements, repo, source, query, opts[:cast_params] || params, result, stacktrace) end,
10871090
ansi_color: sql_color(query)
10881091
)

0 commit comments

Comments
 (0)