Search code examples
elixirlager

How to setup lager on elixir project


I would like to use lager from elixir project.

My current mix.exs is following.

def application do
  [erl_opts: [parse_transform: "lager_transform"]]
end

defp deps do
  [{:lager, github: "basho/lager"}]
end

I would like to output log to file by using lager. How can I set log file path? (Can I change this file path after starting application?)

And, I would like to devide log file by using tracing How can I set above configuration?


Solution

  • Here's a minimal setup for lager with Elixir:

    # mix.exs
    def application do
      [
        applications: [:lager],
        erl_opts: [parse_transform: "lager_transform"]
      ]
    end
    
    defp deps do
      [{:lager, github: "basho/lager"}]
    end
    
    # config/config.exs
    config :lager,
      log_root: '/var/log/hello',
      handlers: [
        lager_console_backend: :info,
        lager_file_backend: [file: "error.log", level: :error],
        lager_file_backend: [file: "console.log", level: :info]
      ]
    

    As you can see, the log_root option will allow you to set a log directory at compile time. I've recreated the example configuration from the lager docs above, you should be able to take it from here and specify the configuration options you need.

    There's no way to change the log directory and/or log level during runtime, which I reckon is one of the downsides of lager. I have no experience with tracing but the example above should enable you to set the necessary config options.