Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

possible to access sensible logger inside class? #138

Open
josh-m-sharpe opened this issue May 27, 2022 · 1 comment
Open

possible to access sensible logger inside class? #138

josh-m-sharpe opened this issue May 27, 2022 · 1 comment

Comments

@josh-m-sharpe
Copy link

josh-m-sharpe commented May 27, 2022

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
@josh-m-sharpe
Copy link
Author

josh-m-sharpe commented May 27, 2022

I added this hackery which allows my in-class logging to work, but seems hackeryish.

  before do
    $logger ||= logger
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant