Is it possible to have different log levels for different applications/modules?

The Phoenix controller has a separate logging module, which is set to debug by default.

You can try changing that setting by editing the my_app_web.ex file and changing the use Phoenix.Controller, namespace: MyAppWeb line to use Phoenix.Controller, namespace: MyAppWeb, log: :warn.

Look at Logger filters and filter based on the metadata (of which :module is one built-in).

Hi @svsdehh; I think your use-case is similar to that catered for by for instance Java’s Logback back-end for slf4j.

Coming from the Java world ourselves, we also looked for such a solution, and so far have ended up adapting this Gist: https://gist.github.com/benperiton/94a02edeef7684c4b3bd6422e4b10118 – which allows separate backends with different log threshold levels, but very restricted filtering, into this Gist: https://gist.github.com/ellispritchard/71cfa6728e521620ed2728ac567ff555 – which simply adds a :not operator.

This allows you to have configuration which says ‘send all log output for application x to this back-end, and everything else to this other back-end’. One back-end is set at :debug level, the other at, say :warn, suppressing everything else.

We actually only use this in :dev mode, when we want to see debug output from our application, but cut-out debug noise from other applications.

In :test we set all logging to :warn, since we don’t usually want to see it at all, and in :prod we just send everything to console using the standard Elixir Logger (though usually set at :info level), and stream it to Splunk, so we don’t care about log overload!

Hope that helps!

Having recently made the switch from Java to Elixir I was also a bit disappointed to learn that there was no built-in mechanism to set different log levels for different modules/applications. To remedy this, and in the hope that his may be useful to others, I wrote a little Logger backend wrapper that adds exactly this functionality: FlexLogger.

4 Likes

Hmm, that looks cool. Should post it in its own thread. :slight_smile:

EDIT: They did:

:slight_smile:

4 Likes

For anyone stumbling upon this now, Elixir now has a great answer to:

Is it possible to have different log levels for different applications/modules?

In Elixir 1.11 Logger learned Logger.put_module_level/2 which lets you change the log level per module.

Then in Elixir 1.13 Logger learned Logger.put_application_level/2 which lets you change the log level per application.

3 Likes

@hauleth just wrote a great blog post detailing these features and many others: Log all the things - Hauleth

2 Likes