From aeefb31ca7f5db242ad492407fb91a1b652bd037 Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Mon, 1 Feb 2016 14:47:34 +0100 Subject: [PATCH] enable to configure the logger via cli --- bin/inspec | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/bin/inspec b/bin/inspec index 422ace3ec..8f8c53ec1 100755 --- a/bin/inspec +++ b/bin/inspec @@ -42,6 +42,8 @@ class InspecCLI < Thor # rubocop:disable Metrics/ClassLength desc: 'Allow remote scans with self-signed certificates (WinRM).' option :json_config, type: :string, desc: 'Read configuration from JSON file (`-` reads from stdin).' + option :log_level, aliases: :l, type: :string, + desc: 'Set the log level: info (default), debug, warn, error' end desc 'json PATH', 'read all tests in PATH and generate a JSON summary' @@ -75,6 +77,8 @@ class InspecCLI < Thor # rubocop:disable Metrics/ClassLength o = opts.dup o[:logger] = Logger.new(STDOUT) + o[:logger].level = get_log_level(o.log_level) + o[:ignore_supports] = true # we check for integrity only profile = Inspec::Profile.from_path(path, o) exit 1 unless profile.check @@ -94,6 +98,8 @@ class InspecCLI < Thor # rubocop:disable Metrics/ClassLength o = options.dup o[:logger] = Logger.new(STDOUT) + o[:logger].level = get_log_level(o.log_level) + profile = Inspec::Profile.from_path(path, o) # generate archive exit 1 unless profile.archive(opts) @@ -109,6 +115,7 @@ class InspecCLI < Thor # rubocop:disable Metrics/ClassLength o = opts.dup o[:logger] = Logger.new(opts['format'] == 'json' ? nil : STDOUT) + o[:logger].level = get_log_level(o.log_level) runner = Inspec::Runner.new(o) runner.add_tests(tests) @@ -173,6 +180,20 @@ class InspecCLI < Thor # rubocop:disable Metrics/ClassLength @json ||= conffile ? read_config(conffile) : {} end + # get the log level + # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN + def get_log_level(level) + valid = %w{debug info warn error fatal} + + if valid.include?(level) + l = level + else + l = 'info' + end + + Logger.const_get(l.upcase) + end + def read_config(file) if file == '-' puts 'WARN: reading JSON config from standard input' if STDIN.tty?