Skip to content

Commit 17253c2

Browse files
authored
Merge pull request #178 from berkeley-cdss/more-logging
Reconfigure logging for clarity
2 parents 320f2f6 + cd085aa commit 17253c2

File tree

5 files changed

+40
-25
lines changed

5 files changed

+40
-25
lines changed

.env.example

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY=
1010
ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT=
1111
ACTION_MAILER_DELIVERY_METHOD='sendmail' # or 'smtp'
1212

13-
# Set to enabled/true on Heroku
14-
RAILS_LOG_TO_STDOUT=enabled
15-
1613
APP_HOST=flextensions.eecs.cloud
1714
APP_PORT=80
1815
# Canvas Configuration

app/services/email_service.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# app/services/email_service.rb
2-
##########################################################################################
3-
# if you want to use Letter Opener Web make sure to set ENV[ENABLE_EMAIL_SENDING] to false
4-
##########################################################################################
1+
# frozen_string_literal: true
2+
3+
# A class for sending templated emails, using basic `{{variable}}` substitution.
54
class EmailService
65
class << self
76
# Given a subject_template and body_template (both strings
@@ -12,7 +11,7 @@ def render_templates(subject_template, body_template, mapping)
1211
mapping.each_with_object(
1312
{ subject: subject_template.dup, body: body_template.dup }
1413
) do |(key, val), memo|
15-
placeholder = "{{#{key}}}"
14+
placeholder = /{{\s*#{key}\s*}}/i
1615
memo[:subject].gsub!(placeholder, val.to_s)
1716
memo[:body].gsub!(placeholder, val.to_s)
1817
end

config/environments/development.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,10 @@
9595
config.active_record.encryption.primary_key = 'dev-primary-key-1234567890abcdef'
9696
config.active_record.encryption.deterministic_key = 'dev-deterministic-key-1234567890abcdef'
9797
config.active_record.encryption.key_derivation_salt = 'dev-salt-1234567890abcdef'
98+
99+
# Setup logging with Lograge [https://github.com/roidrage/lograge]
100+
# See config/initializers/lograge.rb for more details.
101+
# This should normally be false in development, but set it to true to
102+
# test log formatting during development.
103+
config.lograge.enabled = false
98104
end

config/environments/production.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,23 @@
5353
#config.force_ssl = true
5454

5555
# Setup logging with Lograge [https://github.com/roidrage/lograge]
56+
# See config/initializers/lograge.rb for more details.
5657
config.lograge.enabled = true
5758

58-
# config.logger = ActiveSupport::Logger.new(log_dest)
59+
# "info" includes generic and useful information about system operation, but avoids logging too much
60+
# information to avoid inadvertent exposure of personally identifiable information (PII). If you
61+
# want to log everything, set the level to "debug".
62+
config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info")
63+
64+
# Disable query logging
65+
# config.active_record.logger = nil
66+
# config.logger = ActiveSupport::Logger.new(STDOUT)
5967
# .tap { |logger| logger.formatter = ::Logger::Formatter.new }
6068
# .then { |logger| ActiveSupport::TaggedLogging.new(logger) }
6169

62-
6370
# # Prepend all log lines with the following tags.
6471
# config.log_tags = [ :request_id ]
6572

66-
# "info" includes generic and useful information about system operation, but avoids logging too much
67-
# information to avoid inadvertent exposure of personally identifiable information (PII). If you
68-
# want to log everything, set the level to "debug".
69-
config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info")
70-
7173
# Use a different cache store in production.
7274
# config.cache_store = :mem_cache_store
7375

config/initializers/lograge.rb

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
1-
if Rails.application.config.lograge.enabled
2-
3-
# Log to ENVIRONMENT.rb
4-
if ENV["RAILS_LOG_TO_STDOUT"] == "true"
5-
log_dest = STDOUT
6-
else
1+
Rails.application.configure do
2+
log_dest = STDOUT
3+
if ENV["RAILS_LOG_TO_FILE"].present?
4+
# Log to environment.rb
75
log_dest = Rails.root.join("log", "#{Rails.env}.log")
86
end
97

10-
Rails.application.configure do
8+
# Basic config moved from production.rb
9+
config.log_level = (ENV["LOG_LEVEL"] || "info").downcase.to_sym
10+
11+
if !Rails.application.config.lograge.enabled
1112
config.logger = ActiveSupport::Logger.new(log_dest)
13+
.tap { |logger| logger.formatter = ::Logger::Formatter.new }
14+
.then { |logger| ActiveSupport::TaggedLogging.new(logger) }
15+
16+
config.log_tags = [:request_id]
17+
else
18+
# This should be set before the logger is created
1219
config.lograge.formatter = Lograge::Formatters::Json.new
20+
config.logger = ActiveSupport::Logger.new(log_dest)
1321
config.colorize_logging = false
14-
config.lograge.ignore_actions = [ 'Rails::HealthController#show',
15-
'StatusController#health_check']
22+
config.lograge.ignore_actions = [
23+
'Rails::HealthController#show',
24+
'StatusController#health_check'
25+
]
1626

1727
config.lograge.custom_payload do |controller|
1828
{
1929
request_id: controller.request.uuid,
2030
user_id: controller.current_user.try(:id)
2131
}
2232
end
33+
2334
config.lograge.custom_options = lambda do |event|
2435
exceptions = %w(controller action format id)
2536
options = {
@@ -37,7 +48,7 @@
3748
options[:exception_backtrace] = event.payload[:exception_object].backtrace.join("\n")
3849
end
3950
options
40-
end
51+
end
4152

4253
ActionDispatch::DebugExceptions.register_interceptor do |request, exception|
4354
case exception

0 commit comments

Comments
 (0)