For a mix task, I can configure Logger in config.exs, but not logger console formatting. Am I doing something wrong here?

1,863 views
Skip to first unread message

Steffen Bauer

unread,
Oct 27, 2015, 9:24:31 AM10/27/15
to elixir-lang-talk
I prefer to add the date in my logger outputs to the console; furthermore I want info messages in green color, so for every project I add according configuration in config.exs.

Unfortunately, I don't manage to properly configure the output in a custom mix task.

To illustrate this I just reproduced this problem with a basic setup:

a) New project with
> mix new logtest

b) Configuration file:

# config/config.exs
use Mix.Config

config
:logger,
  level
: :debug

config
:logger, :console,
  format
: "$date $time [$level] $levelpad$message\n",
  colors
: [info: :green]


c) A most basic mix task:

# lib/mix/tasks/doit.ex
defmodule
Mix.Tasks.Doit do
 
use Mix.Task
 
@shortdoc "Do something"

 
def run(_args) do
   
Logtest.do_something
 
end

end

# lib/logtest.ex
defmodule
Logtest do
 
require Logger
 
 
def do_something do
   
Logger.info "This is an info message in the real thing"
   
Logger.debug "This is a debug message in the real thing"
   
Logger.warn "This is a warning message in the real thing"
   
Logger.error "This is an error message in the real thing"
 
end

end

d) Test file:

# test/logtest_test.exs
defmodule
LogtestTest do
 
use ExUnit.Case
 
require Logger
 
  test
"the truth" do
   
Logger.info "This is an info message in a test"
   
Logger.debug "This is a debug message in a test"
   
Logger.warn "This is a warning message in a test"
   
Logger.error "This is an error message in a test"
 
end
end

Effect: When running the test, I get the expected logger output (the info message is green on my console, date is included)

> mix test

2015-10-27 13:46:23.368 [info]  This is an info message in a test
.


2015-10-27 13:46:23.368 [debug] This is a debug message in a test
2015-10-27 13:46:23.368 [warn]  This is a warning message in a test
Finished in 0.05 seconds (0.05s on load, 0.00s on tests)
2015-10-27 13:46:23.368 [error] This is an error message in a test

1 test, 0 failures


But not when I run the mix task (it shows default formatting, and the info messages are in black color as default console color):

> mix doit

13:59:12.637 [info]  This is an info message in the real thing

13:59:12.638 [debug] This is a debug message in the real thing

13:59:12.638 [warn]  This is a warning message in the real thing

13:59:12.638 [error] This is an error message in the real thing


When for example I change the logging level in config.exs to :warn, running > mix doit will only show warn and error messages as expected, while still ignoring my console format configuration. So config.exs is definitely used for logger configuration in my mix task, but it ignores the 'config :logger, :console' part

No changes in mix.exs. Elixir 1.1.1, Erlang OTP 17.5. Linux Debian 8 'Jessie'

Am I doing something fundamentally wrong? It might be only a minor problem, but I am struggling with this for days now. Googled a lot and searched here for similiar problems, found nothing so far.

José Valim

unread,
Oct 27, 2015, 9:52:45 AM10/27/15
to elixir-l...@googlegroups.com
Steffen, it sounds like it should just work. Can you push a simple app to Github? I'd gladly take a look.



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-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/835e4570-24bf-4a4e-8993-74d2b2a6c4f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Steffen Bauer

unread,
Oct 27, 2015, 10:02:49 AM10/27/15
to elixir-lang-talk, jose....@plataformatec.com.br
Hi José,

thanks a lot for having a look. I just pushed the basic example project onto GitHub under:

https://github.com/SteffenBauer/elixir-logtest

Theron Boerner

unread,
Oct 27, 2015, 10:14:30 AM10/27/15
to elixir-lang-talk
Do you have any other files in config like dev.exs, test.exs, or
prod.exs? They may be messing with the settings.

Steffen Bauer

unread,
Oct 27, 2015, 10:21:23 AM10/27/15
to elixir-lang-talk
No other files at all.

I posted the example to:

https://github.com/SteffenBauer/elixir-logtest

I double-re-checked it, I just checked out this repository, compiled it with 'mix compile', then both 'mix test' and 'mix doit'. The test run is showing the desired logger formatting on my machine, the doit task is showing default logger formatting ignoring my configuration.

José Valim

unread,
Oct 27, 2015, 12:44:03 PM10/27/15
to elixir-l...@googlegroups.com
The reason is that the Logger application is restarted when you start your application but not before. So the configuration is loaded but it takes no effect until Mix.Task.run "app.start" is called (which the test task does). If your mix task is meant to be called inside an application, you can add Mix.Task.run "app.start" to it, otherwise, you can't really configure Logger for a custom/bare task.



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-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.

Steffen Bauer

unread,
Oct 28, 2015, 5:09:50 AM10/28/15
to elixir-lang-talk
Hi José,

that worked! Thanks a lot!

My code now looks like this:

# lib/logtest.ex
defmodule
Logtest do

 
use Application
 
require Logger
 
 
def start(_type, _args) do
 
end

 
 
def do_something do
   
Logger.info "This is an info message in the real thing"
   
Logger.debug "This is a debug message in the real thing"
   
Logger.warn "This is a warning message in the real thing"
   
Logger.error "This is an error message in the real thing"
 
end

end

# lib/mix/tasks/doit.ex
defmodule
Mix.Tasks.Doit do
 
use Mix.Task
 
@shortdoc "Do something"

 
def run(_args) do

   
Mix.Task.run "app.start"
   
Logtest.do_something
 
end

end




José Valim

unread,
Oct 28, 2015, 5:31:04 AM10/28/15
to elixir-l...@googlegroups.com
Great! Btw, you don't need use Application and def start, only if you have a supervision tree (which in your case you do not!).
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/44791f22-8e53-4c03-ac19-ac898a3d2e01%40googlegroups.com.

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


--
Reply all
Reply to author
Forward
0 new messages