Closed albertored closed 6 years ago
Hi @albertored! Yes, this would be a breaking change. The best option in your case is maybe to use a custom formatter. See Logger.Formatter for examples.
The better solution I got using a custom formatter and without duplicating a lot of the Logger.Formatter
code is the following
defmodule MetadataFixedFormatter do
def format(level, message, timestamp, []) do
[:date, " ", :time, " [", :level, "] ", :metadata, :message]
|> Logger.Formatter.format(level, message, timestamp, [])
end
def format(level, message, timestamp, metadata) do
[:date, " ", :time, " [", :level, "] (", :metadata, ") ", :message]
|> Logger.Formatter.format(level, message, timestamp, metadata)
|> Enum.map(fn
[[_k, ?=, _v, ?\s] | _tail] = list -> List.update_at(list, -1, fn elem -> Enum.drop(elem, -1) end)
other -> other
end)
end
end
There is still one thing that bothers me and it is the need of putting the format compiled patter inside the custom logger module. I was wondering if we can add another way of setting up custom logger by passing an extra_arguments
parameter like that
config :logger, :console,
format: {MetadataFixedFormatter, :format, extra_args}
that should result in a MetadataFixedFormatter.format(level, message, timestamp, metadata, extra_args)
. In my specific situation I would use it for setting format pattern from config file but it can be used in general for setting options on custom loggers.
what do you think about?
Environment
Current behavior
At the moment the
Logger.Formatter
add a space at the end of metadata chardata:this makes log formats like the follwowing a little bit ugly
Expected behavior
The last space at the end of metadata part should be removed.
I took a look at the code and implementing this fix should be straightforward so I'm wondering if there is a reason that I'm not aware of for having that space.
One con I can think of is that this change can be viewed as a breaking one since all format strings like
"$metadata$message"
will then result in log messages with a missing space and maybe you are not interested in changing it for this reason. If not I can submit a PR.