Skip to content

Commit 7b1c1fc

Browse files
committed
Use JSON-format structured logging in production
1 parent 7fdca65 commit 7b1c1fc

3 files changed

Lines changed: 14 additions & 4 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/environments/production.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@
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+
end
5254

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

0 commit comments

Comments
 (0)