Fl4m3Ph03n1x

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:

  1. Is there a way to activate logs but without outputting them to the terminal when I am running tests?
  2. Is there a way to only activate logs for some tests in my test suite?

Marked As Solved

lpil

lpil

Creator of Gleam

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

lpil

Creator of Gleam

This is built into the Elixir logger, users can purge logging statements from applications. :slight_smile:

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.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement