Steven
Steven

Reputation: 4963

How to create a special logger

I'm trying create a log specifically for logging user account activity. I created this inside the initializer folder:

class UserTraceLogger < Logger
  def format_message(severity, timestamp, progname, msb)
    "#{timestamp.to_formatted_s(:db)} #{severity} #{msg}\r\n"
  end
end

logfile = File.open("#{RAILS_ROOT}/log/user.log", 'a')
userlog = AuditLogger.new(logfile)
userlog.sync = true

I would like to be able to then use it in my code with:

userlog.info 'my message'

Right now I am getting this error message when I start the rails server:

config/initializers/usertrace_logger.rb:10:in `<top (required)>': uninitialized constant AuditLogger (NameError)

I'm pretty new to rails. I'm not entirely sure this is the correct way to create a global instance or know what exactly I'm missing.

What am I doing wrong?

Upvotes: 2

Views: 417

Answers (1)

Erik Peterson
Erik Peterson

Reputation: 4311

Did you mean UserTraceLogger.new(logfile) instead of AuditLogger.new(logfile)?

Otherwise you may be missing require 'audit_logger' somewhere.

Upvotes: 4

Related Questions