Manage multiples logs with rails (en)
When you want to log something in rails, you probably write this :
And your log appears in your log file (for exemple developement.log). Great.
But, the problem is that you fill this file with a log information that 3/4 of time you don’t need.
Today, you want to clearly visualize your logs in an admin panel, as follow :
- All logs interfacing with an external
- All logs concerning the back-office
- All logs concerning an API
TO do this, we have to save logs somewhere and find a way to distinguish differents types of logs.
We clearly do not want to add custom code to manage this. So, what should we do?
Log subscriber is your friend
Log subscriber is an object that consume ActiveSupport::Notifications.
The idea is to replace all our calls of:
By calls to ActiveSupport::Notifications :
The previous code overrides the 3 main logs methods and sends an ActiveSupport::Notifications when theses methodes are called :
Ok, great. Now, We need to create a subscriber that will consume this notification:
This log subscriber simply formats the notifications it receives. Once message is formatted, it makes a call to the base log rails through the method ‘send’. You can send the log in another place simultaneously. For instance, you can store the log in ElasticSearch.
And now, we test and…. Nothing! nitch, nada!
Indeed, there is no indication that the notification should be consumed by the subscriber. There’s a missing little line :
That’s it!
Now, you can send your unified logs in several places (Default rails logger, ElasticSearch, file…). Create a new subscriber and register it.