Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add current_user support #23

Closed
mperham opened this issue Nov 24, 2012 · 8 comments
Closed

Add current_user support #23

mperham opened this issue Nov 24, 2012 · 8 comments

Comments

@mperham
Copy link

mperham commented Nov 24, 2012

I'd love it if lograge would auto-support current_user logging, something like:

 config.lograge.custom_options = lambda do |event|
    { :uid => event.request.controller.current_user.id }
  end

We find it vital for debugging and 'watching' how a user got into a particular state.

@roidrage
Copy link
Owner

I've started working on a branch that brings support for tagged logging to lograge, which would be much more fitting in this case, as it'd utilize something Rails already has support for.

@freegenie
Copy link

I've been looking for a solution to this issue because that would be tremendously useful. You can use a Rails' hook method in Instrumentation called append_info_to_payload. Just override that in your ApplicationController:

def append_info_to_payload(payload)
    super
    payload[:uid] = current_user.id if logged_in? 
end

Then you can use:

config.lograge.custom_options = lambda do |event|
    { :uid => event.payload[:uid] }
end

@nathany
Copy link

nathany commented Feb 11, 2014

We are using config.log_tags in Rails to add the user_id. If we switch to lograge, should we use custom_options instead?

@coreymartella
Copy link

@nathany I'm going thru this issue myself. The main thing I'm noticing is that lograge's payload would only apply to things it is subscribed to (namely process_action.action_controller).

However if you have any generic logging happening elsewhere the application you want the tags attached to you will still need to use the tags (i.e a log even happening within model code where you want the current user_id included in the log method w/o knowing at the model level what it is.

But if you just want it for the general "request completed" lograge will do the trick

@nathany
Copy link

nathany commented Feb 18, 2014

After switching to append_info_to_payload, I'm actually likely it better than what we were doing with config.log_tags before setting up lograge (grabbing the session id and doing a separate lookup).

@fabn
Copy link

fabn commented May 16, 2015

+1000 for this feature builtin in lograge.

@JeremiahChurch
Copy link

just want to say freegenie's option from '12 still works great in Rails 4.2 / Ruby 2.2

for Devise there are a few ways to check for logged in - I went with

def append_info_to_payload(payload)
    super
    payload[:uid] = current_user.id unless current_user.nil?
end

@sairam
Copy link

sairam commented Feb 20, 2017

@tongboy instead of current_user.nil? - you should use user_signed_in?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants