Multiple Loggers In Elixir

How to have multiple Loggers in Elixir and Log to specific Logger by the message.

Import the LoggerFileBackend into your mix.exs

     {:logger_file_backend, "~> 0.0.10"}

Add multiple Loggers to your config.exs

config :logger,
  backends: [:console, {LoggerFileBackend, :import_log}]

config :logger, :console,
  format: "$time $metadata[$level] $message\n",
  metadata: [:request_id]

config :logger, :import_log,
  path: "/my/project/path/import.log",
  level: :info,
  metadata_filter: [component: 1]

In your application, specify component: 1 you want to write to the import_log. This log message will log to both the console and the import_log.

   Logger.info("My Import_log Statement", component: 1)

Exclude the metadata when you would like to log normally. This log message will only log into the console

   Logger.info("My Normal Log Statement")

 


Home Office

Cherry Hill, NJ

North New Jersey Office

Kinnelon, NJ

Philadelphia Office

Media, PA

office Hours

Mon - Fri : 8am to 6pm

Social Media