While participating (mostly working on getting some existing apps running under 3.0) in the Rails BugMash this weekend I noted there was recently a subscription system for logging added by José Valim.
I usually enable some hack to watch SQL while I’m in
script/console so I thought I’d see if I could use these new subscription models to see the passing sql in growl. I came up with this (sorry for the lack of formatting, I’ll fix this up later) which you can put in
development.rb or whatever method you prefer for development-mode-only code.
You enable the growling of SQL via a call to SQLGrowler::enable in the script/console session. You will also need to require the ‘g’ gem in development mode as well.
I have a couple of problems with this:
- There is no way to turn it off once it is on.
- It seems there is only one subscriber per namespace so I inherit and ‘super’ — maybe not the best way, but it doesn’t clobber the existing logging facility. Thanks to lenary in the #railsbridge IRC for that suggestion.
- Growling all your sql calls may be more annoying than helpful
- This is probably a horrible use of
Rails::Subscriberfor which I will be publicly admonished for using.
I haven’t asked the core team what the plan for
Rails::Subscriber is yet. This may be a nice way to observe/log events coming from the core library (or perhaps even your own code)