Skip to content

Commit

Permalink
feat(semantic-logging): allow ruby standard logger to be configured t…
Browse files Browse the repository at this point in the history
…o ensure backwards compatibility
  • Loading branch information
bethesque committed Oct 4, 2018
1 parent 01f1a8d commit cfbb704
Show file tree
Hide file tree
Showing 27 changed files with 93 additions and 37 deletions.
2 changes: 1 addition & 1 deletion lib/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require 'pact_broker/project_root'

module DB
include SemanticLogger::Loggable
include PactBroker::Logging
##
# Sequel by default does not test connections in its connection pool before
# handing them to a client. To enable connection testing you need to load the
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/renderers/html_pact_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HtmlPactRenderer

class NotAPactError < StandardError; end

include SemanticLogger::Loggable
include PactBroker::Logging

def self.call pact, options = {}
new(pact, options).call
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/base_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class BaseResource < Webmachine::Resource
include PactBroker::Services
include PactBroker::Api::PactBrokerUrls
include PactBroker::Api::Resources::Authentication
include SemanticLogger::Loggable
include PactBroker::Logging


attr_accessor :user
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Api
module Resources
class ErrorHandler

include SemanticLogger::Loggable
include PactBroker::Logging
include PactBroker::Logging

def self.call e, request, response
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
module PactBroker

class App
include SemanticLogger::Loggable
include PactBroker::Logging

attr_accessor :configuration

Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/badges/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Badges
module Service

extend self
include SemanticLogger::Loggable
include PactBroker::Logging

SPACE_DASH_UNDERSCORE = /[\s_\-]/
CACHE = {}
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/certificates/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Service

extend self
extend PactBroker::Logging
include SemanticLogger::Loggable
include PactBroker::Logging

def cert_store
cert_store = OpenSSL::X509::Store.new
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/config/load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module PactBroker
module Config
class Load

include SemanticLogger::Loggable
include PactBroker::Logging

def self.call configuration
new(configuration).call
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/config/save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module PactBroker
module Config
class Save

include SemanticLogger::Loggable
include PactBroker::Logging

def self.call configuration, setting_names
new(configuration, setting_names).call
Expand Down
16 changes: 10 additions & 6 deletions lib/pact_broker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def self.reset_configuration
end

class Configuration
include SemanticLogger::Loggable

SAVABLE_SETTING_NAMES = [
:order_versions_by_date,
Expand Down Expand Up @@ -44,14 +43,15 @@ class Configuration
attr_accessor :disable_ssl_verification
attr_accessor :base_equality_only_on_content_that_affects_verification_results
attr_reader :api_error_reporters
attr_writer :logger
attr_reader :custom_logger

def initialize
@before_resource_hook = ->(resource){}
@after_resource_hook = ->(resource){}
@authenticate_with_basic_auth = nil
@authorize = nil
@api_error_reporters = []
@semantic_logger = SemanticLogger[Configuration]
end

def self.default_configuration
Expand Down Expand Up @@ -83,6 +83,14 @@ def self.default_configuration
config
end

def logger
custom_logger || @semantic_logger
end

def logger= logger
@custom_logger = logger
end

def self.default_html_pact_render
lambda { |pact, options|
require 'pact_broker/api/renderers/html_pact_renderer'
Expand Down Expand Up @@ -199,9 +207,5 @@ def parse_space_delimited_string_list_property property_name, property_value
raise ConfigurationError.new("Pact Broker configuration property `#{property_name}` must be a space delimited String or an Array")
end
end

def log_path
log_dir + "/pact_broker.log"
end
end
end
2 changes: 1 addition & 1 deletion lib/pact_broker/domain/order_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module PactBroker
module Domain
class OrderVersions

include SemanticLogger::Loggable
include PactBroker::Logging

def self.call new_version
new_version.lock!
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/domain/webhook_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def redact? name

class WebhookRequest

include SemanticLogger::Loggable
include PactBroker::Logging
include PactBroker::Messages
HEADERS_TO_REDACT = [/authorization/i, /token/i]

Expand Down
27 changes: 25 additions & 2 deletions lib/pact_broker/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,33 @@
require 'semantic_logger'

module PactBroker

module Logging

def self.included(base)
base.extend(self)
base.extend self
base.extend SemanticLogger::Loggable::ClassMethods
base.class_eval do
# Returns [SemanticLogger::Logger] class level logger
def self.logger
require 'pact_broker/configuration'
@logger ||= PactBroker.configuration.custom_logger || SemanticLogger[self]
end

# Replace instance class level logger
def self.logger=(logger)
@logger = logger
end

# Returns [SemanticLogger::Logger] instance level logger
def logger
@logger ||= self.class.logger
end

# Replace instance level logger
def logger=(logger)
@logger = logger
end
end
end

def log_error e, description = nil
Expand Down
4 changes: 3 additions & 1 deletion lib/pact_broker/matrix/deployment_status_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module PactBroker
module Matrix
class DeploymentStatusSummary
include PactBroker::Logging

attr_reader :rows, :resolved_selectors, :integrations

def initialize(rows, resolved_selectors, integrations)
Expand Down Expand Up @@ -88,7 +90,7 @@ def resolved_version_for(pacticipant_id)
if resolved_selector
resolved_selector[:pacticipant_version_number]
else
PactBroker.logger.warn "Could not find the resolved version for pacticipant_id #{pacticipant_id} from integrations #{integrations.collect(&:to_s).join(", ")} in resolved selectors #{resolved_selectors.inspect}"
logger.warn "Could not find the resolved version for pacticipant_id #{pacticipant_id} from integrations #{integrations.collect(&:to_s).join(", ")} in resolved selectors #{resolved_selectors.inspect}"
"unresolved version"
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/matrix/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Row < Sequel::Model(:matrix)

dataset_module do
include PactBroker::Repositories::Helpers
include SemanticLogger::Loggable
include PactBroker::Logging

def matching_selectors selectors
if selectors.size == 1
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/pacticipants/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Service

extend PactBroker::Repositories
extend PactBroker::Services
extend PactBroker::Logging
include PactBroker::Logging

def self.messages_for_potential_duplicate_pacticipants pacticipant_names, base_url
messages = []
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module PactBroker
module Pacts
class Repository

include SemanticLogger::Loggable
include PactBroker::Logging
include PactBroker::Repositories

def create params
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/pacts/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Service

extend PactBroker::Repositories
extend PactBroker::Services
include SemanticLogger::Loggable
include PactBroker::Logging

def find_latest_pact params
pact_repository.find_latest_pact(params[:consumer_name], params[:provider_name], params[:tag])
Expand Down
4 changes: 3 additions & 1 deletion lib/pact_broker/verifications/service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'pact_broker/repositories'
require 'pact_broker/api/decorators/verification_decorator'
require 'pact_broker/verifications/summary_for_consumer_version'
require 'pact_broker/logging'

module PactBroker

Expand All @@ -11,13 +12,14 @@ module Service

extend PactBroker::Repositories
extend PactBroker::Services
include PactBroker::Logging

def next_number
verification_repository.next_number
end

def create next_verification_number, params, pact
PactBroker.logger.info "Creating verification #{next_verification_number} for pact_id=#{pact.id} from params #{params}"
logger.info "Creating verification #{next_verification_number} for pact_id=#{pact.id} from params #{params}"
verification = PactBroker::Domain::Verification.new
provider_version_number = params.fetch('providerApplicationVersion')
PactBroker::Api::Decorators::VerificationDecorator.new(verification).from_hash(params)
Expand Down
4 changes: 3 additions & 1 deletion lib/pact_broker/versions/repository.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'sequel'
require 'pact_broker/logging'
require 'pact_broker/domain/version'
require 'pact_broker/tags/repository'

module PactBroker
module Versions
class Repository

include PactBroker::Logging
include PactBroker::Repositories::Helpers

def find_by_pacticipant_id_and_number pacticipant_id, number
Expand Down Expand Up @@ -53,7 +55,7 @@ def find_by_pacticipant_name_and_number pacticipant_name, number

# There may be a race condition if two simultaneous requests come in to create the same version
def create args
PactBroker.logger.info "Upserting version #{args[:number]} for pacticipant_id=#{args[:pacticipant_id]}"
logger.info "Upserting version #{args[:number]} for pacticipant_id=#{args[:pacticipant_id]}"
version_params = {
number: args[:number],
pacticipant_id: args[:pacticipant_id],
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/webhooks/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Webhooks
class Job

include SuckerPunch::Job
include SemanticLogger::Loggable
include PactBroker::Logging
include PactBroker::Logging

def perform data
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/webhooks/webhook_request_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module PactBroker
module Webhooks
class WebhookRequestTemplate

include SemanticLogger::Loggable
include PactBroker::Logging
include PactBroker::Messages
HEADERS_TO_REDACT = [/authorization/i, /token/i]

Expand Down
9 changes: 7 additions & 2 deletions spec/lib/pact_broker/domain/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ module Domain
let(:options) { double('options') }
let(:pact) { double('pact') }
let(:verification) { double('verification') }
let(:logger) { double('logger').as_null_object }

before do
allow(webhook).to receive(:logger).and_return(logger)
end

subject(:webhook) { Webhook.new(request: request_template, consumer: consumer, provider: provider) }

Expand Down Expand Up @@ -59,8 +64,8 @@ module Domain
end

it "logs before and after" do
allow(PactBroker.logger).to receive(:info)
expect(PactBroker.logger).to receive(:info).with(/Executing/)
allow(logger).to receive(:info)
expect(logger).to receive(:info).with(/Executing/)
execute
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
module PactBroker
module Matrix
describe DeploymentStatusSummary do

before do
allow(subject).to receive(:logger).and_return(logger)
end

let(:logger) { double('logger').as_null_object }

describe ".call" do
let(:rows) { [row_1, row_2] }
let(:row_1) do
Expand Down Expand Up @@ -106,7 +113,7 @@ module Matrix
its(:reasons) { is_expected.to eq ["There is no verified pact between Foo (unresolved version) and Baz (4ee06460f10e8207ad904fa9fa6c4842e462ab59)"] }

it "logs a warning" do
expect(PactBroker.logger).to receive(:warn).with(/Could not find the resolved version/)
expect(logger).to receive(:warn).with(/Could not find the resolved version/)
subject.reasons
end
end
Expand Down
8 changes: 5 additions & 3 deletions spec/lib/pact_broker/pacticipants/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
require 'pact_broker/domain/pact'

module PactBroker

module Pacticipants
describe Service do
before do
allow(Service).to receive(:logger).and_return(logger)
end

let(:td) { TestDataBuilder.new }
let(:logger) { double('logger').as_null_object }

subject{ Service }

Expand Down Expand Up @@ -106,8 +109,7 @@ module Pacticipants
end

it "logs the names" do
allow(PactBroker.logger).to receive(:info)
expect(PactBroker.logger).to receive(:info).with(/pacticipant_name.*Fred, Mary/)
expect(logger).to receive(:info).with(/pacticipant_name.*Fred, Mary/)
subject.find_potential_duplicate_pacticipants pacticipant_name
end
end
Expand Down
8 changes: 6 additions & 2 deletions spec/lib/pact_broker/verifications/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ module PactBroker

module Verifications
describe Service do
before do
allow(Service).to receive(:logger).and_return(logger)
end

let(:logger) { double('logger').as_null_object }

subject { PactBroker::Verifications::Service }

Expand All @@ -18,8 +23,7 @@ module Verifications
let(:create_verification) { subject.create 3, params, pact }

it "logs the creation" do
allow(PactBroker.logger).to receive(:info).and_call_original
expect(PactBroker.logger).to receive(:info).with(/.*verification.*3.*success/)
expect(logger).to receive(:info).with(/.*verification.*3.*success/)
create_verification
end

Expand Down
Loading

0 comments on commit cfbb704

Please sign in to comment.