Last active
September 19, 2018 00:36
-
-
Save wkrsz/5109643 to your computer and use it in GitHub Desktop.
Example on how to extract Devise user ID for Rails TaggedLogger.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.log_tags = [ | |
:remote_ip, | |
->(req){ | |
if user_id = WardenTaggedLogger.extract_user_id_from_request(req) | |
"#" + user_id.to_s | |
else | |
"guest" | |
end | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module WardenTaggedLogger | |
def self.extract_user_id_from_request(req) | |
session_key = Rails.application.config.session_options[:key] | |
session_data = req.cookie_jar.encrypted[session_key] | |
warden_data = session_data["warden.user.user.key"] | |
warden_data[0][0] | |
rescue | |
nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Never use begin rescue in a critical path like this one: