Ebtoulson

Ebtoulson

Proposal: Logger Backends and Formatters

This is kind of a question and a proposal.
I recently looked into switching loggers for a few our phoenix applications from a file logger to a stdout json logger.
While doing some research, I noticed a few different patterns in use. The first was to wrap the existing Logger module and the second was to create a backend.

I don’t think wrapping Logger is ideal since a lot of other packages have dependencies on Logger and could be calling it directly.
This would also require the project owner to be on the lookout for any direct use of the module in any future changes.

Creating a backend seems to be the intended pattern but I think this could be improved. I see a definite separation between outputting (stdio, file, tcp/udp, etc)
and formatting, but this seems to be coupled in a lot of existing logger packages (e.g. logger_json_file_backend).

Have I been looking at this wrong? If not, here’s my proposal: Let’s create a behavior for formatters, break out the default formatter from console, and determine formatters from configuration.

config :logger,
  backends: [{Logger.Backends.Stdout, :stdout_logger}]

config :logger, :stdout_logger,
  level: :debug,
  metadata: [:request_id],
  formatter: Logger.Formatters.Json

Doing something like this while remaining backwards compatible with the existing :console backend and :format configuration might be a little tricky.
We might also want to break out common functionality into the utils module or a helper. This would include things like handling metadata and what not.
Thoughts?

Most Liked

josevalim

josevalim

Creator of Elixir

It seems we do support it today, I just forgot about it. :sweat_smile:

More info: Proposal: Logger Backends and Formatters · Issue #5754 · elixir-lang/elixir · GitHub

josevalim

josevalim

Creator of Elixir

It is not possible in the current implementation, you would have to reimplement the whole backend. Can someone please open up an issue for supporting a custom formatter? I think the current console backend is complex enough to justify the inclusion of a formatting convenience.

ChristopherBui

ChristopherBui

@josevalim This is a pain point we’re also dealing with. Is there a good way to accomplish this with the current implementation?

Where Next?

Popular in Discussions Top

jesse
Hi everyone, I hesitated to post this here because I don’t want you to think I’m spamming, but I’ve been working on a Platform-as-a-Serv...
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New
shishini
I think this twitter post and youtube video didn’t get as much attention as I hoped I am still new to Elixir, so can’t really judge ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

We're in Beta

About us Mission Statement