Skip to content

Commit

Permalink
chore: allow logger to be configured via YAML and environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 21, 2021
1 parent 869bcd6 commit 3f80598
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
11 changes: 1 addition & 10 deletions lib/pact_broker/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def call env
attr_reader :custom_ui, :create_pact_broker_api_block

def post_configure
configure_logger
SuckerPunch.logger = configuration.custom_logger || SemanticLogger["SuckerPunch"]
configure_database_connection
configure_sucker_punch
Expand Down Expand Up @@ -242,14 +241,6 @@ def configure_sucker_punch
end
end

def configure_logger
if SemanticLogger.appenders.empty?
path = configuration.log_dir + "/pact_broker.log"
FileUtils.mkdir_p(configuration.log_dir)
SemanticLogger.add_appender(file_name: path, formatter: PactBroker::Logging::DefaultFormatter.new)
end
end

def running_app
@running_app ||= begin
prepare_app
Expand All @@ -262,7 +253,7 @@ def running_app
end

def print_startup_message
if ENV["PACT_BROKER_HIDE_PACTFLOW_MESSAGES"] != "true"
unless configuration.hide_pactflow_messages
logger.info "\n\n#{'*' * 80}\n\nWant someone to manage your Pact Broker for you? Check out https://pactflow.io/oss for a hardened, fully supported SaaS version of the Pact Broker with an improved UI + more.\n\n#{'*' * 80}\n"
end
end
Expand Down
11 changes: 11 additions & 0 deletions lib/pact_broker/config/runtime_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ class RuntimeConfiguration < Anyway::Config

LOGGING_ATTRIBUTES = {
log_dir: File.expand_path("./log"),
log_level: :info,
log_format: nil,
warning_error_class_names: ["Sequel::ForeignKeyConstraintViolation", "PG::QueryCanceled"],
hide_pactflow_messages: false
}

WEBHOOK_ATTRIBUTES = {
Expand Down Expand Up @@ -82,6 +85,14 @@ def self.getter_and_setter_method_names

sensitive_values(:database_url, :database_password)

def log_level= log_level
super(log_level&.to_sym)
end

def log_format= log_format
super(log_format&.to_sym)
end

def database_port= database_port
super(database_port&.to_i)
end
Expand Down
21 changes: 19 additions & 2 deletions lib/pact_broker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def initialize
@authenticate_with_basic_auth = nil
@authorize = nil
@api_error_reporters = []
@semantic_logger = SemanticLogger["root"]
end

# rubocop: disable Metrics/MethodLength
Expand Down Expand Up @@ -108,11 +107,29 @@ def self.default_configuration
end
# rubocop: enable Metrics/MethodLength

def logger_from_runtime_configuration
@logger_from_runtime_configuration ||= begin
SemanticLogger.default_level = runtime_configuration.log_level
if runtime_configuration.log_dir
path = runtime_configuration.log_dir + "/pact_broker.log"
FileUtils.mkdir_p(runtime_configuration.log_dir)
@default_appender = SemanticLogger.add_appender(file_name: path, formatter: runtime_configuration.log_format)
else
@default_appender = SemanticLogger.add_appender(io: $stdout, formatter: runtime_configuration.log_format)
end
@logger_from_runtime_configuration = SemanticLogger["pact-broker"]
end
end

def logger
custom_logger || @semantic_logger
custom_logger || logger_from_runtime_configuration
end

def logger= logger
if @default_appender && SemanticLogger.appenders.include?(@default_appender)
SemanticLogger.remove_appender(@default_appender)
@default_appender = nil
end
@custom_logger = logger
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def log_error e, description = nil
message = "#{description} - #{message}" if description
logger.error message
end
if ENV["PACT_BROKER_HIDE_PACTFLOW_MESSAGES"] != "true"
unless PactBroker.configuration.hide_pactflow_messages
logger.info "\n\n#{'*' * 80}\n\nPrefer it was someone else's job to deal with this error? Check out https://pactflow.io/oss for a hardened, fully supported SaaS version of the Pact Broker with an improved UI + more.\n\n#{'*' * 80}\n"
end
end
Expand Down

0 comments on commit 3f80598

Please sign in to comment.