Skip to content

Instantly share code, notes, and snippets.

@lmmendes
Last active February 8, 2017 15:23
Show Gist options
  • Select an option

  • Save lmmendes/681ec8c08219b108325414cb593516d5 to your computer and use it in GitHub Desktop.

Select an option

Save lmmendes/681ec8c08219b108325414cb593516d5 to your computer and use it in GitHub Desktop.
Rails tagged logger with user id
#
# This Tag logger adds users that to the request example:
# 2017-02-08 15:07:13.227 [fyi] [ce3cfbef-792d-4606-b8d4-927cd526fe7f] Completed 200 OK in 4355ms
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]
if session_data.present? && session_data.has_key?('warden.user.user.key')
session_data['warden.user.user.key'][0][0]
else
"guest"
end
end
end
Rails.configuration.log_tags = [
->(req){
WardenTaggedLogger.extract_user_id_from_request(req)
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment