OOZOU
ติดต่อเรา
Back to TIL
ruby on rails

Tag rails logs with useful information

13 ตุลาคม 2563

Just learned about tagged logging in rails. Did you know you can tag a log by using

logger.tagged('Your Tag') { logger.info('Message') }

config/application.rb

You can also do it globally with

# config/application.rb
config.log_tags = [:method_on_request_object, lambda { |request| request.method.modifier_method }]

config/application.rb

Add client Device OS and OS Version information to logs (requires clients to send X-OS and X-OS-Version headers)

# config/application.rb
config.log_tags = [ :request_id, lambda { |request| "#{request.headers['HTTP_X_OS']} #{request.headers['HTTP_X_OS_VERSION']}" } ]

For production environments, specific settings can be added in the config environments production.rb file to optimize logging and performance.

1. Introduction to Log Tags

Log tags are a powerful feature in Rails that allows you to add custom information to your logs, making it easier to debug and understand your application’s behavior. By using log tags, you can categorize related log messages and make your logs more readable and useful. In this section, we will introduce the concept of log tags and explain how to use them in your Rails application.

2. Configuring Rails Components

Configuring Rails components is an essential part of setting up your application. In this section, we will discuss how to configure various Rails components, including the application, environment, and middleware. We will also cover how to use the config method to set up custom configurations for your application.

3. Customizing Log Tags

Customizing log tags allows you to add specific information to your logs that is relevant to your application. In this section, we will discuss how to customize log tags using the log_tags method. We will also cover how to use Procs and objects that respond to to_s to add dynamic information to your logs.

4. Log Tagging Examples

Log tagging is a versatile feature that can be used in various ways to improve your logs. In this section, we will provide examples of how to use log tags in different scenarios, such as logging requests to external APIs, background jobs, and user information. We will also cover how to use log tags to debug multi-tenant applications.

5. Best Practices for Log Tags

Using log tags effectively requires some best practices to ensure that your logs are readable and useful. In this section, we will discuss best practices for using log tags, including how to keep your logs concise, how to use meaningful tag names, and how to avoid over-tagging. We will also cover how to use log tags in conjunction with other logging tools and techniques to get the most out of your logs.

More on ruby on rails

ruby on rails10 กุมภาพันธ์ 2565

Use Pundit Policies to manage permitted parameters, defaults and per action

To manage permitted params you can add permitted\attributes and permitted\attributes\for\#{action} in your policies e.g.

ruby on rails31 มกราคม 2565

Use `requestSubmit` for form submits with Turbo

Turbo listens to submit events on form.  Hence, always use requestSubmit to submit  a form using JS.

ruby on rails19 มกราคม 2565

Ignore bundled gems for Rubocop and ESLint in GitHub Actions

Rails projects will often install gems into the vendor directory in GitHub actions so we can cache them for the consecutive runs.

มีโปรเจกต์ในใจ?

เรายินดีรับฟังสิ่งที่คุณกำลังสร้าง

เริ่มต้นสนทนาhello@oozou.com
OOZOU Logo

กรุงเทพฯ · สิงคโปร์ · ฮ่องกง

X
Facebook
Instagram
LinkedIn

บริการ

  • พัฒนาเว็บ
  • AI และ Generative AI
  • พัฒนาแอปมือถือ
  • การวิเคราะห์และวิศวกรรมข้อมูล
  • ออกแบบ UI/UX และผลิตภัณฑ์
  • การเปลี่ยนแปลงทางดิจิทัล

บริษัท

  • เกี่ยวกับเรา
  • ร่วมงานกับเรา
  • บล็อก
  • เอกสารวิชาการ
  • กรณีศึกษา
  • Today I Learned
  • ติดต่อเรา

เพิ่มเติม

  • อุตสาหกรรม
  • ขอใบเสนอราคา
  • เครือข่ายพันธมิตร
© 2026 OOZOU สงวนลิขสิทธิ์
นโยบายความเป็นส่วนตัวจรรยาบรรณนโยบาย ABAC