4

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?

tamagohan2
  • 445
  • 4
  • 15

1 Answers1

4

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.

Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
  • Thank you for reply. But, following code raise UndefinedFunctionError.. `:lager.error('Some message')` Should I start lager? I try to use `:lager.start` But, not work... – tamagohan2 Oct 27 '15 at 07:52
  • On starting app, following error message is displayed.. `2015-10-27 17:21:15.671 [error] <0.231.0> Supervisor 'Elixir.Logger.Supervisor' had child undefined started with 'Elixir.Logger.Watcher':watcher(error_logger, 'Elixir.Logger.ErrorHandler', {true,false,500}, link) at <0.236.0> exit with reason normal in context child_terminated` – tamagohan2 Oct 27 '15 at 08:23
  • Looks like you using the Elixir logger as well? I think it would be better to stick to one logging mechanism. You don't need to start lager manually, it will be done for you if you put it in the applications list in mix.exs. – Patrick Oscity Oct 27 '15 at 08:28
  • I commented out logger.( `#:logger,`) from application on mix.exs. but, `:lager.error('Some message')` raise `(UndefinedFunctionError) undefined function: :lager.error/1`... – tamagohan2 Oct 27 '15 at 08:38
  • 1
    To log an error do `:lager.log(:error, self, "message")` – Patrick Oscity Oct 27 '15 at 08:42
  • Thank you! I could output log! – tamagohan2 Oct 27 '15 at 08:54