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 Libraries Top

markmark206
simple_feature_flags is a tiny package that lets you turn features on or off based on which environment (e.g. localhost, staging, product...
New
praveenperera
FastRSS Parse RSS feeds very quickly: This is rust NIF built using rustler Uses the RSS rust crate to do the actual RSS parsing Speed...
New
OvermindDL1
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
132 13487 106
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
achempion
Hi, I would like to tell about my initiative to further maintain and develop Waffle project which is the fork of Arc library. The progre...
New
tfwright
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New
bryanjos
Hi, I just published version 0.23.0 of Elixirscript. Most of the changes are around JavaScript interop now that Elixirscript uses the ...
New
tmbb
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
New
KallDrexx
For a good number of months I've been working on creating a very basic RTMP live video streaming server. Now that I have a very, very ba...
New
mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Sub Categories:

We're in Beta

About us Mission Statement