Removing the LoggerBackends.Console at runtime does not work · Issue #6 · elixir-lang/logger_backends · GitHub
Skip to content

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

Open

Description

@BlackEdder

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

Activity

  1. vkryukov commented on Nov 26, 2024

    @vkryukov

    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
  2. BlackEdder commented on Nov 27, 2024

    @BlackEdder
    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?

  3. vkryukov commented on Nov 27, 2024

    @vkryukov

    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.

  4. vkryukov commented on Nov 27, 2024

    @vkryukov

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions