Skip to content
This repository was archived by the owner on Dec 8, 2020. It is now read-only.

Commit abd7854

Browse files
authored
Gaurd against nil time_native values (#208)
* Gaurd against nil time_native values * Add test for nil query_time
1 parent 783ba2a commit abd7854

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/timber/integrations/ecto_logger.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ defmodule Timber.Integrations.EctoLogger do
8080
which is then logged at the designated level.
8181
"""
8282
@spec log(Ecto.LogEntry.t) :: Ecto.LogEntry.t
83-
def log(%{query: query, query_time: time_native} = entry, level) do
83+
def log(%{query: query, query_time: time_native} = entry, level) when is_integer(time_native) do
8484
case resolve_query(query, entry) do
8585
{:ok, query_text} ->
8686
# The time is given in native units which the VM determines. We have
@@ -107,6 +107,13 @@ defmodule Timber.Integrations.EctoLogger do
107107
end
108108
end
109109

110+
# time_native above is not an integer as expected. This is required and a violation of the
111+
# type spec. Queries of this nature will be ignored.
112+
def log(entry, _level) do
113+
entry
114+
end
115+
116+
110117
# Interestingly, the query is not necessarily a String.t, it
111118
# can also be a single-arity function which, given the log entry
112119
# as a parameter, will return a String.t

test/lib/timber/integrations/ecto_logger_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ if Code.ensure_loaded?(Ecto) do
2020
assert log =~ "Processed SELECT * FROM table in"
2121
assert log =~ " @metadata "
2222
end
23+
24+
test "query_time is nil" do
25+
query = "SELECT * FROM table"
26+
27+
log = capture_log(fn ->
28+
EctoLogger.log(%{query: query, query_time: nil}, :info)
29+
end)
30+
31+
assert log == ""
32+
end
2333
end
2434
end
2535
end

0 commit comments

Comments
 (0)