Fl4m3Ph03n1x
How to hide logs in some tests but not in others?
Background
I have library that logs messages depending on the value of disable_logging:
unless Application.get(:my_app, :disable_logging, false), do:
Logger.info("Hello World!")
Depending on MIX_ENV I have a config for each setup:
#test.config
use Mix.Config
config :my_app, disable_logging: true
Problem
The issue here is that I don’t want log messages all over my test results. So, naturally, I could set the disable_logging to true and be done with it.
However, if I do it, I can’t test whether or not the Logger is being called and if it’s being called with the correct values:
Question
So, given this I have some questions:
- Is there a way to activate logs but without outputting them to the terminal when I am running tests?
- Is there a way to only activate logs for some tests in my test suite?
Marked As Solved
lpil
Alternatively update test_helper.exs to have ExUnit.start(capture_log: true) and then you can make assertions about logging with capture_log as per usual, but it won’t print the log output in tests where you don’t.
Also Liked
lpil
This is built into the Elixir logger, users can purge logging statements from applications. ![]()
You can remove your logging logic and instead instruct users to add this config if they wish to remove your logging.
config :logger,
compile_time_purge_matching: [
[application: :foo]
]
- If I do it your way, will I be able to make my tests async again? Or the fact I am capturing logs means I can never have them be async?
Using capture log does not prevent you from being able to run your test async, I would run all the tests async using this method.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









