arnomi

arnomi

FlexLogger - Allowing logger configurations per module/application

One of the key features I found missing from Elixir’s Logger is the possibility of configuring Loggers (in particular log levels) differently for different modules/applications. For example, in many cases I like to have my application to log on :debug mode while everything else should be on a much quieter level. Some backends (for example FileLoggerBackend) provide custom filters but such functionality is not universally available. In particular, it is not available for the console logger.

Enter FlexLogger (https://hex.pm/packages/flex_logger). FlexLogger is a simple wrapper around Logger backends that allows to configure log levels per module/application. Furthermore, it can be even used to have different log configurations (e.g., formatters) per module. To set up a logger use

config :logger,
   backends: [{FlexLogger, :bar_console_logger},
              {FlexLogger, :default_logger}]

configure one or more FlexLogger as logger backends, where each logger is given a name. The specific configuration for each logger is then provided as:

config :logger, :bar_console_logger,
   logger: :console,
   default_level: :off, # this is the loggers default level, here it is turned off by default
   level_config: [ # override default levels
     [application: :some_app, module: Bar, level: :info],
   ],
   format: "BAR $message" # logger backend specific config

config :logger, :default_logger,
   logger: :console,
   default_level: :debug, # this is the logger's default level
   level_config: [ # override default levels
     [application: :some_app, module: Bar, level: :off]
   ],
   format: "DEFAULT $message" # logger backend specific config

Via the :logger key you define the actual logger backend to use. You can then define a :default_level (:debug, :info, :warn, :error or :off) and specify module/application specific log levels via the level_config. You can even go so far as to have different log levels for individual functions within a module, although that is probably very rarely called for. Configuration properties for the logger backend that is actually used are simply added to the configuration (e.g., :format in the above example)

For most use cases a single logger is sufficient as you can control the log levels via the level_config property, so the configuration overhead when using FlexLogger is very small. But if need be, using multiple named loggers allows for a very granular configuration of how the system is logging.

I hope this is useful to someone and I am looking forward to feedback.

Most Liked

arnomi

arnomi

Version 0.2.0 adds the possibility of controlling log levels by message content. This becomes especially useful in case logs are generated via Erlang’s :error_logger as in this case no metadata to filter upon is available. The following example looks for foo within messages:

config :logger, :foo_logger,
  logger: :console,
  default_level: :error, # this is the logger's default level
  level_config: [ # override default levels
     [message: ~r/foo/, level: :debug]
  ]
hisa

hisa

Thanks for this FlexLogger … I posted an usage example for Cabbage in How to log steps ala Cucumber · Issue #53 · cabbage-ex/cabbage · GitHub

slouchpie

slouchpie

This library is really really good for anyone else who is coding on a tight budget. I started using timber.io today and filled up 200kB in 1 hour of development on an unreleased app. This was 95% useless info logs from libraries that don’t afford setting log_level configuration. I tried using FlexLogger with default log level :warn and just allowing :info logs from my own application modules and the result is soooo much cleaner and smaller. I’m saving money and effort.

Thanks so much for writing this awesome library!

P.S. I don’t want to flame anybody but I don’t recommend timber.io. Some logs seem to just not make it to them. I even set it up as a gigalixir sink and I can see logs in gigalixir logs that don’t make it to timber. Also I am unable to clear my logs, despite that functionality being “available” and also their “chat to an engineer” button does nothing.

Where Next?

Popular in Announcing Top

tmbb
PhoenixWS - Websockets over Phoenix Channels Source code on Github here: GitHub - tmbb/phoenix_ws: Websockets implemented over Phoenix Ch...
New
mspanc
I am pleased to announce an initial release of the Membrane Framework - an Elixir-based framework with special focus on processing multim...
New
pkrawat1
Presenting Aviacommerce, open source e-commerce platform in Elixir Aviacommerce is an open source e-commerce platform in Elixir. We at...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
Qqwy
Today I realized that it would be possible to implement currying-capability in Elixir, using some clever anonymous function creation. (‘c...
New
zorbash
I created Kitto a framework for dashboards inspired by Dashing. The distributed characteristics of Elixir and the low memory footprint...
New
oltarasenko
Dear Elixir community, After a year of development, bug fixes, and improvements, we are proudly ready to share the release of Crawly 0.1...
New
mbuhot
Leverage Open Api 3.0 (Swagger) to document, test, validate and explore your Plug and Phoenix APIs. Generate and serve a JSON Open API ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement