diff --git a/lib/pact_broker/app.rb b/lib/pact_broker/app.rb index 586fb72bf..e308e7e2d 100644 --- a/lib/pact_broker/app.rb +++ b/lib/pact_broker/app.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/pact_broker/config/runtime_configuration.rb b/lib/pact_broker/config/runtime_configuration.rb index 7b7127f72..fbb224435 100644 --- a/lib/pact_broker/config/runtime_configuration.rb +++ b/lib/pact_broker/config/runtime_configuration.rb @@ -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 = { @@ -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 diff --git a/lib/pact_broker/configuration.rb b/lib/pact_broker/configuration.rb index 47a73834a..ca576322a 100644 --- a/lib/pact_broker/configuration.rb +++ b/lib/pact_broker/configuration.rb @@ -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 @@ -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 diff --git a/lib/pact_broker/logging.rb b/lib/pact_broker/logging.rb index a3c67d2d4..25e428769 100644 --- a/lib/pact_broker/logging.rb +++ b/lib/pact_broker/logging.rb @@ -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