1

Currently I'm developing a rails project and logging is required in this project, so I used aquarium gem for doing the trick. But the problem is when printing a logging statement I must print which person who made this action, for example

include Aquarium::Aspects
include Aquarium::DSLMethods
class LoggerController < ApplicationController



Aspect.new :after, :calls_to => :index,:in_types => [ReadingsController] do |jp, obj, *args|
  joinpoint=jp.target_type.to_s
  write_logs_to_file( "Action => list of all readings is shown, By => #{User.find(session[:userId]).name}")
 end
end

The problem is that class logger controller has no access to the session object, the question is how to wrap around this problem ?

4

1 回答 1

0

提前感谢@all 我已经通过使用 Thread 对象解决了这个问题,所以我的记录器类看起来像这样

include Aquarium::Aspects
include Aquarium::DSLMethods
class LoggerController < ApplicationController
def current_user
Thread.current[:user]
end

def self.current_user=(user)
Thread.current[:user] = user
end


Aspect.new :after, :calls_to => :index,:in_types => [ReadingsController] do |jp, obj, *args|

  write_logs_to_file( "Action => list of all readings is shown, By => #{Thread.current[:user]}")
 end
end

其中 Thread.current[:user] 在登录函数期间由 userName 设置

于 2013-11-07T09:59:48.543 回答