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
  • Loading branch information
CatalinVoineag committed Jan 2, 2024
1 parent da68e26 commit 7439728
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 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
19 changes: 15 additions & 4 deletions app/services/gias_csv_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ 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, :active_record_logger

def initialize(csv_path)
@csv_path = csv_path
@logger = Logger.new($stdout)
@active_record_logger = ActiveRecord::Base.logger
end

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

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

Rails.logger.info "Done!"
end

private
Expand All @@ -53,4 +56,12 @@ def school_excluded?(school)
school["EstablishmentStatus (code)"] != OPEN_SCHOOL ||
NON_ENGLISH_ESTABLISHMENTS.include?(school["TypeOfEstablishment (code)"])
end

def turn_off_ar_logger
ActiveRecord::Base.logger = nil
end

def turn_on_ar_logger
ActiveRecord::Base.logger = active_record_logger
end
end
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 7439728

Please sign in to comment.