epsilon

epsilon

When/how is Logger app started?

I have a legacy Erlang app that is part of Mix umbrella project. The app has eunit tests that are ran using a custom Mix task. When migrating the whole project to Erlang/OTP 23 and Elixir 1.11 I switched logging from lager to Erlang logger (with Elixir backend) and for some reason logger config is not loaded from test.exs when running this custom eunit mix task - logger gets some default configuration with level: :all which is not ideal :slight_smile: .

This is not really specific to this mix task, this is true for any mix task it seems. Maybe the logger app is started before the config is loaded?

For example:

defmodule Mix.Tasks.Hello do
  use Mix.Task
  require Logger

  def run(_) do
    IO.inspect(Application.get_all_env(:logger))
    # level is info:

    # [
    #   utc_log: false,
    #   translators: [{Logger.Translator, :translate}],
    #   backends: [:console],
    #   discard_threshold_for_error_logger: 500,
    #   handle_sasl_reports: false,
    #   truncate: 8096,
    #   discard_threshold: 500,
    #   compile_time_application: nil,
    #   sync_threshold: 20,
    #   discard_threshold_periodic_check: 30000,
    #   console: [level: :info, format: {LoggerFormatter, :format}, metadata: :all],
    #   handle_otp_reports: true,
    #   start_options: [],
    #   level: :info,
    #   translator_inspect_opts: [],
    #   compile_time_purge_matching: []
    # ]

    Logger.debug("!!!")
    # level is info in config but still a debug message is printed
    # !!!

    IO.inspect(:logger.get_config())
    # level: :all is set for Elixir handler

    # %{
    #   handlers: [
    #     %{
    #       config: %{ ... },
    #       filter_default: :stop,
    #       filters: [
    #         filter_non_ssl: {&:logger_filters.domain/2, {:log, :sub, [:otp, :ssl]}}
    #       ],
    #       formatter: {:ssl_logger, %{}},
    #       id: :ssl_handler,
    #       level: :debug,
    #       module: :logger_std_h
    #     },
    #     %{
    #       config: %{ ... },
    #       filter_default: :log,
    #       filters: [],
    #       formatter: {:logger_formatter, %{}},
    #       id: Logger,
    #       level: :all,
    #       module: Logger.Handler
    #     }
    #   ],
    #   module_levels: [ssl_logger: :debug],
    #   primary: %{
    #     filter_default: :log,
    #     filters: [process_disabled: {&Logger.Filter.process_disabled/2, []}],
    #     level: :debug
    #   },
    #   proxy: %{ ... }
    # }
  end
end

How and when is the Logger app started when Mix task is ran? Why is the specified env config not loaded?
Thanks!

Most Liked

hauleth

hauleth

:logger and Logger are separate beasts. :logger is part of Erlang’s :kernel application and cannot be directly configured via config.exs file (as it is already loaded and started when config.exs is parsed) and :logger application is part of Elixir’s distribution. From now I will call them :logger module and logger application respectively.

The current flow is as follows (simplified):

  • kernel application is started (so :logger module is configured)
  • config.exs file is loaded (so it cannot configure :logger module)
  • logger application is started
  • logger application configures :logger module to use Elixir’s configuration

So if you want to change log level from the config.exs then you need to use logger application:

config :logger, level: :warning

This should set :logger.get_config().primary.level to :warning so messages would be filtered by :logger module.

Yes, this is a little bit confusing, because of the compatibility reasons in Elixir, but I hope to improve that over a time in the future.

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement