arnomi

arnomi

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.

Showing Posts 1 to 4

arnomi

arnomi OP

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.

Maxximiliann

Maxximiliann

Looks like a great lib! How would one configure automated log rotation on file size?

— All posts loaded —

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
CodeSync
:microphone: ElixirConf 2026 - Call for Talks is open! We’re heading to Chicago :united_states: :round_pushpin: In person + virtual :d...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews