Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Include Rails log_tags in metadata

Đang mở
#37 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
45/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
rails, ruby
Lĩnh vực
observability-sre

Hướng nghiên cứu

Bắt đầu bằng việc xác định Logdna::Ruby#log và cấu hình logger được sử dụng cùng với ActiveSupport::TaggedLogging. Xác minh cách các tags hiện tại cần được thêm vào metadata của log trong khi vẫn giữ nguyên metadata hiện có, sau đó xác nhận rằng các Rails logs có tag bao gồm request_id trong metadata kết quả.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

I've made this change locally. But I think it could go in the public gem. I'm using ActiveSupport::TaggedLogging in Rails to include log tags like this:

# This is the default in Rails, but could include more stuff if desired
config.log_tags = [:request_id]

# Send logs to LogDNA
log_dna_logger = Logdna::Ruby.new(
  Rails.application.secrets.log_dna_api_key, {
    env: "development",
    app: "ShayHi",
    level: Logger::DEBUG
  }
)
# Wrap LogDNA logger in TaggedLogging to add log tags
config.logger = ActiveSupport::TaggedLogging.new(log_dna_logger)

I then modified Logdna::Ruby#log to include the tags in the metadata

def log(message = nil, opts = {})
  # MY NEW CODE BELOW vvv
  if formatter.current_tags.present?
    opts[:meta] = (opts[:meta] || {}).merge(tags: formatter.current_tags.join(" "))
  end
  # MY NEW CODE ABOVE ^^^
  if message.nil? && block_given?
    message = yield
  end
  if message.nil?
    @internal_logger.debug("provide either a message or block")
    return
  end
  message = message.to_s.encode("UTF-8")
  @client.write_to_buffer(message, default_opts.merge(opts).merge(
                                     timestamp: (Time.now.to_f * 1000).to_i
                                   ))
end

Logs now look like this:

tags

This is particularly useful for including the Rails request_id in the log metadata. I can open a PR if this is something you're interested in adding to the gem for everyone.

Ngôn ngữ chính
Ruby
Star
19
Fork
18
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của logdna/ruby

Tất cả issue của logdna/ruby

Issue tương tự

Thêm issue về Ruby

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.