Skip to content

Commit

Permalink
Use Rails.logger instead of Logger
Browse files Browse the repository at this point in the history
Previously with Logger, there was noise added to the terminal when running
the specs. Because of this we want to use Rails.logger instead, in production
and development

Temporarily silent the Active Record logger when
upserting all the GiasSchools. This only adds noise
to the logs when running the gias_update task in production.
  • Loading branch information
CatalinVoineag committed Jan 3, 2024
1 parent ecea566 commit 1db3ad8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ PLACEMENTS_HOST=placements.localhost
CLAIMS_HOST=claims.localhost
GIAS_CSV_BASE_URL=https://ea-edubase-api-prod.azurewebsites.net/edubase/downloads/public
PUBLISH_BASE_URL=https://www.publish-teacher-training-courses.service.gov.uk
RAILS_LOG_TO_STDOUT=true
12 changes: 7 additions & 5 deletions app/services/gias_csv_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ class GiasCsvImporter
OPEN_SCHOOL = "1".freeze
NON_ENGLISH_ESTABLISHMENTS = %w[8 10 25 24 26 27 29 30 32 37 49 56 57].freeze

attr_reader :csv_path, :logger
attr_reader :csv_path

def initialize(csv_path)
@csv_path = csv_path
@logger = Logger.new($stdout)
end

def call
Expand Down Expand Up @@ -37,10 +36,13 @@ def call
end

if invalid_records.any?
logger.info "Invalid rows - #{invalid_records.inspect}"
Rails.logger.info "Invalid rows - #{invalid_records.inspect}"
end
GiasSchool.upsert_all(records, unique_by: :urn)
logger.info "Done!"
Rails.logger.silence do
GiasSchool.upsert_all(records, unique_by: :urn)
end

Rails.logger.info "Done!"
end

private
Expand Down
6 changes: 6 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@

# Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true
#
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new($stdout)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
end
7 changes: 3 additions & 4 deletions lib/tasks/gias_update.rake
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
desc "Upsert GIAS data into GiasSchool"
task gias_update: :environment do
logger = Logger.new($stdout)
today = Time.zone.today.strftime("%Y%m%d")
gias_filename = "edubasealldata#{today}.csv"

logger.info "Downloading the new gias file for #{Time.zone.today}"
Rails.logger.info "Downloading the new gias file for #{Time.zone.today}"
tempfile = Down.download("#{ENV["GIAS_CSV_BASE_URL"]}/#{gias_filename}")
logger.info "Done!"
Rails.logger.info "Done!"

logger.info "Importing data"
Rails.logger.info "Importing data"
GiasCsvImporter.call(tempfile.path)

tempfile.unlink
Expand Down
9 changes: 4 additions & 5 deletions spec/services/gias_csv_importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
end

it "logs messages to STDOUT" do
expect { subject }.to output(
match(/Done!/).and(match(/Invalid rows - /)).and(
match(/Row 5 is invalid/),
),
).to_stdout
expect(Rails.logger).to receive(:info).with("Invalid rows - [\"Row 5 is invalid\"]")
expect(Rails.logger).to receive(:info).with("Done!")

subject
end
end

0 comments on commit 1db3ad8

Please sign in to comment.