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.

13 Likes

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]
  ]
3 Likes

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

1 Like

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.

1 Like

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