Forcing evaluation of log messages

From the Logger docs:

The Logger.info/2 macro emits the provided message at the :info level. Note the arguments given to info/2 will only be evaluated if a message is logged. For instance, if the Logger level is set to :warning , :info messages are never logged and therefore the arguments given above won’t even be executed.

A buggy logger message expression escaped unit test checks due to the configured log level. Is there a way to force evaluation of all log messages? Ideally I’m thinking of something like

config :logger, force_evaluation: true

in config/test.exs.

Any thoughts on this?

2 Likes

Yeah that’s an interesting idea, in particular because in tests you usually have a really high log level set since you don’t want to actually see the output.

2 Likes

Sounds like interesting concept. It shouldn’t be super hard to implement it, though I would worry a little about potential performance issues. Though I think that it could be compile time only flag and there would be no performance hit in such case.

3 Likes

Got bitten by this behaviour again: Logger.info("Failed foo: #{foo}") blew up on production, but was not caught by tests because the logging level is set to :warn for tests.

An alternative that I tried was setting the logging level to :debug in tests and doing ExUnit.start(capture_log: true). The disadvantage of this approach is that the ExUnit output is very noisy when a test fails - it prints out all logs.

@josevalim Any thoughts on what would be the best approach to handle this?

1 Like

Please open up an issue, we will add a flag to enable so.

4 Likes

A link to the opened issue will be appreciated as well!

1 Like

Thank you! Here’s the issue.

3 Likes

Quite a natural limitation of code injection at compile-time, I’m not sure about performance implications but I think that a logger option for runtime evaluation would be welcome.