talira

LogStruct

Verified

Zero-config, type-safe JSON logging for Ruby on Rails apps

open sourceMITself-hostablefree

LogStruct is a Ruby gem that makes a Rails app (Rails 7.1+, Ruby 3.3+) write one JSON line per log entry to STDOUT in production: requests, jobs, mail, errors, security events and the app's own logs. It replaces Rails' loggers with a SemanticLogger logger, turns on Lograge for request lines, and adds structured lines for Sidekiq, GoodJob, ActiveJob, ActionMailer, Puma, ActiveStorage, Shrine, CarrierWave and more. Values under sensitive keys are filtered, and emails (as a keyed hash), card numbers, phone numbers, SSNs and credentials are scrubbed from strings before a line is written. Every built-in line is a Sorbet-typed struct, and a separate Terraform provider checks the source and event names in CloudWatch metric filters at plan time.

Problems it solves

  • my Rails production logs are multi-line text that CloudWatch can't turn into metrics
  • Lograge only covers request lines, and Sidekiq jobs, mailers and errors are still plain text
  • passwords and API tokens from request params and job arguments are ending up in our logs
  • scrub email addresses and card numbers out of log messages but still follow one user through the logs
  • I need logging that satisfies SOC 2 requirements without writing it all myself
  • one JSON line per request with the request ID, duration, status and client IP
  • every log line during a request should carry the same request id so I can trace it
  • an unhandled exception in Rails prints a huge block of text instead of one searchable entry
  • huge params hashes and backtraces are bloating our log ingestion bill
  • build Datadog or Grafana dashboards from Rails logs without regex parsing
  • my CloudWatch metric filters silently stop matching when someone renames a log event
  • decide per kind of error whether it's raised in development and reported to Sentry in production
  • log to stdout only in containers so there's no log file filling up the disk

Features

  • JSON lines to STDOUT in production
  • switches on for recognized server and worker processes only
  • filtering of values under sensitive key names
  • scrubbing of emails (keyed hash), card numbers, phone numbers, SSNs, URL passwords and bearer credentials
  • size limits for hashes, arrays, backtraces and strings
  • per-error-type handling modes (raise, log, report)
  • Sorbet-typed log structs and custom typed log classes
  • request ID and tags on every line
  • Terraform provider that checks CloudWatch metric filters at plan time

Alternatives

The same job

These do what LogStruct does.

  1. Lograge Taming Rails' default request logging

Compare LogStruct with the top 3