I use Devise in Rails 3. I want to see name of current_user in production.log.
I would like to configure rails like this:
config.log_tags = [:user_name]
I use Devise in Rails 3. I want to see name of current_user in production.log.
I would like to configure rails like this:
config.log_tags = [:user_name]
And Now for Something Completely Different...
https://github.com/roidrage/lograge/issues/23#issuecomment-11709861
I just tried this with Rails 4, latest Devise and Lograge.
Here is the cookie decrypter I use in Rails 5.1 to tag the logs with the
user_id
, which is stored in the cookie. It basically lets me access the controller equivalent ofsession[:user_id]
from a raw cookieenvironments/production.rb:
app/models/cookie_decrypter.rb:
Added 2013-03-07:
In Rails 4 encrypted_cookie_store is the default session store. This is how you can access session data:
And it looks like warden_data looks differently in my new app, e.g.:
[[542], "$2a$10$e5aYxr/PIp6OOj8jzE7mke"]
, where first item is user id.Here's my current snippet: https://gist.github.com/wojt-eu/5109643
Previous version:
This is what I came up with:
_qnaire_session
can be replaced withinstance.config.session_options[:key]
or via singleton:Rails.application.config.session_options[:key]
I have ProviderUser model, hence
warden.user.provider_user.key
. I suppose with User model this would bewarden.user.user.key
.It's messy, but it does not affect normal authentication process, middleware stack order etc. If it breaks during some update only tagging logs will be affected, which I should quickly notice while looking at development logs.
What almost worked for me (Rails 3.2.22.2) is the answer from here: http://benjit.com/rails/logger/2016/02/26/getting-admin-user-into-rails-logfile/
This assumes that the
cookie_jar
object responds toencrypted
. However it wasn't the case for me. What ultimately worked for me is as follows:config/initializers/logging.rb
:As a quick and ugly workaround, maybe one could log a second line after the request has been processed.
This 500 ServerError has been presented by: #{username}
Here's what I just added to
config/initializers/logging.rb
:This is for Authlogic. What you need to do might vary, so you should really dig in and see what your data exposes to you already.
Step 1:
See what the
req
object has available. Add this toconfig/initializers/logging.rb
:Then hit a page, and see what gets dumped.
Step 2: See if your cookie jar has enough information, using the same technique:
(hit a request)
An aside: Don't worry about putting in usernames/emails into the logs - user ID is good enough, and you can look it up in the database to get any extra metadata you need.