Rails3 ActiveSupport Notification subscription – Rails3 Tricks #03

Hello there! Almost 10 months without any post due to a very busy period: I moved from Paris to San Francisco.
Ok, so now facts has been said, this will hopefully change! I plan to write a new series of Rails3 posts, starting with a quick one on the new way Rails handles notifications.

The new version of ActiveSupport shipped with Rails brings along a new notification system which is heavily used by Rails3 internarlly. Rails doesn’t write directly to logs anymore, instead of that, it publishes a notification which can be caught by any observers.

In production, Rails will by default publish deprecation warnings through this notification system. Last week I was looking for a way to play with that and didn’t find a clear example on the web, so here is a small snippet of code if you are also looking for a nice way to log your deprecation warnings:


# In config/initializers/deprecations_logger.rb
DeprecationLogger = Logger.new(Rails.root.join('log/deprecations.log'))

ActiveSupport::Notifications.subscribe(/deprecation/) do |type, date,b,c, event|
  DeprecationLogger.info("#{date} - #{event[:message]}")
end
  • Share/Bookmark

Comments (5)

Thanks for the great post !

May I ask what refers to the “b” and “c” block parameters ?

Take it easy ;-)

Ahaha :)
Sure, it was some timestamps if I’m correct. But not useful in our example ;)

Thanks for the answer budy !

Speaking of your moving, you know Paris miss you right ?

:-P

This is sweet :)

+1 Zitooon

Write a comment