18

I wonder how to disable logging in Elixir when testing. In my current code I test for logger messages, so I don't want to disable it completely, but hide the messages until any test stop passing.

I'm using mix and ExUnit to manage and test my project.

mix test
Compiling 2 files (.ex)
.........
17:59:18.446 [warn]  Code seems to be empty
.
17:59:18.447 [warn]  Code doesn't contain HLT opcode
.
17:59:18.448 [warn]  Code doesn't contain HLT opcode

17:59:18.448 [error] Label error doesn't exist
.....

Finished in 0.07 seconds
16 tests, 0 failures

3 Answers 3

29

Either use ExUnit.CaptureLog:

import ExUnit.CaptureLog

test "example" do
   assert capture_log(fn ->
      Logger.error "log msg"
   end) =~ "log msg"
end

Or if you just want to ignore any logs then:

@tag capture_log: true
test "example" do
    Logger.error "log msg"
end
1
  • This answer is perfect. Thanks!
    – user938883
    Commented Oct 3, 2023 at 3:17
9

Into your config/test.exs file put the following config for the logger:

config :logger, level: :error

If you don’t have environment-specific configs, put the following line in your config/config.exs:

config :logger, level:
  case Mix.env do
    :test -> :error
    _ -> :info
  end

Another option would be to use another backend for the logging messages (assuming {:logger_file_backend, "~> 0.0"} is included in deps section of mix.exs):

config :logger,
  compile_time_purge_level: :debug,
  format: "$date $time $metadata[$level] $message\n",
  metadata: [:application, :module, :function, :file, :line],
  backends: [{LoggerFileBackend, :info_warn_error}]

config :logger, :info_warn_error,
  path: "log/info_warn_error.log", # or "/dev/null"
  level: :warn
4
  • I didn't mention, but I tried using first way and it preserved error, plus tests based on warning stopped passing. Second way is more or less the same thing, so I'll try adding own backend or just leave it as it is. Anyway, thanks for your answer.
    – Jump3r
    Commented Dec 27, 2017 at 18:08
  • Having the only backend with path /dev/null does exactly what you want: it suppresses any output to everywhere (including console,) but it leaves the logger’s gen_event server alive, so you can test whatever you want. Sidenote: I find it strange you want to suppress errors. Commented Dec 28, 2017 at 5:59
  • Well, I think this isn't strange when you start testing for errors. If you write like 10-15 tests for it, it becomes really annoying to see this all error messages, which are expected to appear.
    – Jump3r
    Commented Dec 28, 2017 at 9:36
  • This post is a few years old and not exactly the same context, but Jose Valim recommends setting to error: github.com/phoenixframework/phoenix/issues/898
    – skwidbreth
    Commented Jul 9, 2018 at 23:27
7

I spotted remove_backend() function in Logger's docs, so after using Logger.remove_backend(:console) in file where Logger should be disabled, every logged message is gone (tests are passing by the way).

EDIT: I asked Logger devs this question. Apparently, it's better to use @moduletag :capture_log on top of the test than removing backend. Anyway, works, so fine for me.

2
  • 2
    Probably not the best idea. You're removing any logging at all--test run or otherwise. You probably don't want to do that. Commented Dec 27, 2017 at 19:20
  • Thank you! Exactly for what I was looking.
    – Baruh
    Commented Apr 11, 2022 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.