Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2012
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. How to log user_name in Rails?
  2. config.log_tags = [:user_name]
  3.  
  4. config.log_tags = [ lambda {|req| req.cookie_jar["_session_id"].to_s ]
  5.  
  6. config.log_tags = [ lambda {|req| req.cookie_jar["user_name"] || 'Noone' } ]
  7.  
  8. config.log_tags = [ lambda { |req| user = req.env['warden'].user; user && user.name || 'Noone'; } ]
  9.  
  10. config.log_tags = [ lambda { |req| user = User.find_by_id(req.env['rack.session']['user_id']); user && user.name || 'Noone'; }
  11.  
  12. Rails.configuration.log_tags = [
  13. :uuid, # request UUID
  14. lambda { |req|
  15. # Credentials are (currently) in the format of:
  16. #
  17. # <session_hash>::<user_id>
  18. #
  19. # So we have to split by '::' to obtain the user_id for logging.
  20. #
  21. # This will just output "User: nil" if there is no current session.
  22. "User: #{req.cookies['user_credentials'].to_s.split('::')[1]}"
  23. }
  24. ]
  25.  
  26. Rails.configuration.log_tags = [
  27. lambda { |req|
  28. req.inspect
  29. }
  30. ]
  31.  
  32. Rails.configuration.log_tags = [
  33. lambda { |req|
  34. req.cookies.inspect
  35. }
  36. ]
  37.  
  38. config.middleware.delete(ActionDispatch::Cookies)
  39. config.middleware.delete(ActionDispatch::Session::CookieStore)
  40. config.middleware.insert_before(Rails::Rack::Logger, ActionDispatch::Session::CookieStore)
  41. config.middleware.insert_before(ActionDispatch::Session::CookieStore, ActionDispatch::Cookies)
  42.  
  43. Rails.configuration.log_tags = [
  44. proc do |req|
  45. if req.session["warden.user.user.key"].nil?
  46. "Anonym"
  47. else
  48. "user_id:#{req.session["warden.user.user.key"][1][0]}"
  49. end
  50. end
  51. ]
  52.  
  53. [Anonym] Served asset ...
  54.  
  55. [user_id:1] Served asset ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement