Skip to content

Instantly share code, notes, and snippets.

@meltingice
Created April 22, 2014 13:15
Show Gist options
  • Save meltingice/11178762 to your computer and use it in GitHub Desktop.
Save meltingice/11178762 to your computer and use it in GitHub Desktop.
Add user ID to Rails logs with log tags and Authlogic
MyApp::Application.configure do
config.log_tags = [UserLogger.get]
end
# 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