How to bypass :console when redirecting logs to a custom Logger backend

I followed a great post on how to direct logs to custom backends - The Ultimate Guide To Logging In Elixir - and it works like a charm. The problem (unsolved in the post) is that my log messages are still printed by the default :console backend, and I don’t want them there. I want them to be fully intercepted by my backend.

So I tried to find a way to tell :console to ignore messages with certain metadata, but I couldn’t. Is there any other way?

Unless I’m missing something, it should be enough to remove :console from the backends:

config :logger, backends: [MyCustomBackendOnly]

@stefanchrobot but I do need :console. I am not going to ditch it. I just don’t want it to be “contaminated” by messages intended for my custom backend (marked with specific metadata).

There is no simple way to do such filtering in Elixir backends. You probably will need to write wrapper over Logger.Backend.Console that will discard messages you do not want.

There is a little bit more complex way to achieve similar thing to what you want, but as it would use non-public API and undocumented behaviour I would discourage such approach.

1 Like

How about writing to the backend directly instead of doing it via the Logger?

That is unsafe way to do so, as it omits overload protection mechanisms in Logger. Additionally then you need to configure all metadata passing, verbosity filtering, compile-time purging, etc. on your own.

I don’t know @lguminski’s use case, but I was assuming most of the features you mentioned might not be needed.

I am trying to avoid this.

My use case: I am having multiple worker jobs that are pretty verbose. That’s why I have redirected their logs to an external system for further investigation in case there was a job-specific problem (log levels are preserved). Now I want to have only non-worker log messages in :console’s output.

@hauleth maybe this could be added to the list of Logger improvements · Issue #9465 · elixir-lang/elixir · GitHub ?

What you exactly mean? Add what?

@hauleth sorry for being too brief. I mean the feature which would allow to exclude log messages from being printed in console. Currently : console prints out everything, even if the log message is meant for a specific logger backend. This leads to duplication of log messages for use cases like mine.

Would it be possible to achieve what you want by playing around with log levels (you can customize logging level per backend).

If not, I’d go with writing a new logger backend that wraps another backend - something along the lines of FilteringWrapperBackend. The implementation should be pretty generic and pretty straightforward - you’d offload the real work to the wrapped backend.

1 Like

That is the point of having multiple backends. And there is no point of adding such functionality to the list you have given, as some points there will cover your needs.

1 Like

This is a viable option. I will do so. Thanks @stefanchrobot