Tagged logs in Rails/ActiveSupport

What's tags in logs?

It's from Rails' ActiveSupport to prepend automatically the logs with a parameter.

https://api.rubyonrails.org/classes/ActiveSupport/TaggedLogging.html

Rails.logger.tagged("background_job_number_1") do
 Rails.logger.error("Thing went wrong for user XXX")
end
#=> [background_job_number_1] Thing went wrong for user XXX

It's very useful in many cases, specially in a background system or a shared code that method:

def shared_method
  if true
    Rails.logger.info("User XXX: done")
  else
    Rails.logger.error("User XXX: failure")
  end
end

def user_update_flow
  Rails.logger.tagged("user_update_flow") do
    shared_method
    # Log: [user_update_flow] User XXX: done
  end
end

def user_create_flow
  Rails.logger.tagged("user_create_flow") do
     shared_method
    # Log: [user_create_flow] User XXX: done
  end
end

In that example, depending on the caller, the logs will be prepend with the expected tag or tags.

Feel free to shared and comment.