diff --git a/.env.example b/.env.example index a327598ef3..d2f987a24b 100644 --- a/.env.example +++ b/.env.example @@ -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 diff --git a/app/services/gias_csv_importer.rb b/app/services/gias_csv_importer.rb index ac2a4fb226..8bed83820b 100644 --- a/app/services/gias_csv_importer.rb +++ b/app/services/gias_csv_importer.rb @@ -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 @@ -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 @@ -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 diff --git a/config/environments/development.rb b/config/environments/development.rb index fb54819e24..73a9c29fed 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -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 diff --git a/lib/tasks/gias_update.rake b/lib/tasks/gias_update.rake index a9ebcbc04f..ae892588c8 100644 --- a/lib/tasks/gias_update.rake +++ b/lib/tasks/gias_update.rake @@ -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 diff --git a/spec/services/gias_csv_importer_spec.rb b/spec/services/gias_csv_importer_spec.rb index 59894b0f18..76c0d172d7 100644 --- a/spec/services/gias_csv_importer_spec.rb +++ b/spec/services/gias_csv_importer_spec.rb @@ -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