Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing the LoggerBackends.Console at runtime does not work #6

Open
BlackEdder opened this issue Jan 21, 2024 · 4 comments
Open

Removing the LoggerBackends.Console at runtime does not work #6

BlackEdder opened this issue Jan 21, 2024 · 4 comments

Comments

@BlackEdder
Copy link

I am trying to disable the Console backend at runtime (and replace it with my own backend), but that does not seem to work and the application continues logging to the console. I am using:

LoggerBackends.remove(LoggerBackends.Console)

I also tried passing :console and Logger.Backends.Console

@vkryukov
Copy link

I suspect the reason it doesn't disable the console logging is that you still have the default handler active; the LoggerBackends.Console is not added unless you do it explicitly, and adding or removing it won't change things with the default handler.

So the proper way to disable the console logging and to enable your own backend would be something like this in your config/config.exs:

config :logger,
  default_handler: false,
  level: :info # important - logs below this level will be ignored globally for all backends

config :logger, LoggerBackends.YourLogger,
  option1: value1,
  option2: value2

@BlackEdder
Copy link
Author

Thanks for the pointer. Does this mean the default handler is distinct from the LoggerBackends.Console? Or does it mean that it is started/added in a different way and therefore not controlled/controllable by LoggerBackends?

If the latter, wouldn't it be better to unify this and have the default_handler be controlled through LoggerBackends as well?

@vkryukov
Copy link

vkryukov commented Nov 27, 2024

Yes, the default handler is distinct from LoggerBackend.Console, it's part of the :logger package written in Erlang that Elixir uses. It is started in a different way than LoggerBackends, which is just a wrapper around :gen_event interface provided by :logger.

From what I understand, it's mostly given as an example. I did found a use case for it, which is when you want to log to console only higher priority logs (e.g., :warning and :error) but want to save to another backend, say a database, everything above :info.

In this case, you can't simply use the default console logger, as setting its level to :warning will also affect all the backend loggers. The proper solution is to turn off the default logger, and use LoggerBackends.Console and LoggerBackends.SQL with different log levels. You can look at my library https://github.com/vkryukov/logger_backends_sql for another logger backend implementation.

PS. All the above is not the absolute truth, but just my understanding of the topic; I might be completely wrong.

@vkryukov
Copy link

Just a quick add-on that the easiest way to remove the default logger would be to just

:logger.remove_handler(:default)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants