Simple question about per class loging

Hi all, two classes, each with its own logger, severity setup is different for the two but in this case Debug logs nothing whilst Trace is ok

https://play.crystal-lang.org/#/r/gcc7

1 Like

The problem is that Log.setup overrides previous configuration. So Log.set "bar", :trace replaces the configuration from the previous line. You’ll want to use the yielding Log.setup method in order to configure each class if you want the configuration to be unique for each source.

Also as a bit of a shortcut, you can also do like LOG = ::Log.for self.

Mmm I tried these solutions but still don’t get no logs

require "log"

class Foo
  LOG = ::Log.for self
  
  def doit
    LOG.debug {"in Foo"} # Nothing logged
  end
end

class Bar
  LOG = ::Log.for self
  
  def doit
    LOG.trace {"in Bar"}
  end
end

Log.setup do |l|
  l.for("foo").level = Log::Severity::Debug
  l.for("bar").level = Log::Severity::Trace
end

# The above does not work either
#Log.for("foo").level = Log::Severity::Debug
#Log.for("bar").level = Log::Severity::Trace

Foo.new.doit
Bar.new.doit

You need to do something like this, is an example of it in the API docs:

Log.setup do |l|
  backend = Log::IOBackend.new
  
  l.bind "foo", :debug, backend
  l.bind "bar", :trace, backend
end
2 Likes

Thanks (again), as always ;-)

Yes that was here

Log.setup do |c|
  backend = Log::IOBackend.new

  c.bind "*", :warn, backend
  c.bind "db.*", :debug, backend
  c.bind "*", :error, ElasticSearchBackend.new("http://localhost:9200")
end

But I did not connect this example to what I was looking for… I don’t understand myself ;-)