Skip to content

Commit

Permalink
Configure semantic logger
Browse files Browse the repository at this point in the history
Configures the app to format it's logs with `rails_semantic_logger` so
logit can ingest them.
https://technical-guidance.education.gov.uk/infrastructure/monitoring/logit/#ruby-on-rails
  • Loading branch information
rjlynch committed May 31, 2024
1 parent 41651fa commit abab0b2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ gem "uk_postcode"
gem "phonelib"
###############

gem "amazing_print"
gem "audited", "~> 5.4"
gem "concurrent-ruby", require: "concurrent"
gem "concurrent-ruby-ext"
Expand All @@ -41,6 +42,7 @@ gem "flipper-ui"
gem "httparty", "~> 0.21"
gem "invisible_captcha"
gem "omniauth-azure-activedirectory-v2"
gem "rails_semantic_logger"
gem "rolify"
gem "sentry-rails", "~> 5.17"

Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.4)
public_suffix (>= 2.0.2, < 6.0)
amazing_print (1.6.0)
annotate (3.2.0)
activerecord (>= 3.2, < 8.0)
rake (>= 10.4, < 14.0)
Expand Down Expand Up @@ -353,6 +354,10 @@ GEM
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
rails_semantic_logger (4.14.0)
rack
railties (>= 5.1)
semantic_logger (~> 4.13)
railties (7.1.2)
actionpack (= 7.1.2)
activesupport (= 7.1.2)
Expand Down Expand Up @@ -445,6 +450,8 @@ GEM
scenic (1.7.0)
activerecord (>= 4.0.0)
railties (>= 4.0.0)
semantic_logger (4.15.0)
concurrent-ruby (~> 1.0)
sentry-rails (5.17.1)
railties (>= 5.0)
sentry-ruby (~> 5.17.1)
Expand Down Expand Up @@ -502,6 +509,7 @@ PLATFORMS
ruby

DEPENDENCIES
amazing_print
annotate
audited (~> 5.4)
binding_of_caller
Expand Down Expand Up @@ -540,6 +548,7 @@ DEPENDENCIES
pry-byebug
puma (>= 6.4.2, < 7)
rails (~> 7.1)
rails_semantic_logger
rolify
rspec-rails
rubocop-govuk
Expand Down
7 changes: 7 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,12 @@
# Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true

# Or :info
config.log_level = :debug
# Console colorised non-json output
config.log_format = :color
# Show file and line number (expensive: not for production)
config.semantic_logger.backtrace_level = :debug

config.hosts << "itrp.local"
end
27 changes: 10 additions & 17 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@
config.force_ssl = true
config.ssl_options = { redirect: { exclude: ->(request) { request.path.include?("healthcheck") } } }

# Include generic and useful information about system operation, but avoid logging too much
# information to avoid inadvertent exposure of personally identifiable information (PII).
config.log_level = :info

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

# Use a different cache store in production.
# config.cache_store = :mem_cache_store

Expand All @@ -61,21 +54,21 @@
# the I18n.default_locale when a translation cannot be found).
config.i18n.fallbacks = true

# Include generic and useful information about system operation, but avoid logging too much
# information to avoid inadvertent exposure of personally identifiable information (PII).
config.log_level = :info

# Don't log any deprecations.
config.active_support.report_deprecations = false

# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = Logger::Formatter.new
# Set format for semantic logger, see config/initializers/semantic_logger.rb
config.log_format = :json

# Use a different logger for distributed setups.
# require "syslog/logger"
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name")
# Don't log to file
config.rails_semantic_logger.add_file_appender = false

if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new($stdout)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
# Don't log SQL
config.active_record.logger = nil

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
Expand Down
7 changes: 7 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,12 @@
# config/initializers/dartsass.rb
config.dartsass.build_options << " --quiet-deps"

# Or :info
config.log_level = :debug
# Console colorised non-json output
config.log_format = :color
# Show file and line number (expensive: not for production)
config.semantic_logger.backtrace_level = :debug

config.hosts << "www.example.com"
end
11 changes: 11 additions & 0 deletions config/initializers/semantic_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Rails.application.configure do
config.semantic_logger.application = "" # This is added by logstash from its tags
config.log_tags = [:request_id] # Prepend all log lines with the following tags
end

SemanticLogger.add_appender(
io: $stdout,
level: Rails.application.config.log_level,
formatter: Rails.application.config.log_format,
)
Rails.application.config.logger.info("Application logging to STDOUT")

0 comments on commit abab0b2

Please sign in to comment.