Introduction
LogStruct adds JSON structured logging to Rails apps. Add the gem to your Gemfile, and in production your app writes one JSON line to STDOUT for each request, job, email, error and log call. An initializer is only needed to change the defaults.
Features
- On in production for the server and job processes it recognizes (Puma, Unicorn, Thin, Falcon,
rails server, Sidekiq andgood_job start), and in test for every process except the console. Off in development, consoles and production Rake tasks unless you setLOGSTRUCT_ENABLED=true. See When LogStruct runs. - Sets up Lograge for request lines, and uses Semantic Logger to write every line.
- Integrations for ActiveJob, ActionMailer, Sidekiq, GoodJob, Puma, ActiveStorage, Shrine, CarrierWave, Ahoy, ActiveModelSerializers, Dotenv and SQL queries (opt-in).
- Filtering of sensitive keys and scrubbing of emails, card numbers and other sensitive values in strings.
- Security lines for blocked hosts, CSRF errors and IP spoofing errors, with Rails' own responses left as they are.
- Error handling modes that decide whether each kind of error is raised, logged or reported, with reports going to Sentry, Bugsnag, Rollbar, Honeybadger or your own reporter.
- Typed log lines with Sorbet, for the built-in lines and your own.
Logging with LogStruct
# Log a message
Rails.logger.info "User signed in"
# Log a Hash: its keys become the line's fields, and evt names the event
Rails.logger.info({evt: "user_login", user_id: user.id, plan: "pro"})
# Log a message with fields
Rails.logger.info({msg: "Charged the card", amount_cents: 1999, currency: "usd"})These lines are written:
{"src":"app","evt":"log","lvl":"info","ts":"2026-10-17T03:14:21.865+00:00","msg":"User signed in","prog":"Rails"}
{"evt":"user_login","user_id":123,"plan":"pro","src":"app","ts":"2026-10-17T03:14:21.865+00:00","lvl":"info","prog":"Rails"}
{"msg":"Charged the card","amount_cents":1999,"currency":"usd","src":"app","evt":"log","ts":"2026-10-17T03:14:21.865+00:00","lvl":"info","prog":"Rails"}Most apps only need this. No knowledge of Sorbet is required.