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))

Last Post!

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.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

We're in Beta

About us Mission Statement