OOZOU
與我們聯繫
Back to TIL
ruby on rails

Tag rails logs with useful information

October 13, 2020

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 railsFebruary 10, 2022

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 railsJanuary 31, 2022

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 railsJanuary 19, 2022

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

服務

  • Web 開發
  • AI 與生成式 AI 解決方案
  • 行動應用程式開發
  • 資料分析與工程
  • UI/UX 設計與產品設計
  • 數位轉型

公司

  • 關於我們
  • 職涯
  • 部落格
  • 白皮書
  • 案例研究
  • Today I Learned
  • 聯繫我們

更多

  • 產業
  • 索取報價
  • 合作夥伴網絡
© 2026 OOZOU. 版權所有。
隱私政策行為準則反賄賂反貪腐政策