Skip to content

Commit e2a1252

Browse files
authored
Use JSON-format structured logging in production (#882)
## Status - Related to RaspberryPiFoundation/digital-editor-issues#1455 ## What's changed? - Adds the Semantic Logging gem and uses it to make log messages more useful. See a comparison [here](https://logger.rocketjob.io/rails.html#configuration). - This includes controller and action names by default. - In the production environment only, logs are structured in JSON-format to make filtering easier in BetterStack. - Example: [filtering down to GET requests on the FeaturesController](https://telemetry.betterstack.com/team/t62404/tail?s=l314881&q=message.payload.controller%3D%22Api%3A%3AFeaturesController%22%20message.payload.method%3D%22GET%22&a=1781695637116522.107740926). - _The change in `application.rb` looks like it also enables JSON logging in non-prod environments, however this only applies to the log files written to disk, not the logging output in standard out._ - Disables Flipper logging, which was at DEBUG level with no clear way of changing the level. ## Steps to perform after deploying to production None.
1 parent 7fdca65 commit e2a1252

5 files changed

Lines changed: 19 additions & 5 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ gem 'puma', '~> 8.0'
4343
gem 'rack_content_type_default', '~> 1.1'
4444
gem 'rack-cors'
4545
gem 'rails', '~> 8.1.3'
46+
gem 'rails_semantic_logger', '~> 4.20'
4647
gem 'ruby-progressbar', '~> 1.13', require: false
4748
gem 'ruby-vips'
4849
gem 'sentry-rails'

Gemfile.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ GEM
403403
rails-html-sanitizer (1.7.0)
404404
loofah (~> 2.25)
405405
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
406+
rails_semantic_logger (4.20.0)
407+
rack
408+
railties (>= 5.1)
409+
semantic_logger (~> 4.16)
406410
railties (8.1.3)
407411
actionpack (= 8.1.3)
408412
activesupport (= 8.1.3)
@@ -513,6 +517,8 @@ GEM
513517
childprocess (>= 0.5, < 5.0)
514518
rexml (~> 3.2, >= 3.2.5)
515519
rubyzip (>= 1.2.2)
520+
semantic_logger (4.18.0)
521+
concurrent-ruby (~> 1.0)
516522
sentry-rails (6.6.0)
517523
railties (>= 5.2.0)
518524
sentry-ruby (~> 6.6.0)
@@ -618,6 +624,7 @@ DEPENDENCIES
618624
rack_content_type_default (~> 1.1)
619625
rails (~> 8.1.3)
620626
rails-erd
627+
rails_semantic_logger (~> 4.20)
621628
rspec
622629
rspec-rails
623630
rspec_junit_formatter

config/application.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,8 @@ class Application < Rails::Application
7373

7474
config.x.cloudflare_turnstile.secret_key = ENV.fetch('CLOUDFLARE_TURNSTILE_SECRET_KEY', nil)
7575
config.x.cloudflare_turnstile.enabled = ENV['CLOUDFLARE_TURNSTILE_SECRET_KEY'].present?
76+
77+
config.rails_semantic_logger.format = :json
78+
config.semantic_logger.application = 'editor-api'
7679
end
7780
end

config/environments/production.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@
4545
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
4646
# config.force_ssl = true
4747

48-
# Log to STDOUT by default
49-
config.logger = ActiveSupport::Logger.new($stdout)
50-
.tap { |logger| logger.formatter = Logger::Formatter.new }
51-
.then { |logger| ActiveSupport::TaggedLogging.new(logger) }
48+
# Log to STDOUT on Heroku in JSON format, where this variable is set automatically.
49+
if ENV['RAILS_LOG_TO_STDOUT'].present?
50+
$stdout.sync = true
51+
config.rails_semantic_logger.add_file_appender = false
52+
config.semantic_logger.add_appender(io: $stdout, formatter: :json)
53+
config.semantic_logger.application = "editor-api@#{ENV['HEROKU_SLUG_COMMIT'] || 'unknown'}"
54+
end
5255

5356
# Prepend all log lines with the following tags.
5457
config.log_tags = [:request_id]

config/initializers/flipper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# config.flipper.strict = Rails.env.development? && :warn
1919

2020
## Show Flipper checks in logs
21-
# config.flipper.log = true
21+
config.flipper.log = false
2222

2323
## Reconfigure Flipper to use the Memory adapter and disable Cloud in tests
2424
# config.flipper.test_help = true

0 commit comments

Comments
 (0)