Skip to content

Adding data to the payload for all logs generated further down the stack #314

@hannesfostie

Description

@hannesfostie

Hi there!

We recently changed our logs to use semantic_logger and its Rails gem, and there is one usecase that we haven't been able to figure out.

We currently use Rails.logger.tagged() { } in order to add some data to all logs that get generated further down the stack. However, some of this data are things like user id's and the like - in other words, they probably shouldn't be tags in the first place.

I know that when you're in a controller context you can use append_info_to_payload but that is not always the case. I couldn't find any other way to do this.

Here's some pseudo-code to illustrate what I'm looking for:

class Worker
  def perform_for_user(user_id)
    Rails.logger.with_payload(user_id: user_id) do
      BusinessLogic.execute
    end
  end
end

class BusinessLogic
  def self.execute
    Rails.logger.info("doing my thing")
    # ...
  end
end

The use case is that we may want to execute BusinessLogic code in various places, but in some scenarios we want to add log data in another place. In the example above, I would expect the payload to contain the user_id when performed from the Worker, but other code paths should still be able to execute the BusinessLogic code which would then just log without that user_id in the payload.

Our current solution is to replace the with_payload line with the following: Rails.logger.tagged("User##{user_id}") do

This kind of works, but as mentioned before I don't believe adding a tag like User#1234 is an amazing solution considering our logs are structured as json and we'd expect this user_id field to be properly searchable (in our case through the ELK stack and Kibana).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions