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
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 failuresBut 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 thingWhen 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.