vlad.grb

vlad.grb

How to add colours to Logger

Can anyone provide please an example how to add, lets say purple color, to console logger backend?
I tried to redo the same what I’ve found in Ecto’s source code, but unfortunately it didn’t give any result.

Marked As Solved

vlad.grb

vlad.grb

Thanks for your response.

I misunderstood documentation and was passing color parameter instead of ansi_color
So here is how to specify a color

Logger.debug("Elixir is awesome", ansi_color: :blue)

Also Liked

SeedyROM

SeedyROM

I realized something dumb that I forgot to mention, the syntax color for info is white… So the reason I never saw the color in my dumb tests was my fault. If you 're looking for something like rust’s env_logger or any nodejs logging library. The level isn’t the color that changes, it’s the entire log. I hope randos from the other programming language worlds find this to be helpful in a latenight configuration hackathon.

A simple example is Logger.info will return an all white output and Logger.warn will return an all yellow result.

CharlesO

CharlesO

I guess so, I applied this scheme to my .iex.exs file and i have a purple IEx> prompt, so I know i have colors enabled…

Application.put_env(:elixir, :ansi_enabled, true)
IEx.configure(
  inspect: [limit: :infinity, pretty: true, structs: true, width: 210],
  colors: [enabled: true],
  default_prompt: [
    "\e[G",    # ANSI CHA, move cursor to column 1
    :magenta,
    "%prefix", # IEx prompt variable
    ">",       # plain string
    :reset
  ] |> IO.ANSI.format |> IO.chardata_to_string
 )

but still, I cannot get Logger to emit colored logs…strange

CharlesO

CharlesO

To get colors in logger, add :colors to config

config :logger, :console, format: "[$date $time] $message\n", colors: [enabled: true]

To get IEx style colours in Logger:

  @syntax_colors [number: :yellow, atom: :cyan, string: :green, boolean: :magenta, nil: :magenta]
  @format [
    pretty: true,
    structs: true,
    syntax_colors: @syntax_colors
  ]

Then write logs like so:

Logger.debug(inspect(%{status: 2, message: "Done", rows: 100}, @format))
CharlesO

CharlesO

I could not get this to work.

I’m using conEmu on windows.
Any help please?

OvermindDL1

OvermindDL1

It is ‘just working’ in ConEmu here in Windows 10 when I run the above code? Do you have the console set up right?

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement