Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logger not working Elixir

I am trying to use the Logger.debug/1 for my web-project. I am unable to get past this error message even though I did make additions to the configuration file (config.exs) present in /myApp/config.

** (CompileError) web/controllers/api/app_controller.ex:36: you must require Logger before invoking the macro Logger.debug/1

I added this particular configuration at the end.

  config :logger,
    backends: [:console],
    compile_time_purge_level: :info

I took help to make this addition from http://elixir-lang.org/docs/master/logger/Logger.html

like image 744
srajappa Avatar asked Nov 26 '15 00:11

srajappa


People also ask

What is the use of elixir logger?

Elixir provides Logger for logging in Elixir applications. Using Elixir Logger is only a little more effort than that, but it’s much more elegant and flexible. As well as being useful for debugging, logging can also provide you with more – and better structured – information about the state and health of your application.

How do I change the default log level in Elixir?

Open the config.exs file, which is the boss of your app configuration and locate the config :logger line. Take a brief look at these lines. This is Elixir’s Logger configuration. Whatever you change here, is going to be applied to all your app’s logs. Start by changing the default log level of the entire app.

How do I log an error in Elixir?

IO.puts (:stderr, "This is an error...") IO.inspect (self ()) This is the simplest form of logging in Elixir. The puts function takes two arguments, the first being the target destination of the log message (as an atom) and the second, the message itself.

What is the best way to debug in Elixir?

Elixir programmers will often use IO.inspect/2 (guilty!) in their code as a quick and convenient debugging tool. Elixir provides Logger for logging in Elixir applications. Using Elixir Logger is only a little more effort than that, but it’s much more elegant and flexible.


2 Answers

You need to add require Logger in your module definition, e.g. right after defmodule AAA...

For example: https://github.com/22cans/exsyslog/blob/2b9ea2be7d7fcc17eab061425b6cd4fad8643996/examples/example1/lib/example1.ex

like image 155
Zepplock Avatar answered Oct 26 '22 11:10

Zepplock


Got it ! The reason why it didn't work because Logger was not found in the same module. So one has to import the module and functions in-order to use them.

So I used

require Logger

and this solved the problem. The program started working again.

like image 43
srajappa Avatar answered Oct 26 '22 13:10

srajappa