You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Removing the LoggerBackends.Console at runtime does not work #6
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
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 backendsconfig:logger,LoggerBackends.YourLogger,option1: value1,option2: value2
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?
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.
Activity
vkryukov commentedon Nov 26, 2024
I suspect the reason it doesn't disable the console logging is that you still have the default handler active; the
LoggerBackends.Consoleis 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:BlackEdder commentedon Nov 27, 2024
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 commentedon Nov 27, 2024
Yes, the default handler is distinct from
LoggerBackend.Console, it's part of the:loggerpackage written in Erlang that Elixir uses. It is started in a different way thanLoggerBackends, which is just a wrapper around:gen_eventinterface 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.,
:warningand: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
:warningwill also affect all the backend loggers. The proper solution is to turn off the default logger, and useLoggerBackends.ConsoleandLoggerBackends.SQLwith 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 commentedon Nov 27, 2024
Just a quick add-on that the easiest way to remove the default logger would be to just