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.

First 4 of 4 Posts Switch mode

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

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
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
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New
kip
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

Other Trending Topics Top

juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New

We're in Beta

About us Mission Statement