shadabahmed / logstasher

Awesome rails logs
MIT License
818 stars 136 forks source link

Adding custom fields at the Controller level #128

Closed mikesea closed 7 years ago

mikesea commented 7 years ago

Hey, thanks for this great gem. I'm trying to figure out the recommended approach for adding custom fields at the controller level. For instance, we want to log some custom fields in our Admin controllers. The docs suggest you should do this in an initializer like so:

config/initializers/logstasher.rb

LogStasher.add_custom_fields do |fields|
    fields[:admin_id] = current_admin.id if request.path =~ /^\/admin/
end

Instead of doing the path matching, I was hoping to add custom fields directly in our AdminController. I landed on this approach (almost identical to lograge's approach):

app/controllers/admin_controller.rb

def append_info_to_payload(payload)
    super
    LogStasher.store[:admin_user_id] = current_admin.id
 end

This works for well for us, but since I couldn't find any documentation around this use-case I wanted to see if you recommend a different approach. Thanks in advance!

shadabahmed commented 7 years ago

Hi - this is a nice approach too. I'll update the documentation soon