nathanl / authority

*CURRENTLY UNMAINTAINED*. Authority helps you authorize actions in your Rails app. It's ORM-neutral and has very little fancy syntax; just group your models under one or more Authorizer classes and write plain Ruby methods on them.
MIT License
1.21k stars 67 forks source link

How can I silence Authority.logger in test environment? #101

Closed natebird closed 9 years ago

natebird commented 9 years ago

Can I add something to my spec_helper.rb file to silence the Authority logging output?

I tried Authority.logger = nil but that isn’t possible. Any other ideas?

nathanl commented 9 years ago

Hi, Nate!

Authority expects to have its logger set to an object that is or acts like an instance of Logger - specifically, that responds to .warn (currently, that's the only method Authority calls on it).

So you could make your own dummy object, if you like:

o = Object.new
def o.warn(*args)
  # do nothing
end

...but a more typical way to do it would be (at least on a Linix or Mac system) would be to use Logger.new('/dev/null').

If you ran the Rails generator, you can look in config/initializers/authority.rb for more info. That initializer would be the most straightforward place to change it, using a conditional like config.logger = Rails.env.test? ? Logger.new('/dev/null') : Logger.new(STDOUT).

What you tried to do looks sensible, but doesn't work because Authority doesn't have a logger= method, so you can't set it directly. You should be able to do

config = Authority.configuration
config.logger = my_logger
Authority.configuration = config

but that doesn't have any effect, because after the first call, the Authority module hangs on to the first logger it saw instead of consulting the current configuration object. I can't see any reason for that, so I'll change it.

nathanl commented 9 years ago

I just pushed a commit to master to make this easier - want to try it out and let me know what you think?

natebird commented 9 years ago

Thank you for the quick response. I’ll try out master and see how it goes.

nathanl commented 9 years ago

Closing - let me know if you have further questions or issues.

nathanl commented 9 years ago

See version 3.1.0

natebird commented 9 years ago

That is just what I was looking for. Thanks!