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: https://github.com/elixir-lang/elixir/issues/5754

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

PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39247 209
New
crabonature
I’m still quite new to Elixir. As I understand we got in Elixir “multi guards” as convention to simplify one large guard with or’s?: de...
New
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
jsonify
So, is Heroku the only free option for hosting Phoenix/Elixir at this point? I’m not ready to commit to paying monthly and was wondering ...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
griffinbyatt
Sobelow Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New

Other popular topics Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement