Getting Started with LogStruct

Follow these steps to add LogStruct to your Rails application and start enjoying the benefits of structured JSON logging.

Installation

Add this line to your application's Gemfile:

gem 'logstruct'

And then execute:

bundle install

LogStruct is now installed and will automatically enable JSON structured logging in the test and production environments.

Basic Configuration

While LogStruct works out of the box with zero configuration, you might want to customize it to better suit your application. Create a new file at config/initializers/logstruct.rb with the following content:

config/initializers/logstruct.rb
LogStruct.configure do |config|
  # your configuration here
end

Basic Usage

You can use LogStruct with the standard Rails logger:

# Log a simple message
Rails.logger.info "User signed in"

# Log structured data
Rails.logger.info({
  src: "rails",
  evt: "user_login",
  user_id: user.id,
  ip_address: request.remote_ip
})

# Log with tags
Rails.logger.tagged("Authentication") do
  Rails.logger.info "User signed in"
  Rails.logger.info(user_id: user.id, ip_address: request.remote_ip)
end

LogStruct will automatically convert all logs to JSON format with a consistent structure, making them easy to parse and search in log management systems.

Next Steps

Explore these pages to learn more about LogStruct:

  • Configuration - Learn how to customize LogStruct for your application
  • Integrations - Explore built-in integrations with popular gems
  • Type Safety - Advanced logging with Sorbet type checking