简体   繁体   中英

Elixir Logger : metadata according to level

In order to keep the log file clean and readable, I would like to print metadata according to their level...

eg : for debug / warn / error I would like to print metadatas like :file and :line

However for info, I really don't have the need for those informations... Does it possible to print the metadata according to their level ?

It seems that it should be possible according to the way the metadata has to be added in the format: $metadata[$level]

It's not possible through configuration ( $metadata[$level] means that the Logger.Formatter will spit out the metadata followed by level in square brackets.)

Luckily enough, Logger.metadata/1 allows to fully configure your metadata.

Also, Logger.info/2 accepts a metadata as a second parameter, so you might introduce your own macro to wrap Logger.info providing any metadata you want.

I think that you can implement your own logger format .

defmodule MyConsoleLogger do
  def format(level, message, timestamp, metadata) do
    # Custom formatting logic...
  rescue
    _ -> "could not format: #{inspect {level, message, metadata}}"
  end
end

It can satisfy your requirements by judging level and metadata . You can also find line and file in metadata , which has below fields.

:application - the current application

:module - the current module

:function - the current function

:file - the current file

:line - the current line

:pid - the current process ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM