Skip to content

Instantly share code, notes, and snippets.

@tbuehlmann
Created January 22, 2013 17:04
Show Gist options
  • Save tbuehlmann/4596310 to your computer and use it in GitHub Desktop.
Save tbuehlmann/4596310 to your computer and use it in GitHub Desktop.
# app/controllers/sessions_controller.rb
class SessionsController < ApplicationController
# ...
def create
user = User.authenticate(params[:username], params[:password])
if user
session[:user_id] = user.id
ActiveSupport::Notifications.instrument('user.logged_in', :username => user.name)
redirect_to root_url, :notice => '...'
else
redirect_to login_url, :alert => '...'
end
end
end
# config/initializers/user_transaction_logger.rb
class FormattedLogger < Logger
def format_message(severity, timestamp, progname, msg)
"#{timestamp.strftime('%d.%m.%Y %H:%M:%S')} #{msg}\n"
end
end
UserTransactionLogger = ActiveSupport::TaggedLogging.new(FormattedLogger.new(File.join(Rails.root, 'log', 'user_transaction.log')))
ActiveSupport::Notifications.subscribe('user.logged_in') do |name, start, finish, id, payload|
UserTransactionLogger.tagged(payload[:username], 'User') do
UserTransactionLogger.info 'logged in'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment