logger configuration in mix commands

152 views
Skip to first unread message

vadim

unread,
Dec 2, 2015, 12:08:53 AM12/2/15
to elixir-lang-core
It appears that Logger is initialized before configuration is read by mix. As result, unless a manual step is included somewhere (presumably, in command itself -- but what about the commands like `mix test`?), the config-provided options are ignored.

1) Am I correct in describing the situation?

2) It appears that Logger does not provide [single-call] api to reset configuration to loaded config. Do I miss it? (Logger.configure_backend does it to specific backend. Is it enough to walk list of configured backends and configure them?) 

3) Is there a particular reason why logger cannot be reconfigured by mix after loading configuration?

Thank you,
/vadim

José Valim

unread,
Dec 2, 2015, 2:33:45 AM12/2/15
to elixir-l...@googlegroups.com
Logger will be reconfigured when your application loads (by the "app.start" to be more precise). So as long as you are starting your app, the new Logger configuration should kick in.



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/b8ef885a-b5c7-470c-8fcb-3fb6702eacb9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

vadim

unread,
Dec 2, 2015, 6:10:45 PM12/2/15
to elixir-lang-core, jose....@plataformatec.com.br
I'm sorry, I am still confused.

When I am running as compiled and released application, everything is handy-dandy.

When I am running `mix test`, everything also works as expected.

I have troubles when I am running `mix myapp.my_cli`, which looks like:

```
defmodule Mix.Tasks.Myapp.MyCli do
  use Mix.Task

  @shortdoc "run action in command line"

  def run(command_line_args) do
    Myapp.MyCli.main(command_line_args)
  end
end

defmodule Myapp.MyCli do
  require Logger

  def main(argv) do
    { :ok, _ } = Application.ensure_all_started(:myapp)

    Logger.metadata(foo: "bar")
    Logger.error "hello, log"
    Logger.flush
    System.halt(0)
  end
end

```

In this case, log is initialized before config, and is not re-initialized later.

Can you elaborate a bit your answer?

Thank you,
/vadim

Mike Evans

unread,
Dec 2, 2015, 10:12:36 PM12/2/15
to elixir-l...@googlegroups.com
Hi, I'm not entirely sure what the most correct solution is, but would explicitly starting your app after the .ensure_all_started call work? 

```
defmodule Myapp.MyCli do
  require Logger

  def main(argv) do
    { :ok, _ } = Application.ensure_all_started(:myapp)
    Myapp.start(_type, _data)

    Logger.metadata(foo: "bar")
    Logger.error "hello, log"
    Logger.flush
    System.halt(0)
  end
end
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

vadim

unread,
Dec 2, 2015, 10:31:59 PM12/2/15
to elixir-lang-core, mi...@silljays.com
My application does not have application callback explicitly defined (no `mod:`).

I tried to define one, but I miss something. How does it relate to the mix task... something else...

What should `Myapp.start` create as supervision? Just empty one? I tried to, with no luck. The logger is not re-initialized (or configured).

/v

vadim

unread,
Dec 2, 2015, 10:50:54 PM12/2/15
to elixir-lang-core, jose....@plataformatec.com.br
Apparently, the magic line is highlighted below.

Still, I do not understand -- where to the parameter of `&Mix.Tasks.App.Start.run/1` is passed? How is it related to parsing of command line arguments?

/v

On Wednesday, December 2, 2015 at 3:10:45 PM UTC-8, vadim wrote:
I'm sorry, I am still confused.

When I am running as compiled and released application, everything is handy-dandy.

When I am running `mix test`, everything also works as expected.

I have troubles when I am running `mix myapp.my_cli`, which looks like:

```
defmodule Mix.Tasks.Myapp.MyCli do
  use Mix.Task

  @shortdoc "run action in command line"

  def run(command_line_args) do 
        Mix.Tasks.App.Start.run([])

Eric Meadows-Jönsson

unread,
Dec 3, 2015, 11:53:09 PM12/3/15
to elixir-l...@googlegroups.com
You should call `Mix.Task.run "app.start", args`, if you call the task you don't have to call `Application.ensure_all_started` as it does that for you.

You don't have to have a application callback if you dont need it, the application can still be started.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Eric Meadows-Jönsson
Reply all
Reply to author
Forward
0 new messages