shadabahmed / logstasher

Awesome rails logs
MIT License
818 stars 136 forks source link

What's the difference between add_custom_fields and add_custom_fields_to_request_context #133

Open andruby opened 7 years ago

andruby commented 7 years ago

I'm struggling in figuring out the difference between LogStasher.add_custom_fields and LogStasher.add_custom_fields_to_request_context.

The example in the Readme doesn't help explain the difference or when to use which. It seems to duplicate the block.

if LogStasher.enabled?
  LogStasher.add_custom_fields do |fields|
    # This block is run in application_controller context,
    # so you have access to all controller methods
    fields[:user] = current_user && current_user.mail
    fields[:site] = request.path =~ /^\/api/ ? 'api' : 'user'

    # If you are using custom instrumentation, just add it to logstasher custom fields
    LogStasher.custom_fields << :myapi_runtime
  end

  LogStasher.add_custom_fields_to_request_context do |fields|
    # This block is run in application_controller context,
    # so you have access to all controller methods
    # You can log custom request fields using this block
    fields[:user] = current_user && current_user.mail
    fields[:site] = request.path =~ /^\/api/ ? 'api' : 'user'
  end
end

If I want to send some field to our logstash, do I use add_custom_fields or add_custom_fields_to_request_context?

inska commented 7 years ago

+1

Youngv commented 7 years ago
  def add_custom_fields(&block)
    wrapped_block = Proc.new do |fields|
      LogStasher::CustomFields.add(*LogStasher.store.keys)
      instance_exec(fields, &block)
    end
    ::ActionController::Metal.send(:define_method, :logstasher_add_custom_fields_to_payload, &wrapped_block)
    ::ActionController::Base.send(:define_method, :logstasher_add_custom_fields_to_payload, &wrapped_block)
  end

  def add_custom_fields_to_request_context(&block)
    wrapped_block = Proc.new do |fields|
      instance_exec(fields, &block)
      LogStasher::CustomFields.add(*fields.keys)
    end
    ::ActionController::Metal.send(:define_method, :logstasher_add_custom_fields_to_request_context, &wrapped_block)
    ::ActionController::Base.send(:define_method, :logstasher_add_custom_fields_to_request_context, &wrapped_block)
  end

someone can explain?

jondoe1337 commented 6 years ago

The only difference I see is that the add_custom_fields uses the request-store cache.

Any update on this?