Skip to content

possible to access sensible logger inside class? #138

@josh-m-sharpe

Description

@josh-m-sharpe

Think my question is more about scoping in a sinatra app, but here goes. When hitting /bad, we get a NameError that logger is undefined inside Work.do_work. Which I suppose makes sense, we didn't pass logger in to the do_work method.

I've tried the likes of defining a global logger var $logger = Logger.new(STDOUT) and passing that to sensible_logging -- and the using $logger inside my Work class - but then I don't get the benefits of sensible_logging. That just produces raw, untagged logging.

So my question is - any way to allow logger to be globally available AND have the benefits of sensible logging?

Cheers - thanks!

class Work
  def self.do_work
    logger.info 'whatever'
  end
end

class App < Sinatra::Base
  register Sinatra::SensibleLogging

  sensible_logging(logger: Logger.new($stdout))
  configure :production do
    set :log_level, Logger::INFO
  end

  get '/bad' do
    Work.do_work
    'tldr'
  end

  get '/good' do
    logger.info 'works fine'
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions