這是我的日誌,我有一個用戶控制器創建這樣的動作:
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "Welcome, #{@user.username}"
redirect_to(:controller => "users", :action => "home")
session[:id] = @user.id
else
render("home")
end
end
現在我想記錄一個創建用戶,那麼我這樣做:
首先創建User.rb(模型)的AuditLogger類:
class User < ActiveRecord::Base
...some stuff other....
class AuditLogger < Logger
def format_message(severity, timestamp, progname, msg)
"#{timestamp.to_formatted_s(:db)} #{severity} #{msg}\n"
end
end
釷恩回到控制器(users.rb的)
def create
@user = User.new(params[:user])
if @user.save
logfile = File.open("#{Rails.root}/log/my_log.log", 'a')
audit_log = AuditLogger.new(logfile)
audit_log.info "#{@user.firstname} was created successfully"
redirect_to(:controller => "users", :action => "home")
else
render("home")
end
end
而且你需要在一個名爲my_log.log日誌目錄下創建文件。希望它應該能夠登錄。我知道它不是最好的解決方案,而且我有更好的方法去做,但是當時我需要緊急工作,所以我堅持下去。
結帳這些鏈接:
rails logging tips
alternative logging solution