[英]Elixir Logger customize metadata format
In config.exs, I have config :logger, :console, metadata: [:module, :function, :line]
在 config.exs 中,我有
config :logger, :console, metadata: [:module, :function, :line]
This will add module=Module function=my_func/1 line=41
to the beginning of every debug message.这会将
module=Module function=my_func/1 line=41
到每条调试消息的开头。
Instead, I would like to format the message in a more easily readable way for me: Module.my_func/1(41)
.相反,我想以更易于阅读的方式格式化消息:
Module.my_func/1(41)
。
Is it possible to segment out specific metadata in the config format specifier?是否可以在配置格式说明符中分割出特定的元数据? It seems to me that I should be able to do something like
format: "$metadata[module].$metadata[function]($metadata[line])"
but this is not possible.在我看来,我应该能够做类似
format: "$metadata[module].$metadata[function]($metadata[line])"
事情,但这是不可能的。
Yes, this is possible, but I think you have to use the :mfa
metadata option .是的,这是可能的,但我认为您必须使用
:mfa
元数据选项。
In your config:在您的配置中:
config :logger, :console,
level: :debug,
metadata: [:mfa, :line],
format: {MyLogFormatter, :format}
Then in your custom MyLogFormatter
module, you would implement something like this:然后在您的自定义
MyLogFormatter
模块中,您将实现如下内容:
def format(level, message, timestamp, metadata) do
src =
case Keyword.get(metadata, :mfa) do
{m, f, a} -> "module=#{m} function=#{f}/#{a} line=#{Keyword.get(metadata, :line)}"
_ -> nil
end
string = "#{src} #{message}"
string <> "\n"
end
You could add a clause or two if you wanted custom behavior depending on the level, but be careful when customizing this because it can really knee-cap your app if there's a problem.如果您想要根据级别自定义行为,您可以添加一两个子句,但在自定义此内容时要小心,因为如果出现问题,它确实会影响您的应用程序。
If it works for you to adjust the log format using the formatting string, that is less work.如果使用格式化字符串调整日志格式对您有用,那工作量就少了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.