Skip to content

Commit be53ee0

Browse files
committed
feat: Add JSON structured logging with lograge
- Add lograge gem for structured logging - Configure JSON output format in production - Include request_id, user_id, ip, and timestamp in logs - Exclude health check endpoints from logs - Toggle via RAILS_LOG_JSON env var (enabled by default)
1 parent 1cb906a commit be53ee0

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ gem "jquery-rails"
2323

2424
gem "exception_notification"
2525

26+
# JSON structured logging
27+
gem "lograge"
28+
2629
gem "bcrypt", "~> 3.1.2"
2730
gem "rotp"
2831
gem "rqrcode"

config/environments/production.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@
4040
# Change to "debug" to log everything (including potentially personally-identifiable information!)
4141
config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info")
4242

43+
# Enable JSON structured logging with lograge (set RAILS_LOG_JSON=false to disable)
44+
config.lograge.enabled = ENV.fetch("RAILS_LOG_JSON", "true") == "true"
45+
config.lograge.formatter = Lograge::Formatters::Json.new
46+
config.lograge.ignore_actions = ["HealthController#live", "HealthController#ready"]
47+
config.lograge.custom_options = lambda do |event|
48+
{
49+
request_id: event.payload[:request_id],
50+
user_id: event.payload[:user_id],
51+
ip: event.payload[:ip],
52+
time: Time.current.iso8601
53+
}
54+
end
55+
config.lograge.custom_payload do |controller|
56+
{
57+
request_id: controller.request.request_id,
58+
user_id: controller.instance_variable_get(:@user)&.id,
59+
ip: controller.request.remote_ip
60+
}
61+
end
62+
4363
# Prevent health checks from clogging up the logs.
4464
config.silence_healthcheck_path = "/up"
4565

0 commit comments

Comments
 (0)