From 37f22923c5d5c06cebdc9095307228e05e1fc04e Mon Sep 17 00:00:00 2001 From: David Hollinger Date: Mon, 3 Aug 2020 14:42:01 -0500 Subject: [PATCH] Add log level options --- README.md | 8 ++++++++ app/controllers/application_controller.rb | 6 +++++- config/config.yml.example | 3 +++ config/environment.rb | 3 ++- lib/puppet_webhook.rb | 15 +++++++++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1665fb8..9119534 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,14 @@ type: Boolean description: Allow uppercase within the name of the module or environment passed to the API. default: true +### Logging Settings + +#### `loglevel` + +type: String +description: Set the Logging level for the application. Valid values are `debug`, `error`, `warn`, `fatal`, and `info`. +default `warn` + ### Configure the webhook This API provides two endpoints that need to be called: diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6951c60..7834725 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -10,7 +10,11 @@ class ApplicationController < Sinatra::Base set :database, "sqlite3:db/#{ENV['SINATRA_ENV']}.sqlite3" set :public_folder, 'public' set :views, 'app/views' - set :logger, Logger.new(STDOUT) + + # Logging settings + logger = Logger.new(STDOUT) + logger.level = LOGLEVEL + set :logger, logger end not_found do diff --git a/config/config.yml.example b/config/config.yml.example index 70398ba..2d25a1e 100644 --- a/config/config.yml.example +++ b/config/config.yml.example @@ -12,4 +12,7 @@ development: ignore_environments: [] allow_uppercase: true + # Logging + loglevel: debug + test: &development diff --git a/config/environment.rb b/config/environment.rb index f0cdcbd..b0b3397 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -7,14 +7,15 @@ config_path = ENV['WEBHOOK_CONFDIR'] || 'config/config.yml' +require './lib/puppet_webhook' config = YAML.load_file(config_path) APP_CONFIG = OpenStruct.new(config[ENV['SINATRA_ENV']]) +LOGLEVEL = PuppetWebhook.loglevel ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: "db/#{ENV['SINATRA_ENV']}.sqlite3" ) -require './lib/puppet_webhook' require './app/controllers/application_controller' require_all 'app' diff --git a/lib/puppet_webhook.rb b/lib/puppet_webhook.rb index abed2d9..c9291b9 100644 --- a/lib/puppet_webhook.rb +++ b/lib/puppet_webhook.rb @@ -6,4 +6,19 @@ module PuppetWebhook VERSION = '2.1.2' + + def self.loglevel + case APP_CONFIG.loglevel + when 'debug' + Logger::DEBUG + when 'info' + Logger::INFO + when 'error' + Logger::ERROR + when 'fatal' + Logger::FATAL + else + Logger::WARN + end + end end