# Add this to your application config.
config.app_middleware.insert_after("Warden::Manager", "Devise::TaggedLogging")
Last active
August 31, 2017 02:25
-
-
Save lucasmazza/5020390 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Devise | |
| class TaggedLogging | |
| def initialize(app) | |
| @app = app | |
| end | |
| def call(env) | |
| if logger.respond_to?(:tagged) | |
| logger.tagged(compute_devise_tags(env)) { @app.call(env) } | |
| else | |
| @app.call(env) | |
| end | |
| end | |
| private | |
| def compute_devise_tags(env) | |
| warden = env['warden'] | |
| Devise.mappings.keys.map do |scope| | |
| if record = warden.user(:scope => scope) | |
| ["current #{scope}", record.to_key].join(": ") | |
| end | |
| end.compact | |
| end | |
| def logger | |
| Rails.logger | |
| end | |
| end | |
| end |
This file contains hidden or 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 Devise | |
| class TaggedLogging | |
| def initialize(app) | |
| @app = app | |
| end | |
| def call(env) | |
| if logger.respond_to?(:tagged) | |
| logger.tagged(compute_devise_tags(env)) { @app.call(env) } | |
| else | |
| @app.call(env) | |
| end | |
| end | |
| private | |
| def compute_devise_tags(env) | |
| serializer = Warden::SessionSerializer.new(env) | |
| Devise.mappings.keys.map do |scope| | |
| key = serializer.key_for(scope) | |
| if session_data = serializer.session[key] | |
| ["current #{scope}", session_data[1]].join(": ") | |
| end | |
| end.compact | |
| end | |
| def logger | |
| Rails.logger | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome! Thanks!