Skip to content

Commit

Permalink
BL-864: Allow logging for Primo::Search class via config.
Browse files Browse the repository at this point in the history
Also allows debugging of Primo requests via configuration.
  • Loading branch information
dkinzer committed Nov 15, 2023
1 parent 3f8c743 commit eb3ee04
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 16 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ Primo.configure do |config|
# By default we validate parameters.
config.validate_parameters = false
# We do not enable primo request logs by default
config.enable_log_requests = false
# By default we use the built in Ruby logger, but you can set it to another logger
config.logger = Logger.new("log/primo_requests.log")
# Log level is set to :info by default
config.log_level = :info
# Log format is set to :logstash by deafult
config.log_format = :logstash
# debugging output is disabled by default
config.enable_debug_output = false
# The debug_output_stream is set to $stderr by default
config.debug_output_stream = $stderr
end
```
Now you can access those configuration attributes with `Primo.configuration.apikey`
Expand Down Expand Up @@ -202,10 +220,28 @@ Primo::Search::Query::build([q1, q2])
This API wrapper validates the `query` object according the specifications documented in [the Ex Libris Api docs](https://developers.exlibrisgroup.com/primo/apis/webservices/xservices/search/briefsearch) for the `query` field.
### Logging
#### Using :enable_loggable
This gem exposes a loggable interface to responses. Thus a response will respond to `loggable` and return a hash with state values that may be of use to log.
As a bonus, when we enable this feature using the `enable_loggable` configuration, error messages will contain the loggable values and be formatted as JSON.
#### Using logging configuration.
You can configure logging via the following configurations:
* `enable_log_requests`: (`true/false`)
* `log_level`
* `log_format`
* `logger`
The logger can be any logger including the Rails.logger. This logging feature is provided through [HTTParty](https://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/ClassMethods#logger-instance_method).
### Debugging requests
You can configure debugging requests by setting the `enable_debug_output` configuration which is false by default. This feature is also provided through [HTTParty](https://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/ClassMethods#debug_output-instance_method).
You can configure the `debug_output_stream` which is set to `$stderr` by default.
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
34 changes: 33 additions & 1 deletion lib/primo/config.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "logger"

module Primo
class << self
attr_accessor :configuration
Expand All @@ -8,12 +10,35 @@ class << self
def self.configure()
self.configuration ||= Configuration.new
yield(configuration) if block_given?
on_configure
end

def self.on_configure
_configure_logging
_configure_debugging
end


def self._configure_logging
if configuration.enable_log_requests
primo_logger = configuration.logger
log_level = Primo.configuration.log_level
log_format = Primo.configuration.log_format
Primo::Search.logger primo_logger, log_level, log_format
end
end

def self._configure_debugging
if configuration.enable_debug_output
Primo::Search.debug_output configuration.debug_output_stream
end
end

class Configuration
attr_accessor :apikey, :region, :operator, :field, :precision
attr_accessor :context, :environment, :inst, :vid, :scope, :pcavailability, :enable_loggable
attr_accessor :timeout, :validate_parameters
attr_accessor :timeout, :validate_parameters, :enable_log_requests, :enable_debug_output
attr_accessor :logger, :log_level, :log_format, :debug_output_stream

def initialize
@apikey = "TEST_API_KEY"
Expand All @@ -28,6 +53,13 @@ def initialize
@enable_loggable = false
@timeout = 5
@validate_parameters = true
@enable_log_requests = false
# debug_output should only be enabled in development mode.
@enable_debug_output = false
@logger = Logger.new("log/primo_requests.log")
@log_level = :info
@log_format = :logstash
@debug_output_stream = $stderr
end

def timeout(params = {})
Expand Down
Empty file added log/.gitignore
Empty file.
53 changes: 38 additions & 15 deletions spec/lib/primo/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
require "spec_helper"

describe "Configuring Primo" do
before do
Primo.configure
end

context "when no attributes are set in the passed block" do
after do
Primo.configuration = nil
end

before(:all) do
Primo.configure {}
end
context "when no attributes are set in the passed block" do

it "uses default values" do
expect(Primo.configuration.apikey).to eql "TEST_API_KEY"
Expand All @@ -24,25 +27,22 @@
expect(Primo.configuration.pcavailability).to eql false
expect(Primo.configuration.timeout).to eql 5
expect(Primo.configuration.validate_parameters).to eql true
end

after(:all) do
Primo.configuration = nil
expect(Primo.configuration.enable_log_requests).to eql false
expect(Primo.configuration.logger).to be_instance_of(Logger)
expect(Primo.configuration.log_level).to eq(:info)
expect(Primo.configuration.enable_debug_output).to eql false
expect(Primo.configuration.debug_output_stream).to eq($stderr)
end
end

context "params set limit of 50" do
before(:all) do
Primo.configure {}
end

it "doubles the default timeout" do
expect(Primo.configuration.timeout("limit" => 50)).to eql 10
end
end

context "when attributes are set in the passed block" do
before(:all) do
before do
Primo.configure do |config|
config.apikey = "SOME_OTHER_API_KEY"
end
Expand All @@ -65,9 +65,32 @@
expect(Primo.configuration.vid).to eql :default_vid
expect(Primo.configuration.scope).to eql :default_scope
end
end

context "when we enable logging via configuration" do
before do
Primo.configure do |config|
config.enable_log_requests = true
end
end

after(:all) do
Primo.configuration = nil
it "should eanble logging in Primo::Search class" do
expect(Primo::Search.default_options[:logger]).to eq(Primo.configuration.logger)
expect(Primo::Search.default_options[:log_level]).to eq(Primo.configuration.log_level)
expect(Primo::Search.default_options[:log_format]).to eq(Primo.configuration.log_format)
end
end

context "when we enable debugging via configuration" do
before do
Primo.configure do |config|
config.enable_debug_output = true
end
end

it "should set debugging options for Primo::Search class" do
expect(Primo::Search.default_options[:debug_output]).to eq(Primo.configuration.debug_output_stream)
end
end

end

0 comments on commit eb3ee04

Please sign in to comment.