le0pard/mongodb_logger

Can't add metadata to logs!

Closed this issue · 8 comments

I'm trying to add some very basic data to the logs in my application controller (i.e. for all actions). I've put this in place (confirmed that the method is running) but nothing is showing up in Mongo DB!

before_filter :add_metadata_to_mongolog

....

  private

  def add_metadata_to_mongolog

    # make sure we're using the MongodbLogger in this environment
    if Rails.logger.respond_to?(:add_metadata)
      if current_user
        id = current_user.id
        email = current_user.email
      else
        id = nil
        email = nil
      end

      Rails.logger.add_metadata(:additional_data => {:user_id => id, :user_email => email, :logged_in => !email.nil?})
    end
  end

Do you add in top of your controller this code?

include MongodbLogger::Base 

The mongodb settings setuped right?

Application controller:

 # MONGO LOGGING
  include MongodbLogger::Base

config/mongodb_logger.yml

production:
  host: xxxxxxx.mongolab.com
  port: xxxxxx
  database: heroku_xxxxx
  username: <%= ENV['MONGODB_USERNAME'] %>
  password: <%= ENV['MONGODB_PASSWORD'] %>
  capsize: <%= 50.megabytes %>

config/initializers/mongo_log_init.rb

# MONGO DB LOGGING
require 'mongodb_logger/server' # required

# callback for errors

MongodbLogger::Base.configure do |config|
  config.on_log_exception do |mongo_record|
    STDOUT.puts "##################### Error begin ###########################"
    STDOUT.puts mongo_record.inspect
    STDOUT.puts "#####################  Error end  ###########################"
  end
end

# this secure you web interface by basic auth, but you can skip this, if you no need this
MongodbLogger::Server.use Rack::Auth::Basic do |username, password|
  [username, password] == ['xxxxxxx', 'xxxxxxx']
end

So logs write, but without addtional info? How you check, what no info?

With everything set up like this, I was expecting to see the user id and email in my logs, or a note that the request was not from a signed in user.

I have looked at the logs in mongodb_logger front-end (at /mongodb) as well as directly in the mongo database via the console, and the "additional_data" hash does not show up in either.

I suppose I am running the "add_metadata" before the "include". Is this the problem? I have the following:

class ApplicationController < ActionController::Base
  protect_from_forgery  
  before_filter :add_metadata_to_mongolog

  # MONGO LOGGING
  include MongodbLogger::Base

Should I put the "include" in the before filter so that it definitely gets run before "add_metadata"?

class ApplicationController < ActionController::Base
  protect_from_forgery  
  before_filter :add_metadata_to_mongolog

 ......

 private

 def add_metadata_to_mongolog

  include MongodbLogger::Base

    # make sure we're using the MongodbLogger in this environment
    if Rails.logger.respond_to?(:add_metadata)
      if current_user
        id = current_user.id
        email = current_user.email
      else
        id = nil
        email = nil
      end

      Rails.logger.add_metadata(:additional_data => {:user_id => id, :user_email => email, :logged_in => !email.nil?})
    end
  end

Or just put the "include" line above the before filter?

class ApplicationController < ActionController::Base
  protect_from_forgery

  include MongodbLogger::Base


  before_filter :add_metadata_to_mongolog

Yep, include should be before all filters - it is initialize mongo record.

Thanks! This got it working. I am having one other issue but I'll open a new thread.