简体   繁体   English

Elixir Logger:根据级别的元数据

[英]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 例如:对于调试/警告/错误,我想打印诸如:file和: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] 根据必须以以下格式添加元数据的方式,似乎应该可行: $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.) 通过配置是不可能的( $metadata[$level]意味着Logger.Formatter将吐出元数据,然后将$metadata[$level] Logger.Formatter方括号中。)

Luckily enough, Logger.metadata/1 allows to fully configure your metadata. 幸运的是, Logger.metadata/1允许完全配置您的元数据。

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. 另外, Logger.info/2接受元数据作为第二个参数,因此您可以引入自己的宏来包装Logger.info提供所需的任何元数据。

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 . 通过判断levelmetadata可以满足您的要求。 You can also find line and file in metadata , which has below fields. 您还可以在metadata找到具有以下字段的行和文件。

:application - the current application :application-当前应用程序

:module - the current module :module-当前模块

:function - the current function :function-当前函数

:file - the current file :file-当前文件

:line - the current line :line-当前行

:pid - the current process ID :pid-当前进程ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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