flex_logger v0.2.1 FlexLogger View Source

FlexLogger is a flexible logger (backend) that adds module/application specific log levels to Elixir’s Logger.

FlexLogger brings the following additions to the table:

  • Configuration of log levels per application, module or even function

  • Possibility of having multiple logger configurations for different applications or modules

Configuration

FlexLogger is configured as a named backend to Logger. Following is an example configuration of a single FlexLogger in combination with a :console logger

 config :logger,
   backends: [{FlexLogger, :logger_name}]

 config :logger, :logger_name,
   logger: :console,
   default_level: :debug, # this is the loggers default level
   level_config: [ # override default levels
     [module: Foo, level: :info]
   ],
   format: "DEV $message" # backend specific configuration

The configuration for FlexLogger as well as the underlying actual log backend are under the named config. FlexLogger knows the following configuration options:

  • logger: The actual logger backend to use. In case of Logger.Backend.Console you can also use the :console shortcut.

  • default_level: The default log level to use. This should be one of [:off, :debug, :info, :warn, :error]. In addition to the standard four log levels the :off level allows to turn of logging for either individual modules or if used as default_level to turn of logging per default to then only enable logging for individual modules or applications

  • level_config: A list of log level configurations for modules and applications. Each entry should be a keyword list. If only a single entry is present the config can be simplified to only a single keyword list like

    level_config: [application: :my_app, level: :info]

    Possible configuration options are :application, to match the application, :module to match a prefix of a module, :function to match a particular function or :message to match a particular message (see below). The level is set via :level. The following configuration

    level_config: [
      [application: :my_app, module: Foo.Bar, level: :debug]
      [function: "some_function/1", level: :error]
    ]

    would set the log level for any module that starts with Foo.Bar in application :my_app to :debug. In addition the log level for any function called some_function and that has arity 1 is set to :error. Note that if a key (ie., :application, :module or :function) is not present then it matches anything.

    Via the :message key you can define specific log levels based on the content of the logged message. This is particularly useful in case of filtering out log messages coming from modules that use Erlang’s :error_logger in which case no other metadata is available. In case a string is provided for :message then FlexLogger checks whether the log message contains the provided string. In case a regular expression is given the log message is matched against the regular expression. In case a function with arity 1 is provided, the message is passed to that function which should return a boolean value. Following is an example config that matches the log message against a regular expression

    level_config: [
      [message: ~r/foo/, level: :debug]
    ]

Backend specific configuration

The entire configuration is passed onto the actual logger for configuration. For example, if you configure the LoggerFileBackend which takes a path parmameter you can do this as follows:

config :logger,
   backends: [{FlexLogger, :foo_file_logger}]

config :logger, :foo_file_logger,
     logger: LoggerFileBackend, # The actual backend to use (for example :console or LoggerFileBackend)
     default_level: :off, # this is the loggers default level
     level_config: [ # override default levels
       [module: Foo, level: :info] # available keys are :application, :module, :function
     ],
     path: "/tmp/foo.log", # backend specific configuration
     format: "FOO $message" # backend specific configuration

Logger Specific Configuration

Logger specific configuration, i.e., not backend specific configuration needs to be specified at the usual place, for example

 config :logger,
    handle_otp_reports: true,
    handle_sasl_reports: true

Supported Backends

FlexLogger has been tested with :console and LoggerFileBackend but should also work with other logging backends.

Link to this section Summary

Functions

Callback implementation for c::gen_event.code_change/3

Updates configuration of flex_logger and underlying logger. Underlying logger may not be changed

Callback implementation for c::gen_event.handle_event/2

Callback implementation for c::gen_event.handle_info/2

Callback implementation for c::gen_event.init/1

Callback implementation for c::gen_event.terminate/2

Link to this section Functions

Link to this function code_change(old_vsn, state, extra) View Source

Callback implementation for c::gen_event.code_change/3.

Link to this function handle_call(opts, state) View Source

Updates configuration of flex_logger and underlying logger. Underlying logger may not be changed.

Link to this function handle_event(opts, state) View Source

Callback implementation for c::gen_event.handle_event/2.

Link to this function handle_info(opts, state) View Source

Callback implementation for c::gen_event.handle_info/2.

Callback implementation for c::gen_event.init/1.

Link to this function terminate(reason, state) View Source

Callback implementation for c::gen_event.terminate/2.