Reputation: 45943
Within a request, the Sinatra logger works fine:
get '/' do
logger.info "loading data"
...
However, from within a model, it doesn't work. The model does not have access to the logger:
class Foo
def self.do_something
logger.info "loading data"
end
end
get '/' do
Foo.do_something
...
How to access the logger from within a model?
Upvotes: 0
Views: 386
Reputation: 12251
@Bjoern's answer will work, but I suppose that would make Foo
a controller and not a model. Another way would be to initialise a logger before Sinatra sets one up for you and then pass it to it to use. One way of doing this could be:
class Foo
def self.logger
@logger ||= Logger.new STDOUT
end
end
configure do
set :logger, Foo.logger
end
Upvotes: 1
Reputation: 6936
Seems you want to do
class Foo < Sinatra::Base
because logger is not available in the scope of the Foo class as you defined it.
Upvotes: 1