tompave

tompave

Can't configure logger in ExUnit tests run with Mix, in Elixir 1.11

Hello,

I’m adding support for Elixir 1.11 to a package I maintain and I’m having some issues with the logger configuration. This might be related to this forum thread or issue 6016 on the Elixir GitHub repo.

Until Elixir 1.10, I’ve been able to set the log level in config/test.exs with:

config :logger, level: :error

Now, with Elixir 1.11 this seems to not have any effect and my test output is full of debug lines, e.g. from Ecto.

That line still works in dev (e.g. if I set it in config/config.exs and then run iex -S mix). I’ve also tried with both the old use Mix.Config and the new import Config, with the same results. (For the avoidance of doubt: yes, the test config file is being imported.)

I find it interesting that by the time test/test_helper.exs is executed, I can verify that this configuration has been set:

Logger.level() # :debug
:logger.get_primary_config()[:level] # :debug
Application.get_env(:logger, :level) # :error

Which makes me think that something is preventing Elixir from configuring the Erlang logger in the tests. In dev, the integration works normally and those values appear to be in sync.

The only way I’ve found to make it work is to configure the logger directly in config/test.exs:

- config :logger, level: :error
+ Logger.configure(level: :error)

This feels wrong though, and I can’t help but wonder if there is something obvious that I’ve missed, and an easier way to solve the problem. This kind of change in behaviour between 1.10 and 1.11 seems the kind of thing that someone else must have stumbled upon before.

Most Liked

Brainiac

Brainiac

For future reference for devs having this issue with --no-start:

This issue occurs when running mix test --no-start and causes the application tree to not start.
The usual advice when runnning with --no-start is to use Application.ensure_all_started/2 to start the application tree under your test_helper.exs, however this is not ideal as the :logger application would have been started prior to calling this, resulting in your config.exs not being respected.
This can be seen in the following code in the mix app.start task, which mix test delegates application starting to. In the linked snippet,the Logger application is actually killed when the application is started on the test run.

As such, it would be necessary to use the app.start task directly instead of using Application.ensure_all_started/2 so that the :logger module works correctly and as expected.

Hence, your test_helper.exs should be updated to:

Mix.Task.run("app.start")
ExUnit.start()
tompave

tompave

I’ve verified that the problem is the --no-start flag to mix test. If I run the tests without it, the logger is configured correctly to the :error severity level.

Apparently Application.ensure_all_started(:fun_with_flags) alone is not enough to compensate for the --no-start flag.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement