Created
April 22, 2014 13:15
-
-
Save meltingice/11178762 to your computer and use it in GitHub Desktop.
Add user ID to Rails logs with log tags and Authlogic
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
MyApp::Application.configure do | |
config.log_tags = [UserLogger.get] | |
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
# Change UserSession and User models, if needed. | |
module UserLogger | |
extend self | |
def get | |
lambda { |req| | |
return 0 if req.cookies[UserSession.cookie_key].nil? | |
token = req.cookies[UserSession.cookie_key].match(/[A-Z0-9]+/i)[0] | |
Rails.cache.fetch [UserSession.cookie_key, token] do | |
user = User.where(persistence_token: token).pluck(:id) | |
user || 0 | |
end | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment