0

I am using rails application, my rails applications logs are should be stroed in mongoDB.

I am logging each and every controller's method call and its params its date of call etc.

Here is my code in my application controller, to log the information

db = Mongo::ReplSetConnection.new([MONGODB_PROP['host'],MONGODB_PROP['port']],:refresh_mode => :sync).db(MONGODB_PROP['database'])
au = db.authenticate(MONGODB_PROP['username'],MONGODB_PROP['password'])

if au
  coll = db.collection("log_info")
  doc = { :tab_name => "#{params[:controller}",:date =>"#{Time.now}"}
  coll.insert(doc)
end

Obviously, my code has need some standard issues. From my implementation each time the method called happend the mongoDB connection is established .So automatically connection object is increased & it will become performance issue. i want the Single DB connection whenever it requires i need to get the connection object and perform the insert operation. How can i do this.

Please help me on this.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
palani
  • 4,661
  • 8
  • 31
  • 36

1 Answers1

0

The easiest way is to use Mongoid and create a LogInfo class. Let mongoid handle your database connections, and you simply call:

LogInfo.create(:tab_name => "#{params[:controller}",:date =>"#{Time.now}")
Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109