diff --git a/lib/fastapi.rb b/lib/fastapi.rb index e26727a..e5b1d26 100644 --- a/lib/fastapi.rb +++ b/lib/fastapi.rb @@ -9,6 +9,11 @@ require 'fastapi/utilities' module FastAPI + + def self.logger + @@logger ||= ActiveSupport::Logger.new(STDERR) + end + Oj.default_options = { mode: :compat } class Wrapper @@ -188,9 +193,12 @@ def fastapi_query(filters = {}, safe = false) parsed_filters = parse_filters(filters, safe) prepared_data = FastAPI::SQL.new(parsed_filters, offset, count, @model, @whitelist_fields) rescue StandardError => exception + FastAPI.logger.error("FastAPI -- Error Creating SQL:\n\t#{exception.message}") return error(offset, exception.message) end + FastAPI.logger.debug("FastAPI -- SQL:\n\t#{prepared_data[:query]}") + model_lookup = prepared_data[:models].each_with_object({}) do |(key, model), lookup| columns = model.columns_hash lookup[key] = { @@ -204,6 +212,7 @@ def fastapi_query(filters = {}, safe = false) count_result = ActiveRecord::Base.connection.execute(prepared_data[:count_query]) result = ActiveRecord::Base.connection.execute(prepared_data[:query]) rescue StandardError + FastAPI.logger.error("FastAPI -- Error Executing Query:\n\t#{prepared_data[:query]}") return error(offset, 'Query failed') end diff --git a/spec/bucket_spec.rb b/spec/bucket_spec.rb index a4a3287..22caaf8 100644 --- a/spec/bucket_spec.rb +++ b/spec/bucket_spec.rb @@ -31,6 +31,14 @@ let!(:bucket) { create(:blue_paper_bucket) } let(:response) { ModelHelper.response(Bucket, { id: 1 }, safe: true) } + before(:all) do + FastAPI.logger.level = ActiveSupport::Logger::FATAL + end + + after(:all) do + FastAPI.logger.level = ActiveSupport::Logger::INFO + end + it_behaves_like 'fastapi_meta' do let(:expected) { { total: 0, count: 0, offset: 0, error: /Filter "id" not supported/ } } end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6b3bcf5..e377070 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -25,6 +25,8 @@ config.before(:suite) do DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with(:truncation) + + FastAPI.logger.level = ActiveSupport::Logger::INFO end config.around(:each) do |example|