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

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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